1 / 1
Aug 2023

I still try to solve this task (this task6).I’ve corrected my previous code

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();

        for (int testCase = 0; testCase < t; testCase++) {
            int n = scanner.nextInt();
            int[] heights = new int[n];

            for (int i = 0; i < n; i++) {
                heights[i] = scanner.nextInt();
            }

            int maxCount = 0;
            int maxHeight = 0;
            int countZeros = 0;
            int minHeight = heights[0];

            for (int height : heights) {
                if (height < minHeight) {
                    minHeight = height;
                }
                if (height == 0) {
                    countZeros++;
                }
                if (height > maxHeight) {
                    maxHeight = height;
                    maxCount = 1;
                } else if (height == maxHeight) {
                    maxCount++;
                }
            }

            if (heights.length == 1 && maxHeight != 0) {
                System.out.println(1);
                continue;
            }

            if (maxHeight == 0 || maxCount % 2 == 0) {
                System.out.println(0);
                continue;
            }

            if (maxCount == 1 && countZeros > 0) {
                System.out.println(1);
                continue;
            }

            if (maxCount == 1 && n % 2 != 0) {
                System.out.println(minHeight);
                continue;
            }

            if (maxCount > 1 && maxHeight == 1) {
                System.out.println(maxCount);
            } else if (maxCount > 1) {
                System.out.println(maxCount * maxHeight);
            } else {
                System.out.println(1);
            }
        }

        scanner.close();
    }
}

He passes my tests such as:
#1
Input:
1
6
4 4 4 4 4 2
Output:
20

#2
Input:
1
5
1 0 1 0 1
Output:
3

#3
Input:
1
4
10 4 4 4
Output:
1

#4
Input:
1
3
3 3 3
Output:
9

#5
Input:
1
4
10 10 10 10
Output:
0

#6
Input:
1
3
4 3 3
Output:
3

In test 1 we get 20 because we can take the first move away from all 5 of the highest fours (1,2,3,4) and so we multiply 4 by 5.

but I still don’t understand what’s wrong and have WA.((