{"id":25813,"date":"2017-10-25T20:47:24","date_gmt":"2017-10-25T15:17:24","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25813"},"modified":"2017-10-25T20:47:24","modified_gmt":"2017-10-25T15:17:24","slug":"c-programming-given-number-find-next-smallest-palindrome","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-given-number-find-next-smallest-palindrome\/","title":{"rendered":"C Programming &#8211; Given a number, find the next smallest palindrome"},"content":{"rendered":"<p>Given a number, find the next smallest palindrome larger than this number. For example, if the input number is \u201c2 3 5 4 5\u201d, the output should be \u201c2 3 6 3 2\u201d. And if the input number is \u201c9 9 9\u201d, the output should be \u201c1 0 0 1\u201d.<span id=\"more-23497\"><\/span><\/p>\n<p>The input is assumed to be an array. Every entry in array represents a digit in input number. Let the array be \u2018num[]\u2019 and size of array be \u2018n\u2019<\/p>\n<p>There can be three different types of inputs that need to be handled separately.<br \/>\n<strong>1)<\/strong> The input number is palindrome and has all 9s. For example \u201c9 9 9\u201d. Output should be \u201c1 0 0 1\u201d<br \/>\n<strong>2)<\/strong> The input number is not palindrome. For example \u201c1 2 3 4\u201d. Output should be \u201c1 3 3 1\u201d<br \/>\n<strong>3) <\/strong>The input number is palindrome and doesn\u2019t have all 9s. For example \u201c1 2 2 1\u201d. Output should be \u201c1 3 3 1\u201d.<\/p>\n<p>Solution for input type 1 is easy. The output contains n + 1 digits where the corner digits are 1, and all digits between corner digits are 0.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Now let us first talk about input type 2 and 3. How to convert a given number to a greater palindrome? To understand the solution, let us first define the following two terms:<br \/>\nLeft Side: The left half of given number. Left side of \u201c1 2 3 4 5 6\u201d is \u201c1 2 3\u201d and left side of \u201c1 2 3 4 5\u201d is \u201c1 2\u201d<br \/>\nRight Side: The right half of given number. Right side of \u201c1 2 3 4 5 6\u201d is \u201c4 5 6\u201d and right side of \u201c1 2 3 4 5\u201d is \u201c4 5\u201d<br \/>\nTo convert to palindrome, we can either take the mirror of its left side or take mirror of its right side. However, if we take the mirror of the right side, then the palindrome so formed is not guaranteed to be next larger palindrome. So, we must take the mirror of left side and copy it to right side. But there are some cases that must be handled in different ways. See the following steps.<\/p>\n<p>We will start with two indices i and j. i pointing to the two middle elements (or pointing to two elements around the middle element in case of n being odd). We one by one move i and j away from each other.<\/p>\n<p>Step 1. Initially, ignore the part of left side which is same as the corresponding part of right side. For example, if the number is \u201c8 3 4 2 2 4 6 9\u2033, we ignore the middle four elements. i now points to element 3 and j now points to element 6.<\/p>\n<p>Step 2. After step 1, following cases arise:<\/p>\n<p>Case 1: Indices i &amp; j cross the boundary.<br \/>\nThis case occurs when the input number is palindrome. In this case, we just add 1 to the middle digit (or digits in case n is even) propagate the carry towards MSB digit of left side and simultaneously copy mirror of the left side to the right side.<br \/>\nFor example, if the given number is \u201c1 2 9 2 1\u201d, we increment 9 to 10 and propagate the carry. So the number becomes \u201c1 3 0 3 1\u201d<\/p>\n<p>Case 2: There are digits left between left side and right side which are not same. So, we just mirror the left side to the right side &amp; try to minimize the number formed to guarantee the next smallest palindrome.<br \/>\nIn this case, there can be two sub-cases.<\/p>\n<p>2.1) Copying the left side to the right side is sufficient, we don\u2019t need to increment any digits and the result is just mirror of left side. Following are some examples of this sub-case.<br \/>\nNext palindrome for \u201c7 8 3 3 2 2\u2033 is \u201c7 8 3 3 8 7\u201d<br \/>\nNext palindrome for \u201c1 2 5 3 2 2\u2033 is \u201c1 2 5 5 2 1\u201d<br \/>\nNext palindrome for \u201c1 4 5 8 7 6 7 8 3 2 2\u2033 is \u201c1 4 5 8 7 6 7 8 5 4 1\u201d<br \/>\nHow do we check for this sub-case? All we need to check is the digit just after the ignored part in step 1. This digit is highlighted in above examples. If this digit is greater than the corresponding digit in right side digit, then copying the left side to the right side is sufficient and we don\u2019t need to do anything else.<\/p>\n<p>2.2) Copying the left side to the right side is NOT sufficient. This happens when the above defined digit of left side is smaller. Following are some examples of this case.<br \/>\nNext palindrome for \u201c7 1 3 3 2 2\u2033 is \u201c7 1 4 4 1 7\u201d<br \/>\nNext palindrome for \u201c1 2 3 4 6 2 8\u2033 is \u201c1 2 3 5 3 2 1\u201d<br \/>\nNext palindrome for \u201c9 4 1 8 7 9 7 8 3 2 2\u2033 is \u201c9 4 1 8 8 0 8 8 1 4 9\u201d<br \/>\nWe handle this subcase like Case 1. We just add 1 to the middle digit (or digits in ase n is even) propagate the carry towards MSB digit of left side and simultaneously copy mirror of the left side to the right side.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/> <br\/>\/\/ A utility function to print an array<br\/>void printArray (int arr[], int n);<br\/> <br\/>\/\/ A utility function to check if num has all 9s<br\/>int AreAll9s (int num[], int n );<br\/> <br\/>\/\/ Returns next palindrome of a given number num[].<br\/>\/\/ This function is for input type 2 and 3<br\/>void generateNextPalindromeUtil (int num[], int n )<br\/>{<br\/>    \/\/ find the index of mid digit<br\/>    int mid = n\/2;<br\/> <br\/>    \/\/ A bool variable to check if copy of left side to right is sufficient or not<br\/>    bool leftsmaller = false;<br\/> <br\/>    \/\/ end of left side is always &#039;mid -1&#039;<br\/>    int i = mid - 1;<br\/> <br\/>    \/\/ Begining of right side depends if n is odd or even<br\/>    int j = (n % 2)? mid + 1 : mid;<br\/> <br\/>   \/\/ Initially, ignore the middle same digits <br\/>    while (i &gt;= 0 &amp;&amp; num[i] == num[j])<br\/>        i--,j++;<br\/> <br\/>    \/\/ Find if the middle digit(s) need to be incremented or not (or copying left <br\/>    \/\/ side is not sufficient)<br\/>    if ( i &lt; 0 || num[i] &lt; num[j])<br\/>        leftsmaller = true;<br\/> <br\/>    \/\/ Copy the mirror of left to tight<br\/>    while (i &gt;= 0)<br\/>    {<br\/>        num[j] = num[i];<br\/>        j++;<br\/>        i--;<br\/>    }<br\/> <br\/>    \/\/ Handle the case where middle digit(s) must be incremented. <br\/>    \/\/ This part of code is for CASE 1 and CASE 2.2<br\/>    if (leftsmaller == true)<br\/>    {<br\/>        int carry = 1;<br\/>        i = mid - 1;<br\/> <br\/>        \/\/ If there are odd digits, then increment<br\/>        \/\/ the middle digit and store the carry<br\/>        if (n%2 == 1)<br\/>        {<br\/>            num[mid] += carry;<br\/>            carry = num[mid] \/ 10;<br\/>            num[mid] %= 10;<br\/>            j = mid + 1;<br\/>        }<br\/>        else<br\/>            j = mid;<br\/> <br\/>        \/\/ Add 1 to the rightmost digit of the left side, propagate the carry <br\/>        \/\/ towards MSB digit and simultaneously copying mirror of the left side <br\/>        \/\/ to the right side.<br\/>        while (i &gt;= 0)<br\/>        {<br\/>            num[i] += carry;<br\/>            carry = num[i] \/ 10;<br\/>            num[i] %= 10;<br\/>            num[j++] = num[i--]; \/\/ copy mirror to right<br\/>        }<br\/>    }<br\/>}<br\/> <br\/>\/\/ The function that prints next palindrome of a given number num[]<br\/>\/\/ with n digits.<br\/>void generateNextPalindrome( int num[], int n )<br\/>{<br\/>    int i;<br\/> <br\/>    printf(&quot;\\nNext palindrome is:\\n&quot;);<br\/> <br\/>    \/\/ Input type 1: All the digits are 9, simply o\/p 1<br\/>    \/\/ followed by n-1 0&#039;s followed by 1.<br\/>    if( AreAll9s( num, n ) )<br\/>    {<br\/>        printf( &quot;1 &quot;);<br\/>        for( i = 1; i &lt; n; i++ )<br\/>            printf( &quot;0 &quot; );<br\/>        printf( &quot;1&quot; );<br\/>    }<br\/> <br\/>    \/\/ Input type 2 and 3<br\/>    else<br\/>    {<br\/>        generateNextPalindromeUtil ( num, n );<br\/> <br\/>        \/\/ print the result<br\/>        printArray (num, n);<br\/>    }<br\/>}<br\/> <br\/>\/\/ A utility function to check if num has all 9s<br\/>int AreAll9s( int* num, int n )<br\/>{<br\/>    int i;<br\/>    for( i = 0; i &lt; n; ++i )<br\/>        if( num[i] != 9 )<br\/>            return 0;<br\/>    return 1;<br\/>}<br\/> <br\/>\/* Utility that prints out an array on a line *\/<br\/>void printArray(int arr[], int n)<br\/>{<br\/>    int i;<br\/>    for (i=0; i &lt; n; i++)<br\/>        printf(&quot;%d &quot;, arr[i]);<br\/>    printf(&quot;\\n&quot;);<br\/>}<br\/> <br\/>\/\/ Driver Program to test above function<br\/>int main()<br\/>{<br\/>    int num[] = {9, 4, 1, 8, 7, 9, 7, 8, 3, 2, 2};<br\/> <br\/>    int n = sizeof (num)\/ sizeof(num[0]);<br\/> <br\/>    generateNextPalindrome( num, n );<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Next palindrome is:\r\n9 4 1 8 8 0 8 8 1 4 9<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Programming &#8211; Given a number, find the next smallest palindrome &#8211; Mathematical Algorithms &#8211; For example, if the input number is \u201c2 3 5 4 5\u201d.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,74058],"tags":[70812,70842,70843,75513,75067,73134,70832,74075,74066,75516,70848,75066,74837,74835,70828,74832,73145,75072,75512,75518,75515,75192,75195,72110,74082,73147],"class_list":["post-25813","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-mathematical-algorithms","tag-array-in-c-programming","tag-array-in-c-programming-examples","tag-array-programming","tag-c-program-to-find-sum-of-n-numbers-using-function","tag-c-program-to-print-given-number-in-words","tag-c-program-to-store-student-information-using-array","tag-c-programming-codes","tag-composite-numbers-from-1-to-1000","tag-for-loop-c-programming","tag-for-loop-c-programming-examples","tag-for-loop-in-c-programming","tag-for-loop-programs-in-c","tag-function-c-programming","tag-function-in-c-programming-examples","tag-functions-in-c-programming","tag-functions-in-c-programming-with-examples","tag-list-of-array-programs-in-c","tag-loop-program-in-c","tag-palindrome-number-program-in-php","tag-palindrome-program-in-python","tag-prime-no-code-in-c","tag-prime-no-program-in-c","tag-prime-number-program-in-c-language","tag-prime-number-program-in-c","tag-program-for-prime-number-in-c","tag-write-a-complete-program-that-declares-an-integer-variable"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25813","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=25813"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25813\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}