-1

I want number of ways to divide an array of possitive integers such that maximum value of left part of array is greater than or equal to the maximum value of right part of the array.

For example, 6 4 1 2 1 can be divide into:

[[6,4,1,2,1]] [[6][4,1,2,1]] [[6,4][1,2,1]] [[6,4,1][2,1]] [[6,4,1,2][1]] [[6][4,1][2,1]] [[6][4][1,2,1]] [[6][4][1,2][1]] [[6][4,1,2][1]] [[6,4,1][2][1]] [[6][4,1][2][1]] [[6,4][1,2][1]]

which are total 12 ways of partitioning.

I tried a recursive approach but it fails beacause of termination due to exceed of time limit. Also this approach is not giving correct output always.

In this another approach, I took the array ,sort it in decreasing order and then for each element I checked weather it lies on right of the original array, and if does then added it's partitions to it's previous numbers too.

I want an approach to solve this, any implementation or pseudocode or just an idea to do this would be appreciable.

Daemon
  • 55
  • 6
  • 3
    First thing you need to do is to choose proper variable names. – Khalil Khalaf Jun 24 '17 at 20:38
  • @KyleKhalaf Okay! Sorry about that. – Daemon Jun 24 '17 at 20:39
  • @Daemon _"... but it fails beacause of termination due to exceed of time limit ..."_ There's no such cause of failure regularly. What are you doing? – πάντα ῥεῖ Jun 24 '17 at 20:42
  • 1
    By failure here I meant that its not running up to the desired time limit. It's taking more time then it should. As tag suggests I want to reduce its time complexity. – Daemon Jun 24 '17 at 20:46
  • Another small comment, try to avoid using global variables. – Fede Jun 24 '17 at 20:50
  • @Daemon There's no time limit running a program in any OS I know of. If you're asking to improve performance your question might be better placed at [SE Code Review](https://codereview.stackexchange.com/). – πάντα ῥεῖ Jun 24 '17 at 20:50
  • 6
    Then this needs to go to [**Code Review**](https://codereview.stackexchange.com) – Khalil Khalaf Jun 24 '17 at 20:51
  • No I am not asking for performance , my code is totally wrong for codereview. I guess I mentioned in the question that it's not giving proper answer and I need some other implementation. – Daemon Jun 24 '17 at 20:54
  • 1
    @Daemon Please refrain asking questions about online code judge engines here. It's very unlikely that anyone could tell you where you failed from their test cases, as these aren't disclosed usually. Even if what you tested was running at your local environment, you may have missed to test some edge cases which are applied in the online challenge. Be creative and try to find them. Additionally there's probably no value for such questions in the long term, other than cheating the online contest, and nothing is learned. – πάντα ῥεῖ Jun 24 '17 at 20:55
  • https://www.youtube.com/watch?v=x3W507oli0g – πάντα ῥεῖ Jun 24 '17 at 20:57
  • @Daemon There are so many things wrong with that code. Lets start here: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). And it's more than typical for online code juge engine. A friend gave this to you?? Stop asking such questions here please. The purpose of the site isn't to help you or your friend with your personal issues you have with that code challenge. – πάντα ῥεῖ Jun 24 '17 at 21:03
  • Wow, now downvote it if you don't think of a solution. So that other stackoverflow users when got the same problem can't find it here. And when they ask downvote theirs also. – Daemon Jun 24 '17 at 21:05
  • 1
    @Daemon I already down- and close voted your question of course. There are no such problems in real world progtamming. So helping others in tuture research is pretty unlikely. – πάντα ῥεῖ Jun 24 '17 at 21:10
  • @πάνταῥεῖ You know that it's not the problem here. It worked in my compiler so I used it. Anyway I updated the code and put the suitable header files. – Daemon Jun 24 '17 at 21:11
  • In order to figure out why you are getting the wrong answer, I suggest that you learn some [debugging skills](https://duckduckgo.com/?t=lm&q=beginner+debugging+skills&ia=web). – Code-Apprentice Jun 24 '17 at 21:13
  • @πάνταῥεῖ Why you think that it wont help future users? programming is not only thing that stackoverflow users can ask , it's on the logical thinking and algorithm forming also. I am definitely sure that this will help other users also in future. – Daemon Jun 24 '17 at 21:14
  • @Code-Apprentice I tried to debug it already and I know how to do that. Anyway thanks. But the problem here is in my logic not code and I am not seeking for any debugging help. – Daemon Jun 24 '17 at 21:16
  • An error in your logic means there is an error in your code. Stepping through your code line by line will help you find that error. – Code-Apprentice Jun 24 '17 at 22:05
  • 1
    @πάνταῥεῖ: Trivial aside: the operating systems for most Control Data mainframes required each job to include a card to specify its time limit. Maximum allowed worked out to approximately 9.1 hours of CPU time (but I doubt that was used very often, given the cost of a CPU hour on a machine that cost 7 or 8 million dollars in 1964). – Jerry Coffin Jun 24 '17 at 22:06
  • @Jerry Cool fact ;-) – πάντα ῥεῖ Jun 24 '17 at 22:07
  • @Code-Apprentice, My code is doing what it should do, it is behaving properly but I am saying that I don't know how to solve that problem. I just posted my code to show what I did. Now I want you(others) to give me a better idea through implementation or by just saying what should I do to solve this efficiently. – Daemon Jun 24 '17 at 22:14
  • "but I am saying that I don't know how to solve that problem" What problem? – Code-Apprentice Jun 24 '17 at 22:34
  • 2
    "Also this approach is not giving correct output always." but "My code is doing what it should do, it is behaving properly" Both of these statements cannot be correct. Which is it? – Code-Apprentice Jun 24 '17 at 22:35
  • Both are correct. The approach is not giving correct output because it is wrong approach but my code is behaving properly because it is coded with worong approach and giving wrong answers according to the approach it is coded. And I mentioned there also from where you have coppien these lines that " I don't know how to solve this problem" because my logic is wrong. I want the correct logic . Also I already said that " I mentioned in the question that it's not giving proper answer and I need some other implementation" , the implementation I did was done with wrong logic. I wnt the correct logic – Daemon Jun 24 '17 at 22:57
  • I also said that I just posted the code so you(others) may not ask what you did? . So please instead of making unnecessary false contradictions if you use your brain in thinking of an approach that would be nice. – Daemon Jun 24 '17 at 23:02
  • @Daemon That's why online code judge engins are bad: You sound seriously confused. – πάντα ῥεῖ Jun 24 '17 at 23:11
  • Does that sound really confusing to you? Then I guess you can only read posts and try just to prove that question is vague . Instead I thought there is someone out there who will rather try to use his/her head to solve the problem which I clearly mentioned `I want an approach to solve this, any implementation or pseudocode or just an idea to do this would be appreciable.` and specified the problem `I want number of ways to divide an array of possitive integers such that maximum value of left part of array is greater than or equal to the maximum value of right part of the array.` sufficiently. – Daemon Jun 24 '17 at 23:16
  • @πάνταῥεῖ Suffcient enough for a normal person to understand rather than being confused. But still there are other people out there on StackOverflow who don't know any answer and just try to tease users so that they quit asking on stackOverflow. Is that your motivative here also? – Daemon Jun 24 '17 at 23:18
  • @Daemon Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Jun 24 '17 at 23:19
  • @πάνταῥεῖ I already took that tour , `Specific programming problems` this is the one which you can ask on SO. I guess you should go and look again. – Daemon Jun 24 '17 at 23:21
  • @Daemon _"I already took that tour "_ You obviously didn't. Where is your _informed_ badge then? – πάντα ῥεῖ Jun 24 '17 at 23:23
  • Badge is always necessary for you right and gain reputation right?? Well you can go and check for this same sentence `Specific programming problems` in your tour.....I guess you need to update everything. – Daemon Jun 24 '17 at 23:26
  • @Daemon Your attempts to convince me you know better how the site works than me are pretty futile. – πάντα ῥεῖ Jun 24 '17 at 23:34
  • When you take the tour you don't get the badge, but when you click on the button for help centre then only you get the badge. Well, i too come to know about that just now when you were struggling to find some points just to say which makes you superior and pointing out that I don't have that badge or this or that....Moreover,coming back on the topic which you were arguing about, in the tour itself it is specified that how and what should the user should ask and so I didn't need to go in depth. But since you forced I took that now what? The question I asked is already in the list for asking. – Daemon Jun 24 '17 at 23:35
  • Well I am not convincing you or anything, I am just making the point which you are helping me to prove that their are some users who just want questions to be removed , they even don't see what problem is and just try to pointing out more mistakes gains reputations and whatever....but their are others also who struggles with the problems and try to find out the solution and by that they gain reputations. It's just the difference among people of StackOverflow. – Daemon Jun 24 '17 at 23:39
  • @KyleKhalaf (and those who upvoted that comment), the question clearly states "*this approach is not giving correct output always*". That means it is **not** on topic for Code Review. – Peter Taylor Jun 26 '17 at 11:19
  • @petertaylor and it clearly says "in another approach" where op edit his q and removed the code and that the other approach works but complicated. See history – Khalil Khalaf Jun 26 '17 at 12:22

1 Answers1

0

I designed a simple recursive algorithm. I will try to explain on your example;

  • First, check if [6] is a possible/valid part of a partition.
  • It is a valid partition because maximum element of ([6]) is bigger than remaining part's ([4,1,2,1]) maximum value.
  • Since it is a valid partition, we can use recursive part of the algorithm.

    concatenate([6],algorithm([4,1,2,1]))
    
  • now the partitions

    [[6][4,1,2,1]], [[6][4,1][2,1]], [[6][4,1][2,1]] [[6][4][1,2,1]] [[6][4][1,2][1]] [[6][4,1,2][1]] 
    

    are in our current solution set.

  • Check if [6,4] is a possible/valid part of a partition.

  • Continue like this until reaching [6,4,1,2,1].
HK1911
  • 59
  • 8