Untitled

Run Settings
LanguageJava
Language Version
Run Command
import java.util.*; public class MaxZeroesAfterOperations { public static void main(String[] args) { // Sample input array as given in the problem List<Integer> arr = new ArrayList<>(Arrays.asList(4, 3, 5,5,3)); int maxZeroes = findMaxZeroes(arr); System.out.println("Maximum number of zeroes after operations: " + maxZeroes); } // Function to find the maximum number of zeroes after performing the operations public static int findMaxZeroes(List<Integer> arr) { // Find the minimum value in the ArrayList int minValue = Collections.min(arr); // Perform 'minValue' operations on the entire ArrayList (since prefix can be the whole list) for (int i = 0; i < arr.size(); i++) { arr.set(i, arr.get(i) - minValue); } // Count the number of zeroes in the resulting ArrayList int zeroCount = 0; for (int value : arr) { if (value == 0) { zeroCount++; } } // Additional operations to decrease prefix by 1 until no more possible // Each time, we attempt to maximize zeroes while (Collections.max(arr) > 0) { for (int i = 0; i < arr.size(); i++) { if (arr.get(i) > 0) { arr.set(i, arr.get(i) - 1); } } // Recount zeroes int currentZeroCount = 0; for (int value : arr) { if (value == 0) { currentZeroCount++; } } zeroCount = Math.max(zeroCount, currentZeroCount); } return zeroCount; } }
Editor Settings
Theme
Key bindings
Full width
Lines