Min operations required for all items in an array to have different parity from the next item in array

Viewed 17

I'm trying to figure out how to find the min operations required to fulfill the requirement that for a List of length n, for each index i < n, list[i] has a different parity than list[i+1]

This is my code thus far:



    public static int getMinimumOperations(List<Integer> items) {
    // Write your code here
        int minOp = 0; 
        for(int i = 0; i<items.size() - 1; i++) {
            int swit = 0;  
            while(items.get(i)%2 == items.get(i+1)%2) {
                System.out.println(items); 
                if (swit == 0) {
                    items.set(i, items.get(i).intValue()/2); 
                }
                else  
                    items.set(i+1, items.get(i+1).intValue()/2);
                minOp += 1; 
                swit = (swit == 0) ? 1 : 0;  
            }
        }
        return minOp; 
        
    }
 

But it's not getting the min operations, its just getting a possible combination of operations. Would this be better suited to do thru recursion? how might I work towards fixing it

0 Answers
Related