Prime #### Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime #### Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

# Cognizant GenC Elevate Sample Coding Question 1

## Question 1

In this article, we will discuss about Coding Question with their solution in C++, java and Python.  In this Question , we have given a positive whole number n and we have to find the smallest number which has the very same digits as n and is greater than n. ### Question 1

Given a positive whole number n, find the smallest number which has the very same digits existing in the whole number n and is greater than n. In the event that no such certain number exists, return – 1.

Note that the returned number should fit in a 32-digit number, if there is a substantial answer however it doesn’t fit in a 32-bit number, return – 1.

Example 1:
Input: n = 12
Output: 21

Explanation:  Using the same digit as the number of permutations, the next greatest number for 12 is 21.

Example 2:
Input: n = 21
Output: -1

Explanation:  The returned integer does not fit in a 32-bit integer

`#include <iostream>#include <cstring>#include <algorithm>using namespace std;void swap(char *a, char *b){	char temp = *a;	*a = *b;	*b = temp;}void findNext(char number[], int n){	int i, j;	for (i = n-1; i > 0; i--)		if (number[i] > number[i-1])		break;	if (i==0)	{		cout << "Next number is not possible";		return;	}	int x = number[i-1], smallest = i;	for (j = i+1; j < n; j++)		if (number[j] > x && number[j] < number[smallest])			smallest = j;	swap(&number[smallest], &number[i-1]);	sort(number + i, number + n);	cout << number;	return;}int main(){	char digits[];  	cin>>digits	int n = strlen(digits);	findNext(digits, n);	return 0;}`
`import java.util.*;public class Main{	static void swap(char ar[], int i, int j)	{		char temp = ar[i];		ar[i] = ar[j];		ar[j] = temp;	}		static void findNext(char ar[], int n)	{		int i;				for (i = n - 1; i > 0; i--)		{			if (ar[i] > ar[i - 1]) {				break;			}		}				if (i == 0)		{			System.out.println("Not possible");		}		else		{			int x = ar[i - 1], min = i;									for (int j = i + 1; j < n; j++)			{				if (ar[j] > x && ar[j] < ar[min])				{					min = j;				}			}					swap(ar, i - 1, min);			Arrays.sort(ar, i, n);						for (i = 0; i < n; i++)				System.out.print(ar[i]);		}	}	public static void main(String[] args)	{	    Scanner sc = new Scanner(System.in);	    int n =sc.nextInt();	    String s = sc.next();		char digits[] = s.toCharArray();				findNext(digits, n);	}}`
`arr=[]def permute(s,ans):    n=len(s)    if (n==0):        arr.append(ans)        return    for i in range(n):        ch = s[i]        L= s[0:i]        R= s[i + 1:]        REM= L+ R        permute(REM,ans+ ch)ans= ""s = input()n=len(s)permute(s,ans)arr=list(set(arr))arr.sort()ind=arr.index(s)if(ind==len(arr)-1):    print(-1)else:    print(arr[ind+1])`