Open In App

Check if max sum of visible faces of N dice is at least X or not

Improve
Improve
Like Article
Like
Save
Share
Report

You are given N and an integer X, where N represents the number of dice. You have to arrange those dices in such a way that the visible number of faces are as minimum as possible and the sum of visible faces is as maximum as possible. The task is to check if the sum of values on the visible faces is at least X or not.

Examples:

Input: N = 1, X = 18
Output: YES
Explanation:

Graphical explanation of input case 1

A maximum of 1 face can be made invisible, Which is having 1 on it and visible faces are 2, 3, 4, 5, 6, Which gives the maximum sum as 20.

20 >= 18, Therefore output is YES.   

Input: N = 2, X = 40
Output: NO
Explanation:

Graphical explanation of 2nd input case

Both dices are attached with the faces having number 2 on it and 1 of both dices is at base. Only this arrangement is possible such that visible faces are as minimum as possible and sum should be max at visible faces.
Maximum possible sum gained by both dice is equal to sum of visible faces, Which are 3, 4, 5, 6. Total max possible sum = (3+4+5+6)+(3+4+5+6)=36. Which is not greater than or equal to 40. Therefore, output is NO.

Approach: The problem is observation based and can be solved by using the Greedy Approach for getting the maximum possible sum. For more clarification follow the below illustration.

Illustration:

The problem can be solved by Greedy approach for getting maximum possible sum. We will see some cases below and will try to make a mathematical formula for calculating sum using some observations. Let us see some cases below:

  • Structure, When N = 1:

Structure of 1 dice

According to greedy approach for making maximum possible sum, We have to put 1 mark of dice on base surface, So that the visible faces will be 2, 3, 4, 5, 6. Maximum possible sum in this case will be 20.

  • Structure, When N = 2:

Structure of 2 dices

In structure of 2 dice, For making visible faces minimum as possible, We have to merge both dice, which will decrement a visible face of both dices and a face will be put as base. So that total 2 faces will be invisible for each dice. For maximizing sum, We have to choose those 2 invisible faces having minimum values, Which are 1 and 2. Visible faces of both dice will be 3, 4, 5 and 6. Total sum in this case = {3 + 4 + 5 + 6} + {3+ 4 + 5 + 6} = 18 + 18 = 36.

  • Structure, When N = 3:

Structure of 3 dices

 For maximizing sum and minimizing visible number of faces, It is optimal to make three invisible faces(1, 2, 3) of one dice and two (1, 2) for two dices. For maximum sum we have to make invisible faces of lowest values first. Let us see the sum gained in this structure.

Format of Explanation is as:

Dice number: {Total faces of dice} – {Invisible faces} = {Visible faces} = Sum of visible faces
Dice 1: {1, 2, 3, 4, 5, 6} – {1, 2, 3}={4, 5, 6}=4 + 5 + 6 = 15
Dice 2: {1, 2, 3, 4, 5, 6} – {1, 2}={3, 4, 5, 6}=3 + 4 + 5 + 6 = 18
Dice 3: {1, 2, 3, 4, 5, 6} – {1, 2}={3, 4, 5, 6}=3 + 4 + 5 + 6 = 18

Total maximum sum=18 + 18 + 15 = 51. It can be verified that it is the maximum sum such that visible faces should be as minimum as possible.

  • Structure, When N = 4:

Struture of 4 dices

For minimizing visible faces, We can make at most 3 invisible faces for each dice, Same can be seen in the picture above. For maximizing sum, 1, 2 and 3 must be as invisible faces and 4, 5 and 6 should be visible faces of each dice. In this case, Maximum sum will be = {4, 5, 6} + {4, 5, 6} + {4, 5, 6} + {4, 5, 6} = 15 + 15 + 15 + 15 = 60, Which is maximum possible.

  • Structure, When N = 7:

Structure of 7 dices

Three dices at base level will have visible faces as 5 and 6 and one dice at base level will have 4, 5, 6 as visible faces. The arrangement of upper three dices 5th, 6th and 7th will follow the case of N = 3.

Level 1:
Dice 1: {1, 2, 3, 4, 5, 6} – {1, 2, 3, 4}={5, 6} = 5 + 6 = 11
Dice 2: {1, 2, 3, 4, 5, 6} – {1, 2, 3, 4} = {5, 6} = 5 + 6 = 11
Dice 3: {1, 2, 3, 4, 5, 6} – {1, 2, 3, 4} = {5, 6} = 5 + 6 = 11
Dice 4: {1, 2, 3, 4, 5, 6} – {1, 2, 3} = {4, 5, 6} = 4 + 5 + 6 = 15

Level 2:
Dice 5: {1, 2, 3, 4, 5, 6} – {1, 2} = {4, 5, 6} = 4 + 5 + 6 = 15
Dice 6: {1, 2, 3, 4, 5, 6} – {1, 2} = {3, 4, 5, 6} = 3+ 4 + 5 + 6 = 18
Dice 6: {1, 2, 3, 4, 5, 6} – {1, 2} = {3, 4, 5, 6} = 3 + 4 + 5 + 6 = 18
total maximum possible sum 11+ 11 + 11 + 15 + 15 + 18 + 18 = 99

  • Structure, When N = 8:

Structure of 8 dices

All the 4 dices present at level 1, Will have only 2 visible faces 5 and 6. While all the 4 dices at level 2 will have three visible faces 4, 5 and 6. 

Level 1:
Dice 1: {1, 2, 3, 4, 5, 6} – {1, 2, 3, 4} = {5, 6} = 5 + 6 = 11
Dice 2: {1, 2, 3, 4, 5, 6} – {1, 2, 3, 4} = {5, 6} = 5 + 6 = 11
Dice 3: {1, 2, 3, 4, 5, 6} – {1, 2, 3, 4} = {5, 6} = 5 + 6 = 11 
Dice 4: {1, 2, 3, 4, 5, 6} – {1, 2, 3, 4} = {5, 6} = 5 + 6 = 11

Level 2:
Dice 5: {1, 2, 3, 4, 5, 6} – {1, 2, 3} = {4, 5, 6} = 4 + 5 + 6 = 15
Dice 6: {1, 2, 3, 4, 5, 6} – {1, 2, 3} = {4, 5, 6} = 4 + 5 +6 = 15
Dice 7: {1, 2, 3, 4, 5, 6} – {1, 2, 3} = {4, 5, 6} = 4 + 5 + 6 = 15
Dice 8: {1, 2, 3, 4, 5, 6} – {1, 2, 3} = {4, 5, 6} = 4 + 5 + 6 = 15
Total maximum sum: 11 + 11 + 11 + 11 + 15+ 15 + 15 + 15 = 104

Observations: From the above observations, We can conclude some mathematical conditions for calculating maximum sum on N dices:

  •  If N = 1, 2, 3, 4 then by following greedy approach maximum sum will be 20, 36, 51, 60 respectively
  • Initialize ans = (N-4)*11
    • If (N%4==0), ans += 15*4
    • else If (N%4==1), ans+=15*3+20
    • else If (N%4==2), ans+=15*2+18*2
    • else, ans+=15*2+18*2

Steps were taken to solve the problem:

  • Initialize the variable sum to hold the maximum sum. 
  • If N = 1, 2, 3, 4 then by following greedy approach maximum sum will be 20, 36, 51, 60 respectively. 
  • Else initialize ans = (N-4)*11
    • If n is divisible by 4 add =15*4 to ans.
    • Else If N%4 = 1 add15*3+20 to ans.
    • Otherwise, add 15*2+18*2 to ans.
  • If ans is greater than equal to X, Then print YES else NO.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate maximum sum
// formed from the faces of dices
void maximumSum(long n, long X)
{
  long ans = 0;
 
  // Base cases
  if (n == 1) {
    ans = 20;
  }
  else if (n == 2) {
    ans = 36;
  }
  else if (n == 3) {
    ans = 51;
  }
  else if (n == 4) {
    ans = 60;
  }
 
  // If number of dices is more
  // than N
  else {
 
    ans = (n - 4) * 11;
 
    if (n % 4 == 0) {
      ans += 15 * 4;
    }
    else if (n % 4 == 1) {
      ans += 15 * 3 + 20;
    }
    else {
      ans += 15 * 2 + 18 * 2;
    }
  }
 
  // Print YES if sum greater than X,
  // else NO
  cout << (ans >= X ? "YES" : "NO") << endl;
}
 
 
// main function
int main() {
 
  long long N = 7, X = 50;
 
  // Function call
  maximumSum(N, X);
 
  return 0;
}
 
// This code is contributed by rahulbhardwaj0711.


Java




// Java code to implement the approach
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Driver Code
    public static void main(String[] args)
        throws java.lang.Exception
    {
        long N = 7, X = 50;
 
        // Function call
        maximumSum(N, X);
    }
 
    // Function to calculate maximum sum
    // formed from the faces of dices
    public static void maximumSum(long n, long X)
    {
        long ans = 0;
 
        // Base cases
        if (n == 1) {
 
            ans = 20;
        }
        else if (n == 2) {
            ans = 36;
        }
        else if (n == 3) {
            ans = 51;
        }
        else if (n == 4) {
            ans = 60;
        }
 
        // If number of dices is more
        // than N
        else {
 
            ans = (n - 4) * 11;
 
            if (n % 4 == 0) {
                ans += 15 * 4;
            }
            else if (n % 4 == 1) {
                ans += 15 * 3 + 20;
            }
            else {
                ans += 15 * 2 + 18 * 2;
            }
        }
 
        // Print YES if sum greater than X,
        // else NO
        System.out.println(ans >= X ? "YES" : "NO");
    }
}


Python3




# Python code to implement the approach
 
# Function to calculate maximum sum
# formed from the faces of dices
 
 
def maximumSum(n, X):
    ans = 0
    # Base cases
    if (n == 1):
        ans = 20
 
    elif (n == 2):
        ans = 36
 
    elif (n == 3):
        ans = 51
 
    elif (n == 4):
        ans = 60
 
    # If number of dices is more
    # than N
    else:
        ans = (n - 4) * 11
 
    if (n % 4 == 0):
        ans += 15 * 4
 
    elif (n % 4 == 1):
        ans += 15 * 3 + 20
 
    else:
        ans += 15 * 2 + 18 * 2
 
    # Print YES if sum greater than X,
    # else NO
    # print(ans >= X ? "YES" : "NO");
    if(ans >= X):
        print("YES")
    else:
        print("NO")
 
 
# Driver Code
N = 7
X = 50
# Function call
maximumSum(N, X)


C#




// C# code to implement the approach
using System;
 
public class GFG {
 
    // Function to calculate maximum sum
    // formed from the faces of dices
    public static void maximumSum(long n, long X)
    {
        long ans = 0;
 
        // Base cases
        if (n == 1) {
 
            ans = 20;
        }
        else if (n == 2) {
            ans = 36;
        }
        else if (n == 3) {
            ans = 51;
        }
        else if (n == 4) {
            ans = 60;
        }
 
        // If number of dices is more
        // than N
        else {
 
            ans = (n - 4) * 11;
 
            if (n % 4 == 0) {
                ans += 15 * 4;
            }
            else if (n % 4 == 1) {
                ans += 15 * 3 + 20;
            }
            else {
                ans += 15 * 2 + 18 * 2;
            }
        }
 
        // Print YES if sum greater than X,
        // else NO
        Console.WriteLine(ans >= X ? "YES" : "NO");
    }
 
    static public void Main()
    {
 
        // Code
        long N = 7, X = 50;
 
        // Function call
        maximumSum(N, X);
    }
}
 
// This code is contributed by lokeshmvs21.


Javascript




  // JS code to implement the approach
   
  // Function to calculate maximum sum
  // formed from the faces of dices
  function maximumSum(n, X) {
    let ans = 0;
 
    // Base cases
    if (n == 1) {
      ans = 20;
    }
    else if (n == 2) {
      ans = 36;
    }
    else if (n == 3) {
      ans = 51;
    }
    else if (n == 4) {
      ans = 60;
    }
 
    // If number of dices is more
    // than N
    else {
 
      ans = (n - 4) * 11;
 
      if (n % 4 == 0) {
        ans += 15 * 4;
      }
      else if (n % 4 == 1) {
        ans += 15 * 3 + 20;
      }
      else {
        ans += 15 * 2 + 18 * 2;
      }
    }
 
    // Print YES if sum greater than X,
    // else NO
    let res = (ans >= X ? "YES" : "NO");
    console.log(res)
  }
 
  // main function
  let N = 7, X = 50;
 
  // Function call
  maximumSum(N, X);
 
// This code is contributed by Potta Lokesh


Output

YES

Time Complexity: O(1)
Auxiliary Space: O(1)

Related Articles:



Last Updated : 07 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads