Infosys | OA | Find minimum possible sum of k faces of n dice in m turns
Anonymous User
721

I was asked this in a technical coding test and I'm still struggling to come up with a solution for this. I think this is a variation of a Recursion / Dynamic Programming question but I'm not able to understand how to approach this problem.

The problem goes as follows: Suppose there are n dice with k faces and they are thrown for m turns. The number of dice is given as input in the first line. The number of turns is given as input in the second line and the number which appears at the top of each dice is recorded at every turn and given as input in the third line.

Constraints:

Note that the order that the dice are rolled in isn't guaranteed for every turn. For e.g. dice 1 may appear at position 1 in the first turn but also may appear at position 3 in the second turn.
k may vary for each dice.

Given the m*n matrix, write a program to find the minimum possible sum of the faces of the dice.

These examples were given.

Example 1:
m=1, n=2
2 dice and 1 turn

2
1
2 3

This example shows that 2 dice were rolled for 1 turn. The top face of the first die is taken as 2, the top face of the second die is taken as 3. The output for this is 5 since 2+3 = 5.

Example 2: 4 dice and 2 turns
m=2, n=4

4
2
2 1 3 1
2 3 1 3

This example shows that 4 dice were rolled for 2 turns. For the first turn, the top faces recorded are 2, 1, 3, 1. For the second turn, the top faces recorded are 2, 3, 1, 3. Minimum Possible Sum can be 2 + 3 + 3 + 3 = 11 (assuming die1's face is taken as 2, die2's face is taken as 3, die3's face is taken as 3, die4's face is taken as 3) but if we assume that the die at position 4 in the 1st row is the same as the die at position 3 in the 2nd row (since the top faces are in random order), then minimum sum is 2 + 3 + 3 + 1 = 9.

Hope somebody can help with this.

Comments (1)