-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNextLargestNumber.java
195 lines (181 loc) · 3.41 KB
/
NextLargestNumber.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* NEXT LARGEST NUMBER
*
* INPUT : 459863
* OUTPUT : 46589
*
* LOGIC :
*
* 4 5 9 8 6 3
*
* STEP 1 : INITIAL SETUP
*
* PLACEVALUE =10
* LEFT = (NUM / PLACEVALUE) % 10
* RIGHT = NUM % 10
*
*STEP 2 :
*
* PLACEVALUE RIGHT LEFT RIGHT<LEFT ACTIONS
*
* 10 3 6 CONTINUE RIGTH =LEFT, LEFT = (NUM/PLACEVALUE)%10
* 100 6 8 CONTINUE "
* 1000 8 9 CONTINUE "
* 10000 9 5 BREAK
*
* THEREFORE WE GOT PLACEVALUE =10000 AND LEFT = 5 , RIGHT =9
*
* STEP 3 :
*
* FROME STEP TWO WE GOT THAT 5 HAS TO BE REPLACED WITH THE VALUES
*
* (I.E) WE CAN REPLACE 5 WITH EITHER
*
* 9
* 8
* 6
* 3
*
* NOW PICK THE VALUE WHICH IS GREATER THAN 5
*
*PLACEVALUE CONDITION ACTION RIGHT
*
* 1 3>5 CONTINUE 6
* 10 6>5 BREAK
*
*
* STEP 4 : NOW SWAP THE VALUES
*
* FROM STEP2 AND STEP 3 WE GOT THAT WHICH VALUES HAVE TO BE REPLACED
*
* WE KNOW
*
* LEFT = 5 PLACEVALUE = 10000
* RIGHT = 6 PLACEVALUE = 10
*
* PERFORM SWAP
*
* 4 5 9 8 6
*
* 4 6 9 8 5
*
*
* STEP 5 : PERFORM PARTIAL REVERSING
*
* FINAL RESULT = 4 6 5 8 9
*
*
* STEP 6 : PRINT ALL THE POSSIBILITIES
*
*
*/
import java.util.*;
public class NextLargestNumber {
/**
* @param args
*/
static int smallest(int n)
{
int k=0;
int a[]=new int[10];
while(n!=0)
{
a[k]=n%10;
n=n/10;
k++;
}
for(int i=0;i<k;i++)
{
for(int j=i+1;j<k;j++)
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
//for(int i=0;i<k;i++)
//printf("%d ",a[i]);
int res=0;
int i=0;
while(k--!=0)
{
res = res*10+a[i];
i++;
}
return res;
}
static int swap(int number,int pv1,int pv2)
{
int val1 = (number/pv1)%10;
int val2 =(number/pv2)%10;
number = number - val1*pv1 + val2*pv1;
number = number - val2*pv2 + val1*pv2;
return number;
}
static int reverse(int number,int pv)
{
int rev = number/pv;
int num =number%pv;
int power =1;
while(num/power!=0)
{
int digit = (number/power)%10;
rev =rev*10+digit;
power=power*10;
}
return rev;
}
static int nextLargest(int number)
{
int placeValue1 =10;
int placeValue2 = 1;
int left = (number/placeValue1)%10;
int right = (number)%10;
//find the position which value has to be replaced
while(number/placeValue1 !=0)
{
if(left<right)
{
while(true)
{
right=(number/placeValue2)%10;
if(right>left)
break;
placeValue2=placeValue2*10;
}
number = swap(number,placeValue1,placeValue2);
number = reverse(number,placeValue1);
break;
}
placeValue1= placeValue1*10;
right = left;
left =(number/placeValue1)%10;
}
return number;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int number = input.nextInt();
//int b =input.nextInt();
// if(number>b)
// number=smallest(number);
number =nextLargest(number);
System.out.println("NEXT LARGEST = "+number);
int count =0;
int next=0,flag=0;
while(true)
{
next = nextLargest(number);
System.out.println(next);
if(next==number)
break;
count++;
number=next;
}
System.out.println("TOTAL POSSIBILITIES "+count);
}
}