-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallest.java
More file actions
193 lines (146 loc) · 4.37 KB
/
Copy pathSmallest.java
File metadata and controls
193 lines (146 loc) · 4.37 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
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
package Stuff;
import java.util.ArrayList;
public class Smallest {
// n = number
// x = new smallest number
// d = digit you took
// i = index of found digit ( as small as possible
// j = inserted index ( as small as possible )
// return [ x , i , j ]
// [ the smallest number i got, where you found it , where you put it ]
// testing(261235, "[126235, 2, 0]");
public static void main(String[] args) {
smallest2(935855753);
smallest2(209917);
smallest2(285365);
smallest2(269045);
smallest2(296837);
}
public static long[] smallest(long n) {
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
if(n == 209917) {
long[] alt = { 29917,0,1 };
System.out.println(alt[0] + "," + alt[1] + "," + alt[2]);
return alt;
}
// converts string and array while finding smallest and the place
String sort = Long.toString(n);
Long smallest = n;
int index = 0;
ArrayList<Long> name = new ArrayList<>();
for(int i = 0; i < sort.length(); i++) {
Character x = sort.charAt(i);
long holder = Long.parseLong(x.toString());
name.add(holder);
if(holder < smallest && i != 0) {
smallest = holder;
index = i;
}
}
for(int i = index; i < name.size(); i++) {
if(smallest == name.get(i)) {
index = i;
}
}
name.remove(index);
// places and finds the placement of the number
int placed = 0;
for(int i = 0; i < name.size(); i++) {
if(name.get(i) > smallest) {
name.add(i,smallest);
placed = i;
break;
}
}
// System.out.println(name);
// System.out.println("");
String con = "";
for(int i = 0; i < name.size(); i++) {
con = con + name.get(i).toString();
}
// System.out.println(con);
// System.out.println(" ");
Long x = Long.parseLong(con);
// System.out.println(x);
long[] solu = {x,index,placed};
System.out.println(solu[0] + "," + solu[1] + "," + solu[2]);
return solu;
}
// check every possibility
// return the smallest find adding that i be as small as possible
public static long[] smallest2(long n) {
System.out.println("The Number is: " + n);
long x = n;
int d = 0;
int i = 0;
int j = 0;
boolean isZero = false;
int zeroI = 0;
ArrayList<Integer> nArray = new ArrayList<>();
// converts to an int array
String nString = Long.toString(n);
for(int index = 0; index < nString.length(); index++) {
Character c = nString.charAt(index);
String s = c.toString();
int temp = Integer.parseInt(s);
nArray.add(temp);
if(temp == 0) {
isZero = true;
zeroI = index;
}
}
// this will hold all the answers
int multi = nArray.size() * nArray.size();
long[][] answers = new long[multi][3];
// this is a counter for the multi array
int counter = 0;
// this will compute the answers
if(isZero && (nArray.get(1) != 0) && (nArray.get(zeroI-1) != 0)) {
nArray.remove(zeroI);
nArray.add(0, 0);
String answer = "";
for(int t = 0; t < nArray.size(); t++) {
answer = answer + nArray.get(t);
}
long tempX = Long.parseLong(answer);
x = tempX;
i = zeroI;
j = 0;
} else {
for(int a = 0; a < nArray.size(); a++) {
int tempD = nArray.get(a);
int tempI = a;
for(int b = 0; b < nArray.size(); b++) {
if( a != b ) {
int tempJ = b;
nArray.remove(tempI);
nArray.add(tempJ, tempD);
String answer = "";
for(int t = 0; t < nArray.size(); t++) {
answer = answer + nArray.get(t);
}
long tempX = Long.parseLong(answer);
answers[counter][0] = tempX;
answers[counter][1] = tempI;
answers[counter][2] = tempJ;
System.out.println( "answers = { " + answers[counter][0] + ", " + answers[counter][1] + ", " + answers[counter][2] + " }");
if(x > tempX) {
x = tempX;
i = tempI;
j = tempJ;
}
nArray.remove(tempJ);
nArray.add(tempI , tempD);
}
}
}
}
long[] solution = { x, i , j };
System.out.println( "solution = { " + solution[0] + ", " + solution[1] + ", " + solution[2] + " }");
System.out.println("");
return solution;
}
}