You are given a binary array (array consists of and ) that contains elements. You can perform the following operation as many time as you like:
- Choose any index and if it is currently 0, then convert it to 1 for C01 coins.
- Choose any index 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 , .
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[] = { 1, 1 };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