The dice problem | Practice | GeeksforGeeks

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.
Example 1:
Input:
N = 6
Output:
1
Explanation:
For dice facing number 6 opposite face
will have the number 1.
Example 2:
Input:
N = 2
Output:
5
Explanation:
For dice facing number 5 opposite face
will have the number 2.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function oppositeFaceOfDice() which takes an integer N as an input parameter and return the number on the opposite face of the dice.

Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 6
+1
dheepanbalajib21 hours ago
int oppositeFaceOfDice(int N)
    {
        int arr[6]={6,5,4,3,2,1};
        
        return arr[N-1];
    }
0
rsbly7300951 week ago
class Solution {
  oppositeFaceOfDice(N) {
    return 7-N;
    // switch(N){
    //     case 1: return 6;
    //     case 2: return 5;
    //     case 3: return 4;
    //     case 4: return 3;
    //     case 5: return 2;
    //     case 6: return 1;
        
    // }
  }
}
0
abhisri19971 week ago
Well most efficient code will be this to return 7-N;
As this doesn't have to store any value.
 
Where as this was my code:
 
const DICE_FACE = [6,5,4,3,2,1]
 
class Solution {
  oppositeFaceOfDice(N) {
      return DICE_FACE[N-1];
  }
}
0
sakshamraj2292 months ago
class Solution
{
public:
    int oppositeFaceOfDice(int N)
    {
        return 7-N;
    }
};
0
sanyam00292 months ago
 int oppositeFaceOfDice(int N)   {       // Write Your Code here       int arr[6] = {1,2,3,4,5,6};       return arr[6-N];          }
0
shaileshk12 months ago
//Java
return 7 -N;
0
rohitpandey484Premium2 months ago
Java Solution 
class Solution{   static int oppositeFaceOfDice(int N){       int sum=7-N;       return sum;   }}
0
harika52112 months ago
 def oppositeFaceOfDice(self, N):    #code here     sum=0    for i in range(1,7):        sum=i+N        if sum==7:            return i            break        else:            continue      
0
shailendrarana8913 months ago
Simple Java Code
-------------------
int sumOfOppositeFace = 7;       int oppositeFace = 0;       for(int  i = 1; i <= N ;i++){           if(i == N){               oppositeFace += sumOfOppositeFace - i;           }       }       return oppositeFace;
0
birlangirohitkumar20023 months ago
 int oppositeFaceOfDice(int N)   {      int arr[6]={1,2,3,4,5,6};      return arr[6-N];// Write Your Code here   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.