Comments on: Programming Interview Questions 24: Find Next Higher Number With Same Digits /2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/?utm_source=rss&utm_medium=rss&utm_campaign=programming-interview-questions-24-find-next-higher-number-with-same-digits Information Retrieval and Machine Learning Mon, 24 Sep 2012 14:57:41 +0000 hourly 1 http://wordpress.org/?v=3.3 By: Dilip /2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/#comment-6130 Dilip Wed, 01 Aug 2012 12:43:56 +0000 /?p=914#comment-6130 But you can't just swap pivot element and pivdx+1 element in all cases. Say the input is 13621. The pivot is 3. After sorting, it becomes 13126. So in this case we have to swap pivot with 6. The moral is you have to traverse through the list to find out the smallest no, greater than the pivot. We can do binary search to find swap element. Correct me if i'm wrong But you can’t just swap pivot element and pivdx+1 element in all cases.
Say the input is 13621. The pivot is 3. After sorting, it becomes 13126. So in this case we have to swap pivot with 6. The moral is you have to traverse through the list to find out the smallest no, greater than the pivot.
We can do binary search to find swap element.
Correct me if i’m wrong

]]>
By: vaibhav goyal /2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/#comment-5932 vaibhav goyal Tue, 24 Jul 2012 10:43:48 +0000 /?p=914#comment-5932 Hi Arden, There is a huge flaw, the number n has (log n) digits and hence there are (log n)! permutations O((log n)!) is not equal to O(n log n), infact much less; for example the number take a five digit number, number of permutations possible are only 5! and we can use some other sort like radix sort which here would take much less time. Thanks Hi Arden,
There is a huge flaw, the number n has (log n) digits and hence there are (log n)! permutations O((log n)!) is not equal to O(n log n), infact much less; for example the number take a five digit number, number of permutations possible are only 5! and we can use some other sort like radix sort which here would take much less time.
Thanks

]]>
By: arpita /2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/#comment-5641 arpita Thu, 12 Jul 2012 12:01:49 +0000 /?p=914#comment-5641 really nice algo..thnks a lot really nice algo..thnks a lot

]]>
By: Aravind Prasad /2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/#comment-5636 Aravind Prasad Thu, 12 Jul 2012 05:38:18 +0000 /?p=914#comment-5636 Nice logic .. Well explained .. Nice logic .. Well explained ..

]]>
By: Ashot Madatyan /2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/#comment-5070 Ashot Madatyan Mon, 18 Jun 2012 20:41:31 +0000 /?p=914#comment-5070 You could also do a slight modification. As soon as the pivot index is found, sort the array from pividx+1 to the end, and swap the pividx element with the pividx+1. You could also do a slight modification. As soon as the pivot index is found, sort the array from pividx+1 to the end, and swap the pividx element with the pividx+1.

]]>
By: Varadharajan /2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/#comment-4409 Varadharajan Fri, 11 May 2012 04:30:50 +0000 /?p=914#comment-4409 Thank you... Thank you…

]]>
By: Arden /2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/#comment-4404 Arden Thu, 10 May 2012 22:35:43 +0000 /?p=914#comment-4404 I mention this case right after the code: "Note that if the digits of the given number is monotonically increasing from right to left, like 43221 then we won’t perform any operations, which is what we want because this is the highest number obtainable from these digits. There’s no higher number, so we return the given number itself." Digits of both 9999 and 10000 are monotonically increasing from right to left. I mention this case right after the code:
“Note that if the digits of the given number is monotonically increasing from right to left, like 43221 then we won’t perform any operations, which is what we want because this is the highest number obtainable from these digits. There’s no higher number, so we return the given number itself.”

Digits of both 9999 and 10000 are monotonically increasing from right to left.

]]>
By: Sandipan /2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/#comment-4380 Sandipan Wed, 09 May 2012 09:18:30 +0000 /?p=914#comment-4380 Since we are search for right to left for a digit which is less than the letter one. i.e. arr[i] < arr[i+1] Here the index goes lesser than 0 - it indicates that it is the greater element - no greater element can be found with same digits. Since we are search for right to left for a digit which is less than the letter one.
i.e. arr[i] < arr[i+1]
Here the index goes lesser than 0 – it indicates that it is the greater element – no greater element can be found with same digits.

]]>
By: Varadharajan /2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/#comment-4379 Varadharajan Wed, 09 May 2012 09:03:44 +0000 /?p=914#comment-4379 Hi, Whatif the numbers like 9999,10000??? Can you please tell me?? Thanks. Hi,
Whatif the numbers like 9999,10000???
Can you please tell me??
Thanks.

]]>
By: Ashot Madatyan /2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/#comment-1777 Ashot Madatyan Sun, 05 Feb 2012 19:57:10 +0000 /?p=914#comment-1777 Terse, comprehensive and exact description of the algorithm. Thanks for that. Terse, comprehensive and exact description of the algorithm. Thanks for that.

]]>