Breaking

Thursday, 11 May 2017

Find next greater number with same set of digits in Java


Find next greater number with same set of digits in Java


 Given a number, find next greater number with the same set of digits as original number.
For example,
if input number is 1234, then output should be 1243
if input number is 1342, then output should be 1423
if input number is 534976, then output should be 536479
if input number is 4321, then output should be next greater number not possible. (As input number is the greatest number that can be formed with the digits.)


Code:
#include<stdio.h>
#include <stdlib.h>
int main()
{
    int a,n,arr1[50],i=0,j=0;
    scanf("%d",&a);
    while(a>0)
    {
        arr1[i++]=a%10;
        a/=10;
    }
    n=i;
    int right=-1,left=-1,arr[n];
    for(i=n-1;i>=0;i--)
        arr[j++]=arr1[i];
    for(i=n-1;i>=1;i--)
    {
        for(j=i-1;j>=0;j--)
        {
            if(arr[i]>arr[j])
            {
                if(right==-1)
                {
                    right=i;
                    left=j;
                }
                else if((left<j)&&(right>i))
                {
                    right=i;
                    left=j;
                }
            }
        }
    }
    int t;
    if(right!=-1)
    {
        t=arr[left];
        arr[left]=arr[right];
        arr[right]=t;
    }
    for(i=left+1;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(arr[i]>arr[j])
            {
                t=arr[i];
                arr[i]=arr[j];
                arr[j]=t;
            }
        }
    }
    printf("\n");
    for(i=0;i<n;i++)
        printf("%d",arr[i]);
}

EASY one:

#include <iostream>
#include<algorithm>  
using namespace std;
int main(int argc, char** argv)
{
    string s;
    std::cin>>s;
    std::next_permutation(s.begin(),s.end());
    std::cout<<s;
    

}

For more :
Refer here......

No comments:

Post a Comment

Like