Next Greater Number - Same Digits ID:2673



A number N is passed as the input. The program must print the next greater number with the same digits.

Input Format:
First line will contain the value of N.

Output Format:
First line will contain the next greater number with the same digits.

Boundary Conditions:
1 <= N <= 99999999999999

Example Input/Output 1:
Input:
12

Output:
21

Example Input/Output 2:
Input:
195

Output:
519

PROGRAM IN C:

#include <stdio.h>


int main(int argc, char** argv)

{

int i,l,j,k,m;

char s[1000];

scanf("%s",s);

l=strlen(s);

int a[l];

for(i=0;i<l;i++)

{

    a[i]=s[i]-'0';

}

for(i=l-1;i>0;i--)

{

    for(j=i-1;j>=0;j--)

    {

        if(a[i]>a[j])

        {

            a[i]=a[i]+a[j]-(a[j]=a[i]);

            for(k=j+1;k<l;k++)

            {

                for(m=k+1;m<l;m++)

                {

                    if(a[k]>a[m])

                    {

                        a[k]=a[k]+a[m]-(a[m]=a[k]);

                    }

                }

            }

            goto x;

        }

    }

}

x: for(i=0;i<l;i++)

{

   printf("%d",a[i]);

}



}

Comments

Popular Posts