A six-side die is a small cube with a different number of pips on each face, ran
ID: 3914814 • Letter: A
Question
A six-side die is a small cube with a different number of pips on each face, ranging from 1 to 6. On any two opposite sides of the cube, the number of pips adds up to 7; that is, there are three pairs of opposite sides: 1and6, 2and5, 3 and4.
There are N dice lying on table, each showing the pips on its top face. In one move, you can take one die and rotate it to an adjacent face. for example, you can rotate a die that shows 1 so that it shows 2,3,4 or 5. However, it cannot show 6 in a bible move, because the faces with one pip and six pips visible are on opposite sides rather than adjacent.
you want to show the same number of pips on the top faces of all N dice. Given that each of the dice can be moved multiple times, count the minimum number of moves needed to get equal faces.
write a function
def solution(A)
that, given an array A consisting of N integers describing the number of pips(from1 to6) shown on each die’s top face, returns the minimum number of moves necessary for each die to show the same number of pips.
for example, given:
A = [1,2,3], the function should return 2, as you can pick the first two dice and rotate each of them in one move so that they all show three pips on the top face. Notice that you can also pick any other pair of dice in this case.
A = [1,1,6], the function should also return2.
Explanation / Answer
//Consider a function solution which accepts the array
//as input and the number of dices n , and return the minimum
//number of moves to be made as integer.
int solution(int a[], int n)
{
//initially we will find the number of dices having same number of pips
//as if such dices are more in number we will not need to move that dices
//giving us the minimum moves.
int i=0; //variable used to parse
int MAX = 0 ; //to keep record of maximum frequency of number of pips;
int POSI = 0; //to keep record of maximum number of dices having same number of pips faces;
//example if POSI= 2 i.e there are maximum number of dices with 2 pips.
int MOVES = 0; // to keeps track of number of moves to be made
//make a array which counts the frequency of number of pips on dices ,and
//keep the initial count of every as 0
// for example b[0]=3 means that three dices have one pip as face , b[4]=2 means 2 dices with 5 pips as face
int b[6]={0};
for(i=0;i<6;i++) // Loop to find frequency on number of pips
{
b[a[i]-1]=b[a[i]-1]+1; // as value of a[i] is between 1 to 6 , we increment that index value of array b;
// for example if a[i]=3 , then b[(3-1)]=b[2] is incremented , i.e b[2]+1 i.e b[2]=0+1=1;
// again if a[i] comes out to be 3 , the b[2] will again increment i.e b[2]=>b[2]+1= 1+1;
if( b[a[i]-1] > MAX) //compares the last maximum frequency value with calculated frequency
{
MAX = b[a[i]-1]; //MAX Keep record of maximum frequency of pips
POSI= a[i]; //Keep record of pips number having maximum frequency;
}
}
//end loop
//Now we know the Most of the dices have POSI number of pips , so we will not move them
// for example if array says {1,1,5,6,2,3,1}, so our will POSI=1 as we will need to move all other dices
//And we will compare all other dices that we will need to move , so we run a loop for all the n dices.
for(i=0;i<n;i++)
{
if( a[i] != POSI) //so if our dice pips is not equal to POSI number , then we need to move it to make it POSI number;
{
if( a[i]+POSI == 7) //if POSI=2 and a[i]=5 , then as 5 is opposite face of 2, and sum is 7
{ //so we will need two moves to get 5 converted to 2
MOVES = MOVES + 2; //so increment number of moves by 2
}
else // else any face can be converted into POSI in one move as given in question
{
MOVES = MOVES + 1; //if POSI=2 and a[i]=3 , then 3 can be get to 2 in one move.
}
}
} //end loop
return MOVES; //SO you will get the number of minimum moves to be made after end of loop;
}