In a school students stand in assembly. But we are arrange them height wise.
So print the indexes of wrong positions.
Sample Input:
You are given an array A having N integers which represents the assembly line.Each integer represents the height of a student.print the indexes of all the students who stand at wrong position in the line.
input; 9--------denotes N
11 8 4 26 7 9 2 16 1 ------denotes array A
output:0 1 3 4 6 8
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int []A = new int[N];
// input N and array
for (int i = 0; i < N; i++)
A[i] = in.nextInt();
// create another array that will be sorted
int[] copyA = Arrays.copyOf(A, N);
// sort the array (arrange height wise)
Arrays.sort(copyA);
for (int i = 0; i < N; i++) {
// check for wrong positions and print their indices
if (A[i] != copyA[i])
System.out.print(i + " ");
}
}
}
Comments
Leave a comment