what is the wrong in this code for reverse an array in java it is showing not work correctly for multiple test cases?

Viewed 37

I am trying to reverse an array in DSA cheat sheet but showing it is not working properly for multiple test cases.

    Scanner sc = new Scanner(System.in);
    int numberOfTestCases = sc.nextInt();
    while (numberOfTestCases > 0) {
        int arraySize = sc.nextInt();
        int[] arr = new int[arraySize];
        for (int i = 0; i < arraySize; i++) {
            arr[i] = sc.nextInt();
        }
        for (int i = 0; i < arraySize / 2; i++) {
            int temp = arr[i];
            arr[i] = arr[arraySize - i - 1];
            arr[arraySize - i - 1] = temp;
        }
        for (int i = 0; i < arraySize; i++) {
            System.out.print(arr[i] + " ");
        }
        numberOfTestCases--;
    }
0 Answers
Related