should we swap..because we should consider number of swaps needed
because the o/p says number of swaps only...
if i have a wrong conception plz help me out .... thanks a lot man for the
reply...
![:lol:](./images/smilies/icon_lol.gif)
still i get wa i dont no y plz help.....
Moderator: Board moderators
Code: Select all
........
printf("Optimal train swapping takes %d swaps\n", optimal);
..................
printf("Optimal train swapping takes 0 swaps\n");
Code: Select all
........
printf("Optimal train swapping takes %d swaps.\n", optimal);
..................
printf("Optimal train swapping takes 0 swaps.\n");
Try to mantain the condition's given in the problem.... "0<=L<=50"
Dont Open new Tread.... Just Search for the problem in board index....![]()
Code: Select all
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] s = null;
int size = Integer.parseInt(br.readLine());
int count = 0;
int i = 0;
int a[] = new int[size];
if(size > 0)
s = br.readLine().trim().split(" +");
while (i < size) {
a[i] = Integer.parseInt(s[i++]);
}
int j, temp;
for (i = 1; i < size; i++) {
j = i;
while (j > 0 && (a[j] < a[j - 1])) {
temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
count++;
j--;
}
}
bw.write("Optimal train swapping takes " + count + " swaps\n");
}
bw.close();
}
}