-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOdd Even.java
More file actions
62 lines (42 loc) · 1.31 KB
/
Odd Even.java
File metadata and controls
62 lines (42 loc) · 1.31 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
// Akash Yadav
// @PD Tandon, MNNIT, Allahabad
// 28th July 18
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
class Ideone{
//https://practice.geeksforgeeks.org/problems/odd-to-even/0
public static Scanner scn = new Scanner(System.in);
public static void main (String[] args) throws java.lang.Exception{
int T = Integer.parseInt(scn.nextLine());
while(T-- > 0){
String str = scn.nextLine();
int N = str.length();
int odd = str.charAt(N-1) - '0';
int even = Integer.MAX_VALUE;
int idx = -1;
for(int i = 0; i < N; i++){
int n = str.charAt(i) - '0';
if(n%2 == 0){
even = n;
idx = i;
}
if(even <= odd)
break;
}
if(idx == -1){
System.out.println(str);
continue;
}
System.out.println(swap(str, idx, N-1));
}
}
public static char[] swap(String str, int i, int j){
char ch[] = str.toCharArray();
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
return ch;
}
}