Skip to content

Instantly share code, notes, and snippets.

@dineshnagarit
Last active May 18, 2020 08:51
Show Gist options
  • Save dineshnagarit/d2f4fc8633290431ca6ca6cc38899273 to your computer and use it in GitHub Desktop.
Save dineshnagarit/d2f4fc8633290431ca6ca6cc38899273 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void printArray (int arr[], int n);
int AreAll9s (int num[], int n );
void generateNextPalindromeUtil (int num[], int n )
{
int mid = n/2;
bool leftsmaller = false;
int i = mid - 1;
int j = (n % 2)? mid + 1 : mid;
while (i >= 0 && num[i] == num[j])
i--,j++;
if ( i < 0 || num[i] < num[j])
leftsmaller = true;
while (i >= 0)
{
num[j] = num[i];
j++;
i--;
}
if (leftsmaller == true)
{
int carry = 1;
i = mid - 1;
if (n%2 == 1)
{
num[mid] += carry;
carry = num[mid] / 10;
num[mid] %= 10;
j = mid + 1;
}
else
j = mid;
while (i >= 0)
{
num[i] += carry;
carry = num[i] / 10;
num[i] %= 10;
num[j++] = num[i--]; // copy mirror to right
}
}
}
void generateNextPalindrome( int num[], int n )
{
int i;
printf("Next palindrome is:");
if( AreAll9s( num, n ) )
{
printf( "1 ");
for( i = 1; i < n; i++ )
printf( "0 " );
printf( "1" );
}
else
{
generateNextPalindromeUtil ( num, n );
printArray (num, n);
}
}
// function to check if num has all 9s
int AreAll9s( int* num, int n )
{
int i;
for( i = 0; i < n; ++i )
if( num[i] != 9 )
return 0;
return 1;
}
/* prints out an array on a line */
void printArray(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int num[] = {9, 4, 1, 8, 7, 9, 7, 8, 3, 2, 2};
int n = sizeof (num)/ sizeof(num[0]);
generateNextPalindrome( num, n );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment