Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are N coins, each showing either heads or tails. We would like all the coi

ID: 3839642 • Letter: T

Question

There are N coins, each showing either heads or tails. We would like all the coins to show the same face. What is the minimum number of coins that must be reversed? Write a function: class solution {public int solution (int [] A);} that, given a zero-indexed array A consisting of N integers representing the coins, returns the minimum number of coins that must be reversed. Consecutive elements of array A represent consecutive coins and contain only a 0 (heads) or a 1 (tails) For example, given array A = [1, 0, 0, 1, 0, 0], there are four coins showing heads and two coins showing tails. The function should return 2, as after reversing two coins (in positions 0 and 3), all the coins will be showing the same face (heads). Assume that: N is an integer within the range [1..100]; each element of array A is an integer that can have one of the following values: 0, 1. In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Explanation / Answer

Solution.java:

import java.util.Scanner;

public class Solution {

            public static void main(String[] args) {

                        Scanner sc = new Scanner(System.in);

                        Solution S = new Solution();

                        System.out.println("Enter N value with in 1 to 100: ");

                        int N = sc.nextInt();

                        int A[] = new int[N];

                        System.out.println("Enter array values (0->head,1->tail): ");

                        for(int i=0;i<N;i++)

                                    A[i] = sc.nextInt();

                        int min = S.solution(A);

                        System.out.println(" Minimum number of coins that need to be reversed is: "+min);

            }

            public int solution(int[] A){

                        int headCount = 0,tailCount = 0;

                        for(int i=0;i<A.length;i++){

                                    if(A[i]==0)

                                                headCount++;

                                    else

                                                tailCount++;

                        }

                        int coin_change,min;

                        if(headCount<tailCount){

                                    min = headCount;

                                    coin_change = 0;

                        }

                        else{

                                    min = tailCount;

                                    coin_change = 1;

                        }

                        System.out.println("After reversing, array is:");

                        for(int i=0;i<A.length;i++){

                                    if(A[i]==coin_change){

                                                if(coin_change==1)

                                                            A[i] = 0;

                                                else

                                                            A[i] = 1;

                                    }

                                    System.out.print(A[i]+" ");

                        }

                        return min;

            }

}

Sample Input and Output:

Enter N value with in 1 to 100:

6

Enter array values (0->head,1->tail):

1 0 0 1 0 0

After reversing, array is:

0 0 0 0 0 0

Minimum number of coins that need to be reversed is: 2