IBM Interview Question
Software Engineer / DevelopersI dont think this answer is right, why would you want to increase the middle digit by 1? the answer should be 1354531.
also, for subcase 1, if the number is 8754719, then you answer would be 8754578, which is not correct.
My algorithm is as follows:
Case I, same;
Case II,
subcase I, if The reverse of "number to the left of the middle digit" is greater than the number on the right eg:2194135 (here 912 > 135)
reverse the left side numbers and replace the right side numbers with them, therefore the ans will be 2194912
subcase II, if The reverse of "number to the left of the middle digit" A is smaller than the number on the rightB eg:2174835 (here A = 712 < B = 835), then find the index i of first number in A that is less than Bi, set Ai = Bi and set every digit else in B equals to A
#include<stdio.h>
#include<string.h>
int main()
{
char a[1000001];
int n;
scanf("%d", &n);
while(scanf("%s", a)!=EOF)
{
int len = strlen(a), l, r, center;
if(len%2 == 0)
{l = len/2-1; r = len/2;}
else
{l = r = len/2;}
center = l;
while(l>=0 && r<len)
{
if(a[l] != a[r])
break;
l--; r++;
}
if(l<0 || a[l]<a[r])
{
l = center;
int carry = 1;
do{
a[l] += carry;
if(a[l] == '9'+1)
a[l] = '0';
else
{carry = 0;break;}
l--;
}while(carry==1 & l>=0);
if(l<0)
{
int i;
for(i=0;i<len-1;i++)
a[i] = '0';
a[len-1] = '1';
printf("1%s\n", a);
continue;
}
}
if(len%2 == 0)
{l = len/2-1; r = len/2;}
else
{l = r = len/2;}
while(l>=0 && r<len)
{
a[r++] = a[l--];
}
printf("%s\n", a);
}
}
//I know it is not concise, but it works.. the algorithm is simple
#include <iostream>
using namespace std;
int main()
{
int val = 57811;
int count =0;
int a[10];
int b[10];
int mode = val;
int div = 0;
//int temp = val;
int mid = 0;
while(mode)
{
mode = mode/10;
count++;
}
mode = val;
for(int i = 0; i<count; i++)
{
div = mode%10;
mode = mode/10;
a[count-1-i]=div;
//cout<<" " <<a[i];
}
if((count%2)!= 0)
mid = count/2 +1;
else
mid = count/2 ;
for(int j=0;j<mid;j++)
{
if(a[j] == a[count-1-j])
{
b[count-1-j] = a[j];
b[j] = a[j];
}
else if(a[j]>a[count-1-j])
{
b[count-1-j] = a[j];
b[j] = a[j];
}
else
{
if(j == mid -1)
b[j] = b[count-1-j] = a[j]+1;
else{
a[j+1] = a[j+1]+1;
b[j+1] = a[j+1];
b[count-j-2]=a[j+1];
a[count-j-2] = a[j+1];
b[j] = a[j];
b[count-1-j] = a[j];
}
}
}
for(int k=0;k<count;k++)
cout<<" " <<b[k];
return 0;
}
I have the following algo: please let me know if there are any faults in it..
- Nameless September 18, 2010Case I: The given number is a palindrome..eg: 1234321
then simply increment the center digit by 1 i.e the ans is 1235321
CaseII: The given number is not a palindrome:
Subcase 1: The number to the left of the middle digit is greater than the number on the right eg:2194135 (here 219 > 135)
reverse the left side numbers and replace the right side numbers with them, therefore the ans will be 2194912
Subcase 2:The number to the right of the middle digit is greater than the number on the left eg:1354219
then increment the middle digit by 1 and then reverse the left side numbers and replace the right side numbers with them.. therefore the answer would be 135 5(middle digit incremented) 531(reversed).1355531.
Note: in case the middle digit is 9 then incrementing it would make it 10..so put 0 instead of 9 and increment the first right and left neighboring digit of 9 by 1.