{"id":26142,"date":"2017-10-26T20:05:18","date_gmt":"2017-10-26T14:35:18","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26142"},"modified":"2017-10-26T20:05:18","modified_gmt":"2017-10-26T14:35:18","slug":"c-programming-find-next-greater-number-set-digits","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-find-next-greater-number-set-digits\/","title":{"rendered":"C++ Programming &#8211; Find next greater number with same set of digits"},"content":{"rendered":"<p>Given a number n, find the smallest number that has same set of digits as n and is greater than n. If x is the greatest possible number with its set of digits, then print \u201cnot possible\u201d.<\/p>\n<p>Examples:<br \/>\nFor simplicity of implementation, we have considered input number as a string.<\/p>\n<p>Input: n = &#8220;218765&#8221;<br \/>\nOutput: &#8220;251678&#8221;<\/p>\n<p>Input: n = &#8220;1234&#8221;<br \/>\nOutput: &#8220;1243&#8221;<\/p>\n<p>Input: n = &#8220;4321&#8221;<br \/>\nOutput: &#8220;Not Possible&#8221;<\/p>\n<p>Input: n = &#8220;534976&#8221;<br \/>\nOutput: &#8220;536479&#8221;<\/p>\n<p>Following are few observations about the next greater number.<br \/>\n1) If all digits sorted in descending order, then output is always \u201cNot Possible\u201d. For example, 4321.<br \/>\n2) If all digits are sorted in ascending order, then we need to swap last two digits. For example, 1234.<br \/>\n3) For other cases, we need to process the number from rightmost side (why? because we need to find the smallest of all greater numbers)<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>You can now try developing an algorithm yourself.<\/p>\n<p>Following is the algorithm for finding the next greater number.<br \/>\nI) Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller than the previously traversed digit. For example, if the input number is \u201c534976\u201d, we stop at 4 because 4 is smaller than next digit 9. If we do not find such a digit, then output is \u201cNot Possible\u201d.<\/p>\n<p>II) Now search the right side of above found digit \u2018d\u2019 for the smallest digit greater than \u2018d\u2019. For \u201c534976\u2033, the right side of 4 contains \u201c976\u201d. The smallest digit greater than 4 is 6.<\/p>\n<p>III) Swap the above found two digits, we get 536974 in above example.<\/p>\n<p>IV) Now sort all digits from position next to \u2018d\u2019 to the end of number. The number that we get after sorting is the output. For above example, we sort digits in bold 536974. We get \u201c536479\u201d which is the next greater number for input 534976.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C++ implementation of above approach.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++ Progrzm<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program to find the smallest number which greater than a given number<br\/>\/\/ and has same set of digits as given number<br\/>#include &lt;iostream&gt;<br\/>#include &lt;cstring&gt;<br\/>#include &lt;algorithm&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ Utility function to swap two digits<br\/>void swap(char *a, char *b)<br\/>{<br\/>    char temp = *a;<br\/>    *a = *b;<br\/>    *b = temp;<br\/>}<br\/> <br\/>\/\/ Given a number as a char array number[], this function finds the<br\/>\/\/ next greater number.  It modifies the same array to store the result<br\/>void findNext(char number[], int n)<br\/>{<br\/>    int i, j;<br\/> <br\/>    \/\/ I) Start from the right most digit and find the first digit that is<br\/>    \/\/ smaller than the digit next to it.<br\/>    for (i = n-1; i &gt; 0; i--)<br\/>        if (number[i] &gt; number[i-1])<br\/>           break;<br\/> <br\/>    \/\/ If no such digit is found, then all digits are in descending order<br\/>    \/\/ means there cannot be a greater number with same set of digits<br\/>    if (i==0)<br\/>    {<br\/>        cout &lt;&lt; &quot;Next number is not possible&quot;;<br\/>        return;<br\/>    }<br\/> <br\/>    \/\/ II) Find the smallest digit on right side of (i-1)&#039;th digit that is<br\/>    \/\/ greater than number[i-1]<br\/>    int x = number[i-1], smallest = i;<br\/>    for (j = i+1; j &lt; n; j++)<br\/>        if (number[j] &gt; x &amp;&amp; number[j] &lt; number[smallest])<br\/>            smallest = j;<br\/> <br\/>    \/\/ III) Swap the above found smallest digit with number[i-1]<br\/>    swap(&amp;number[smallest], &amp;number[i-1]);<br\/> <br\/>    \/\/ IV) Sort the digits after (i-1) in ascending order<br\/>    sort(number + i, number + n);<br\/> <br\/>    cout &lt;&lt; &quot;Next number with same set of digits is &quot; &lt;&lt; number;<br\/> <br\/>    return;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    char digits[] = &quot;534976&quot;;<br\/>    int n = strlen(digits);<br\/>    findNext(digits, n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<p>Next number with same set of digits is 536479<br \/>\nThe above implementation can be optimized in following ways.<br \/>\n1) We can use binary search in step II instead of linear search.<br \/>\n2) In step IV, instead of doing simple sort, we can apply some clever technique to do it in linear time. Hint: We know that all digits are linearly sorted in reverse order except one digit which was swapped.<\/p>\n<p>With above optimizations, we can say that the time complexity of this method is O(n).<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming &#8211; Find next greater number with same set of digits &#8211; Mathematical Algorithms &#8211; find the smallest number that has same set of digits as n <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83515,1,74058],"tags":[77406,77377,75676,77392,77372,77369,77382,77384,77403,77394,77388,75652,75666,77399,77397,77391,77383,77376,77404,77389,77395,77370,77375,77385,77381,77402,77407,75583,75590,75668,75660,75672,75670,77401,75673,77380,74059,77371,77379,77387,77378,77405,77400,77373,77374,77398,77386,77396,77390,77393],"class_list":["post-26142","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","category-mathematical-algorithms","tag-3-digit-by-2-digit-multiplication","tag-3-digit-number","tag-6-digit-smallest-number","tag-all-numbers","tag-digits-math","tag-excel-roundup-to-nearest-100","tag-greatest-6-digit-number","tag-how-many-numbers-exactly-have-2-digits","tag-how-to-divide-2-digit-numbers","tag-how-to-divide-two-digit-numbers","tag-largest-3-digit-prime-number","tag-largest-4-digit-number","tag-largest-5-digit-number","tag-largest-one-digit-number","tag-magic-number-trick","tag-math-digits","tag-math-tricks-with-answer","tag-mathematics-tricks-of-calculation","tag-multiples-of-two","tag-multiplication-tricks-for-3-digit-numbers","tag-number-of-squares","tag-number-square","tag-number-tricks","tag-pick-a-number-between-1-and-10-trick","tag-rounding-numbers","tag-same-set","tag-set-of-digits","tag-smallest-2-digit-number","tag-smallest-4-digit-number","tag-smallest-5-digit-number","tag-smallest-8-digit-number","tag-smallest-five-digit-number","tag-smallest-four-digit-number","tag-smallest-number-of-one-digit","tag-smallest-one-digit-number","tag-smallest-one-digit-number-is","tag-smallest-three-digit-number","tag-square-numbers","tag-squares-and-cubes-upto-50","tag-squares-of-numbers","tag-squares-upto-30","tag-sum-of-4-digit-number","tag-the-smallest-one-digit-number","tag-three-digit-multiplication","tag-two-digit-number","tag-what-are-square-numbers","tag-what-is-the-largest-2-digit-number","tag-what-is-the-smallest-number","tag-what-is-the-smallest-one-digit-number","tag-which-is-the-smallest-one-digit-number"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26142"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26142\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}