The minimum cost

You are given a binary array (array consists of 0s and 1sA that contains N elements. You can perform the following operation as many time as you like:

  1.  Choose any index  1iN and if it is currently 0, then convert it to 1 for C01 coins.
  2.  Choose any index  1iN and if it is currently 1, then convert it to 0 for C10 coins.

Your task is to transform the given array into a special array that for every index 1i<N, AiAi+1=1.

Here, 

 denotes XOR. 

Example:

Input:  n=2, arr[] = { 1, 1 }, c01=1, c10=1
Output: 1

Approach

Java

public class MinimumCost {
    public static void main(String args[]) {
        int n = 2;
        int arr[] = { 11 };
        long c01 = 1;
        long c10 = 1;
        int[][] tab = new int[2][2];
        for (int i = 0; i < n; i++) {
            int A_i = arr[i];
            tab[i % 2][A_i] = tab[i % 2][A_i] + 1;
        }
        System.out.println(Math.min(tab[0][0] * c01 + tab[1][1] * c10, tab[0][1] * c10 + tab[1][0] * c01));
    }
}


No comments:

Post a Comment