I/P : "ABC","DEF","GHI","JKL","MNO" (1,2,3,4,..N)
O/P:"ABC","MNO","DEF","JKL","GHI" (1,N-1,2,N-2.....)
Logic :
public static void main(String[] args) {
String[] str = { "ABC", "DEF", "GHI", "JKL", "MNO" };
int ln = str.length - 1;
for (int i = 0, j = ln; i <= j; i++, j--) {
if (i == j) {
System.out.println(str[i]);
} else {
System.out.println(str[i]);
System.out.println(str[j]);
}
}
}
O/P:"ABC","MNO","DEF","JKL","GHI" (1,N-1,2,N-2.....)
Logic :
public static void main(String[] args) {
String[] str = { "ABC", "DEF", "GHI", "JKL", "MNO" };
int ln = str.length - 1;
for (int i = 0, j = ln; i <= j; i++, j--) {
if (i == j) {
System.out.println(str[i]);
} else {
System.out.println(str[i]);
System.out.println(str[j]);
}
}
}