-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_15.java
More file actions
59 lines (48 loc) · 1.83 KB
/
Problem_15.java
File metadata and controls
59 lines (48 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package strings;
import java.util.*;
// Problem Title => Find next greater number with same set of digits. [Very Most IMP]
public class Problem_15 {
// Utility function to swap two digit
static void swap(char[] ar, int i, int j) {
char temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
// Given a number as a char arrays.array number[],
// this function finds the next greater number.
// It modifies the same arrays.array to store the result
static void findNext(char[] ar, int n) {
int i;
// I) Start from the right most digit and find the first digit that is smaller
// than the digit next to it.
for (i = n - 1; i > 0; i--) {
if (ar[i] > ar[i - 1])
break;
}
// If no such digit is found, then all digits are in descending order means
// there cannot be a greater number with same set of digits
if (i == 0)
System.out.println("Not possible");
else {
int x = ar[i - 1], min = i;
// II) Find the smallest digit on right side of (i-1)'th digit that is greater
// than number[i-1]
for (int j = i + 1; j < n; j++) {
if (ar[j] > x && ar[j] < ar[min])
min = j;
}
// III) Swap the above found the smallest digit with number[i-1]
swap(ar, i - 1, min);
// IV) Sort the digits after (i-1) in ascending order
Arrays.sort(ar, i, n);
System.out.print("Next number with same" + " set of digits is ");
for (i = 0; i < n; i++)
System.out.print(ar[i]);
}
}
public static void main(String[] args) {
char[] digits = { '5', '3', '4', '9', '7', '6' };
int n = digits.length;
findNext(digits, n);
}
}