Related Articles

# The dice problem

• Last Updated : 05 Aug, 2021

You are given a cubic dice with 6 faces. All the individual faces have a number printed on them. The numbers are in the range of 1 to 6, like any ordinary dice. You will be provided with a face of this cube, your task is to guess the number on the opposite face of the cube.

Examples:

Input: N = 2
Output: 5
Explanation:
For dice facing number 5 opposite face will have the number 2.

Input: N = 6
Output: 1

Naive Approach: In a normal 6 faced dice, 1 is opposite to 6, 2 is opposite to 5, and 3 is opposite to 4. Hence a normal if-else-if block can be placed

Approach: The idea is based on the observation that the sum of two opposite sides of a cubical dice is equal to 7. So, just subtract the given N from 7 and print the answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include``using` `namespace` `std;` `// Function to find number``// written on the``// opposite face of the dice``int` `oppositeFaceOfDice(``int` `N)``{``    ``// Stores number on opposite face``    ``// of dice``    ``int` `ans = 7 - N;` `    ``// Print the answer``    ``cout << ans;``}` `// Driver Code``int` `main()``{``    ``// Given value of N``    ``int` `N = 2;` `    ``// Function call to find number written on``    ``// the opposite face of the dice``    ``oppositeFaceOfDice(N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;` `class` `GFG``{``  ` `    ``// Function to find number``    ``// written on the``    ``// opposite face of the dice``    ``static` `void` `oppositeFaceOfDice(``int` `N)``    ``{``      ` `        ``// Stores number on opposite face``        ``// of dice``        ``int` `ans = ``7` `- N;` `        ``// Print the answer``        ``System.out.println(ans);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``      ` `        ``// Given value of N``        ``int` `N = ``2``;` `        ``// Function call to find number written on``        ``// the opposite face of the dice``        ``oppositeFaceOfDice(N);``    ``}``}` `// This code is contributed by Potta Lokesh`

## Python3

 `# Python3 program for the above approach` `# Function to find number written on the``# opposite face of the dice``def` `oppositeFaceOfDice(N):``    ` `    ``# Stores number on opposite face``    ``# of dice``    ``ans ``=` `7` `-` `N``    ` `    ``# Print the answer``    ``print``(ans)` `# Driver Code` `# Given value of N``N ``=` `2` `# Function call to find number written on``# the opposite face of the dice``oppositeFaceOfDice(N)` `# This code is contributed by gfgking`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{` `// Function to find number``// written on the``// opposite face of the dice``static` `void` `oppositeFaceOfDice(``int` `N)``{``  ` `    ``// Stores number on opposite face``    ``// of dice``    ``int` `ans = 7 - N;` `    ``// Print the answer``    ``Console.Write(ans);``}` `// Driver Code``public` `static` `void` `Main()``{``    ``// Given value of N``    ``int` `N = 2;` `    ``// Function call to find number written on``    ``// the opposite face of the dice``    ``oppositeFaceOfDice(N);``}``}` `// This code is contributed by SURENDRA_GANGWAR.`

## Javascript

 ``
Output
`5`

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up