`{"id":"1685b135-2aa6-4949-94d6-785c4472b81d","name":"Next Greater Element I I I","description":"1. Given a positive number 'n' in form of String.\r\n2. Find the smallest number which has exactly the same digits existing in the number 'n' and is greater in value than 'n'.\r\n3. If no such positive number exists, return -1 as string.\r\n","inputFormat":"Input: n = 12\r\nInput: n = 21\r\n","outputFormat":"Output: 21\r\nOutput: -1","constraints":"1. 'n' is given in form of string.\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nstring nextGreaterElement(string str) {\n //write your code here\n \n}\n\nint main(){\n string num;\n cin>>num;\n string res = nextGreaterElement(num);\n\n cout<<res;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n \r\n //~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~~\r\n public static String nextGreaterElement(String str) {\r\n // write your code here\r\n }\r\n\r\n //~~~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n String num = scn.next();\r\n String res = nextGreaterElement(num);\r\n\r\n System.out.println(res);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"132","sampleOutput":"213","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"f27aacfc-ce1d-4303-92ea-ff8904107f93","name":"Next Greater Element I I I","slug":"next-greater-element-i-i-i","type":1}],"next":{"id":"7d586e74-ffec-4dc8-a48e-3ca1d3707491","name":"Next Greater Element I I I","type":3,"slug":"next-greater-element-i-i-i"},"prev":{"id":"52fe69a8-b35f-473d-9bf4-937a89f91ef1","name":"Faulty Keyboard MCQ","type":0,"slug":"faulty-keyboard-mcq"}}}`

# Next Greater Element I I I

1. Given a positive number 'n' in form of String. 2. Find the smallest number which has exactly the same digits existing in the number 'n' and is greater in value than 'n'. 3. If no such positive number exists, return -1 as string.

`{"id":"1685b135-2aa6-4949-94d6-785c4472b81d","name":"Next Greater Element I I I","description":"1. Given a positive number 'n' in form of String.\r\n2. Find the smallest number which has exactly the same digits existing in the number 'n' and is greater in value than 'n'.\r\n3. If no such positive number exists, return -1 as string.\r\n","inputFormat":"Input: n = 12\r\nInput: n = 21\r\n","outputFormat":"Output: 21\r\nOutput: -1","constraints":"1. 'n' is given in form of string.\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nstring nextGreaterElement(string str) {\n //write your code here\n \n}\n\nint main(){\n string num;\n cin>>num;\n string res = nextGreaterElement(num);\n\n cout<<res;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n \r\n //~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~~~~~\r\n public static String nextGreaterElement(String str) {\r\n // write your code here\r\n }\r\n\r\n //~~~~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n String num = scn.next();\r\n String res = nextGreaterElement(num);\r\n\r\n System.out.println(res);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"132","sampleOutput":"213","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"f27aacfc-ce1d-4303-92ea-ff8904107f93","name":"Next Greater Element I I I","slug":"next-greater-element-i-i-i","type":1}],"next":{"id":"7d586e74-ffec-4dc8-a48e-3ca1d3707491","name":"Next Greater Element I I I","type":3,"slug":"next-greater-element-i-i-i"},"prev":{"id":"52fe69a8-b35f-473d-9bf4-937a89f91ef1","name":"Faulty Keyboard MCQ","type":0,"slug":"faulty-keyboard-mcq"}}}`

Editor

# Next Greater Element I I I

easy

1. Given a positive number 'n' in form of String. 2. Find the smallest number which has exactly the same digits existing in the number 'n' and is greater in value than 'n'. 3. If no such positive number exists, return -1 as string.

## Constraints

1. 'n' is given in form of string.

## Format

### Input

Input: n = 12 Input: n = 21

### Output

Output: 21 Output: -1

## Example

Sample Input

`.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}132`

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}213`

Discussions

Show Discussion

Related Resources