Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

556. Next Greater Element III

Medium34.3% Acceptance
MathTwo PointersString
Asked by:
D
DoorDash
ProblemSolutions (12)VideosCompanies (11)Notes

Problem Statement

Given a positive integer n, find the smallest integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive integer exists, return -1.

Note that the returned integer should fit in 32-bit integer, if there is a valid answer but it does not fit in 32-bit integer, return -1.

Example 1:

Input: n = 12
Output: 21

Example 2:

Input: n = 21
Output: -1

Constraints:

  • 1 <= n <= 231 - 1
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
T
TikTok
F
Facebook
A
Amazon
A
Adobe
+6

Approach

The key idea behind #556 Next Greater Element III is recognizing that the problem is essentially asking for the next lexicographically greater permutation of the digits of the given integer. Instead of brute‑forcing all permutations, we can use a structured approach similar to the classic next_permutation algorithm.

Start by converting the number into a digit sequence (string or array). Scan from right to left to find the first position where the digits stop decreasing. This position acts as the pivot. Then locate the smallest digit on the right side that is larger than this pivot and swap them. Finally, reverse the suffix to make it the smallest possible order.

After reconstructing the integer, ensure the result fits within the 32-bit signed integer range. If no pivot exists, it means the digits are already in descending order and no greater permutation is possible.

This approach efficiently computes the answer in linear time relative to the number of digits.

Complexity

ApproachTime ComplexitySpace Complexity
Next Permutation on DigitsO(d)O(d)

Video Solution Available

CrioDo

View all video solutions

Solutions (12)

Approach 1: Next Permutation

This approach uses the concept of finding the next permutation of the digits of the number. We traverse the digits from right to left to find the first digit that is smaller than the digit next to it. Once found, we swap it with the smallest larger digit on its right and then reverse the sequence following the swapped digit.

Time Complexity: O(n log n) (due to sorting in worst case)
Space Complexity: O(1) (in-place swap and conversion)

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5int compare(const void* a, const
    
    





Explanation

The solution involves converting the integer to a string and manipulating the characters to find the next permutation. We ensure to swap the digits right before reversing the tail end. We also handle possible overflow scenarios by checking if the result exceeds the 32-bit limit.

Approach 2: Mathematical Permutation Logic

This approach entails understanding mathematical permutation generation principles. First, identify the point where the digits stop increasing when moving left-to-right, then swap it. Finally, regenerate that segment to form the smallest sequential increase.

Time Complexity: O(n^2) (as it checks for minimum swap position for small digits segment)
Space Complexity: O(n) (array storage)

CC++JavaPythonC#JavaScript
1#








Video Solutions

Watch expert explanations and walkthroughs

How many LeetCode problems should you solve? #leetcode #techinterview #developer #softwareengineer

CrioDo
0:58304,599 views

Asked By Companies

11 companies
D
DoorDash
T
TikTok
F
Facebook
A
Amazon
A
Adobe
G
Google
G
Goldman Sachs
M
Microsoft
A
Apple
D
DE Shaw
B
Bloomberg

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Related Topics

MathTwo PointersString

Problem Stats

Acceptance Rate34.3%
DifficultyMedium
Companies11

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Next Greater Element III asked in FAANG interviews?

Yes, variations of permutation and digit-rearrangement problems appear in many technical interviews, including FAANG companies. This question tests understanding of permutations, greedy logic, and careful edge-case handling.

What is the optimal approach for Next Greater Element III?

The optimal approach uses the next permutation technique on the digits of the number. By identifying a pivot, swapping it with the next larger digit on the right, and reversing the suffix, we can efficiently generate the smallest greater number using the same digits.

Why is the next permutation concept used in Next Greater Element III?

The problem asks for the smallest number greater than the given one using the same digits. This exactly matches the definition of the next lexicographical permutation, making the next permutation algorithm a natural and efficient solution.

What data structure is commonly used to solve Next Greater Element III?

Most solutions convert the integer into a string or an array of digits. This allows easy traversal, swapping, and reversing operations needed for implementing the next permutation logic.

void
*
b
)
{
6
return
(
*
(
char
*
)
a
-
*
(
char
*
)
b
)
;
7
}
8
9
int
nextGreaterElement
(
int
n
)
{
10
char
digits
[
11
]
;
11
sprintf
(
digits
,
"%d"
,
n
)
;
12
int
len
=
strlen
(
digits
)
;
13
int
i
,
j
;
14
15
for
(
i
=
len
-
2
;
i
>=
0
;
i
--
)
{
16
if
(
digits
[
i
]
<
digits
[
i
+
1
]
)
{
17
break
;
18
}
19
}
20
21
if
(
i
<
0
)
return
-
1
;
22
23
for
(
j
=
len
-
1
;
j
>
i
;
j
--
)
{
24
if
(
digits
[
j
]
>
digits
[
i
]
)
{
25
break
;
26
}
27
}
28
29
char
temp
=
digits
[
i
]
;
30
digits
[
i
]
=
digits
[
j
]
;
31
digits
[
j
]
=
temp
;
32
33
qsort
(
digits
+
i
+
1
,
len
-
i
-
1
,
sizeof
(
char
)
,
compare
)
;
34
35
long
long
newNumber
=
atoll
(
digits
)
;
36
return
(
newNumber
>
INT_MAX
)
?
-
1
:
(
int
)
newNumber
;
37
}
38
39
int
main
(
)
{
40
int
n
=
12
;
41
printf
(
"%d\n"
,
nextGreaterElement
(
n
)
)
;
42
return
0
;
43
}
include
<stdio.h>
2
3
int
calculateMinimumLargerSwap
(
int
*
digits
,
int
start
,
int
indexToSwap
)
{
4
int
minIndex
=
-
1
;
5
for
(
int
i
=
start
;
i
>=
0
;
--
i
)
{
6
if
(
digits
[
i
]
>
digits
[
indexToSwap
]
)
{
7
if
(
minIndex
==
-
1
||
digits
[
i
]
<
digits
[
minIndex
]
)
{
8
minIndex
=
i
;
9
}
10
}
11
}
12
return
minIndex
;
13
}
14
15
void
reverse
(
int
*
digits
,
int
start
,
int
end
)
{
16
while
(
start
<
end
)
{
17
int
temp
=
digits
[
start
]
;
18
digits
[
start
]
=
digits
[
end
]
;
19
digits
[
end
]
=
temp
;
20
start
++
;
21
end
--
;
22
}
23
}
24
25
int
nextGreaterElement
(
int
n
)
{
26
int
digits
[
10
]
,
count
=
0
;
27
while
(
n
)
{
28
digits
[
count
++
]
=
n
%
10
;
29
n
/=
10
;
30
}
31
32
int
i
=
0
;
33
for
(
i
=
0
;
i
<
count
-
1
&&
digits
[
i
]
>=
digits
[
i
+
1
]
;
i
++
)
;
34
if
(
i
==
count
-
1
)
return
-
1
;
35
36
int
j
=
calculateMinimumLargerSwap
(
digits
,
i
-
1
,
i
)
;
37
38
int
temp
=
digits
[
i
]
;
39
digits
[
i
]
=
digits
[
j
]
;
40
digits
[
j
]
=
temp
;
41
42
reverse
(
digits
,
0
,
i
-
1
)
;
43
44
long
long
result
=
0
;
45
for
(
int
k
=
count
-
1
;
k
>=
0
;
k
--
)
result
=
result
*
10
+
digits
[
k
]
;
46
return
(
result
>
INT_MAX
)
?
-
1
:
(
int
)
result
;
47
}
48
49
int
main
(
)
{
50
int
n
=
12
;
51
printf
(
"%d\n"
,
nextGreaterElement
(
n
)
)
;
52
return
0
;
53
}

Explanation

This method parses digits from the number to locate the next higher permutation swap, initiating a reverse rotation to create the smallest increment. It precisely tracks indices, ensuring accurate results.

Rotate ArrayMedium
Happy NumberEasy
Sort Transformed ArrayMedium
Sum of Square NumbersMedium
More similar problems