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++
#include<bits/stdc++.h>
using namespace std;
int oppositeFaceOfDice( int N)
{
int ans = 7 - N;
cout << ans;
}
int main()
{
int N = 2;
oppositeFaceOfDice(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void oppositeFaceOfDice( int N)
{
int ans = 7 - N;
System.out.println(ans);
}
public static void main(String[] args)
{
int N = 2 ;
oppositeFaceOfDice(N);
}
}
|
Python3
def oppositeFaceOfDice(N):
ans = 7 - N
print (ans)
N = 2
oppositeFaceOfDice(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void oppositeFaceOfDice( int N)
{
int ans = 7 - N;
Console.Write(ans);
}
public static void Main()
{
int N = 2;
oppositeFaceOfDice(N);
}
}
|
Javascript
<script>
function oppositeFaceOfDice(N) {
let ans = 7 - N;
document.write(ans);
}
let N = 2;
oppositeFaceOfDice(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...