Tuesday, April 15, 2014

Given a number, find the next higher number which has the exact same set of digits as the original number

You can do it in O(n) (where n is the number of digits) like this:
Starting from the right, you find the first pair-of-digits such that the left-digit is smaller than the right-digit. Let's refer to the left-digit by "digit-x". Find the smallest number larger than digit-x to the right of digit-x, and place it immediately left of digit-x. Finally, sort the remaining digits in ascending order - since they were already in descending order, all you need to do is reverse them (save for digit-x, which can be placed in the correct place in O(n)).
An example will make this more clear:

123456784987654321
start with a number

123456784 987654321
         ^the first place where the left-digit is less-than the right-digit is here.  
         Digit "x" is 4

123456784 987654321
              ^find the smallest digit larger than 4 to the right

123456785 4 98764321
        ^place it to the left of 4

123456785 4 12346789
123456785123446789
         ^sort the digits to the right of 5.  Since all of them except 
         the '4' were already in descending order, all we need to do is 
         reverse their order, and find the correct place for the '4'
code---->
#include<stdio.h>
void nextno(int x , int y){
 int arr[y];
 int i,j,temp,len=y;
 for(i=y-1;i>=0;i--,x=x/10)
 arr[i]=x%10;
 i=y-1;
  while(i>=0){
      if(i==0){
         i--;
     break ;
     }
     if(arr[i]>arr[i-1]){
        i--;
        break;
    }
    else
        i--;
}
if(i<0){
    printf("no number found ");
return ;
}
for(j=i+1;j<y;j++){
    if(arr[j] <= arr[i]){
            j--;
            break ;
            }
     if(j==y-1)
        break ;
}
temp = arr[i];
arr[i]=arr[j];
arr[j]=temp ;
i++;
y--;
while(i<y){
    temp=arr[i];
    arr[i]=arr[y];
    arr[y]=temp;
    i++;y--;
}
for(i=0;i<len;i++)
    printf("%d",arr[i]);
}
void main(){
int x=27653 ;
int y=0,z=x;
while(z){
z=z/10;
y++;
}
nextno(x,y);
}

No comments:

Post a Comment