{"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 = \u201c218765\u201d<br \/>\nOutput: \u201c251678\u201d<\/p>\n<p>Input: n = \u201c1234\u201d<br \/>\nOutput: \u201c1243\u201d<\/p>\n<p>Input: n = \u201c4321\u201d<br \/>\nOutput: \u201cNot Possible\u201d<\/p>\n<p>Input: n = \u201c534976\u201d<br \/>\nOutput: \u201c536479\u201d<\/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=\u201dbanner\u201d]\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=\u201dbanner\u201d]\n<p>Following is C++ implementation of above approach.<\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20C%2B%2B%20program%20to%20find%20the%20smallest%20number%20which%20greater%20than%20a%20given%20number%0A%2F%2F%20and%20has%20same%20set%20of%20digits%20as%20given%20number%0A%23include%20%3Ciostream%3E%0A%23include%20%3Ccstring%3E%0A%23include%20%3Calgorithm%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Utility%20function%20to%20swap%20two%20digits%0Avoid%20swap(char%20*a%2C%20char%20*b)%0A%7B%0A%20%20%20%20char%20temp%20%3D%20*a%3B%0A%20%20%20%20*a%20%3D%20*b%3B%0A%20%20%20%20*b%20%3D%20temp%3B%0A%7D%0A%20%0A%2F%2F%20Given%20a%20number%20as%20a%20char%20array%20number%5B%5D%2C%20this%20function%20finds%20the%0A%2F%2F%20next%20greater%20number.%20%20It%20modifies%20the%20same%20array%20to%20store%20the%20result%0Avoid%20findNext(char%20number%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20i%2C%20j%3B%0A%20%0A%20%20%20%20%2F%2F%20I)%20Start%20from%20the%20right%20most%20digit%20and%20find%20the%20first%20digit%20that%20is%0A%20%20%20%20%2F%2F%20smaller%20than%20the%20digit%20next%20to%20it.%0A%20%20%20%20for%20(i%20%3D%20n-1%3B%20i%20%3E%200%3B%20i\u2013)%0A%20%20%20%20%20%20%20%20if%20(number%5Bi%5D%20%3E%20number%5Bi-1%5D)%0A%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20no%20such%20digit%20is%20found%2C%20then%20all%20digits%20are%20in%20descending%20order%0A%20%20%20%20%2F%2F%20means%20there%20cannot%20be%20a%20greater%20number%20with%20same%20set%20of%20digits%0A%20%20%20%20if%20(i%3D%3D0)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22Next%20number%20is%20not%20possible%22%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20II)%20Find%20the%20smallest%20digit%20on%20right%20side%20of%20(i-1)\u2019th%20digit%20that%20is%0A%20%20%20%20%2F%2F%20greater%20than%20number%5Bi-1%5D%0A%20%20%20%20int%20x%20%3D%20number%5Bi-1%5D%2C%20smallest%20%3D%20i%3B%0A%20%20%20%20for%20(j%20%3D%20i%2B1%3B%20j%20%3C%20n%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(number%5Bj%5D%20%3E%20x%20%26%26%20number%5Bj%5D%20%3C%20number%5Bsmallest%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20smallest%20%3D%20j%3B%0A%20%0A%20%20%20%20%2F%2F%20III)%20Swap%20the%20above%20found%20smallest%20digit%20with%20number%5Bi-1%5D%0A%20%20%20%20swap(%26number%5Bsmallest%5D%2C%20%26number%5Bi-1%5D)%3B%0A%20%0A%20%20%20%20%2F%2F%20IV)%20Sort%20the%20digits%20after%20(i-1)%20in%20ascending%20order%0A%20%20%20%20sort(number%20%2B%20i%2C%20number%20%2B%20n)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22Next%20number%20with%20same%20set%20of%20digits%20is%20%22%20%3C%3C%20number%3B%0A%20%0A%20%20%20%20return%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20char%20digits%5B%5D%20%3D%20%22534976%22%3B%0A%20%20%20%20int%20n%20%3D%20strlen(digits)%3B%0A%20%20%20%20findNext(digits%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC++ Progrzm\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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}]}}