Answer to Question #286555 in Java | JSP | JSF for Sukuu

Question #286555

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




1
Expert's answer
2022-01-11T06:05:08-0500
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 + " ");
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog