Open In App

The dice problem

Last Updated : 16 Jul, 2025
Comments
Improve
Suggest changes
10 Likes
Like
Report

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
Explanation: For dice facing number 6 opposite face will have the number 1.

[Naive Approach] Using if-else Statement

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 

C++
// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;

int oppositeFaceOfDice(int n) {
  int ans;
  if(n==1){
    ans = 6;
  }else if(n==2){
    ans = 5;
  }else if(n==3){
    ans = 4;
  }else if(n==4){
    ans = 3;
  }else if(n==5){
    ans = 2;
  }else{
    ans = 1;
  }
  return ans; 
}

int main() {
    int n = 2;
    cout << oppositeFaceOfDice(n);
    return 0;
}
C
#include <stdio.h>

int oppositeFaceOfDice(int n)
{
    int ans;
    if (n == 1)
    {
        ans = 6;
    }
    else if (n == 2)
    {
        ans = 5;
    }
    else if (n == 3)
    {
        ans = 4;
    }
    else if (n == 4)
    {
        ans = 3;
    }
    else if (n == 5)
    {
        ans = 2;
    }
    else
    {
        ans = 1;
    }
    return ans;
}

int main()
{
    int n = 2;
    printf("%d", oppositeFaceOfDice(n));
    return 0;
}
Java
public class GfG {
    public static int oppositeFaceOfDice(int n)
    {
        int ans;
        if (n == 1) {
            ans = 6;
        }
        else if (n == 2) {
            ans = 5;
        }
        else if (n == 3) {
            ans = 4;
        }
        else if (n == 4) {
            ans = 3;
        }
        else if (n == 5) {
            ans = 2;
        }
        else {
            ans = 1;
        }
        return ans;
    }

    public static void main(String[] args)
    {
        int n = 2;
        System.out.println(oppositeFaceOfDice(n));
    }
}
Python
def oppositeFaceOfDice(n):
    if n == 1:
        return 6
    elif n == 2:
        return 5
    elif n == 3:
        return 4
    elif n == 4:
        return 3
    elif n == 5:
        return 2
    else:
        return 1

n = 2
print(oppositeFaceOfDice(n))
C#
using System;

class GfG {
    static int oppositeFaceOfDice(int n) {
        int ans;
        if(n==1){
            ans=6;
        }else if(n==2){
            ans=5;
        }else if(n==3){
            ans=4;
        }else if(n==4){
            ans=3;
        }else if(n==5){
            ans=2;
        }else{
            ans=1;
        }
        return ans;
    }

    static void Main() {
        int n = 2;
        Console.WriteLine(oppositeFaceOfDice(n));
    }
}
JavaScript
function oppositeFaceOfDice(n) {
    let ans;
    if(n === 1) {
        ans = 6;
    } else if(n === 2) {
        ans = 5;
    } else if(n === 3) {
        ans = 4;
    } else if(n === 4) {
        ans = 3;
    } else if(n === 5) {
        ans = 2;
    } else {
        ans = 1;
    }
    return ans;
}

let n = 2;
console.log(oppositeFaceOfDice(n));

Output
5

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

[Expected Approach] Using Sum of Two Sides

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.

C++
#include<bits/stdc++.h>
using namespace std;

int oppositeFaceOfDice(int n) {
  
    // Stores number on opposite face
    // of dice
    int ans = 7 - n;
    return ans;
}

int main() {
  
    int n = 2;
    cout << oppositeFaceOfDice(n);

    return 0;
}
C
#include <stdio.h>

int oppositeFaceOfDice(int n) {
    
    // Stores number on opposite face
    // of dice
    int ans = 7 - n;
    return ans;
}

int main() {
    int n = 2;
    printf("%d", oppositeFaceOfDice(n));
    return 0;
}
Java
public class GfG {
    public static int oppositeFaceOfDice(int n) {
        
        // Stores number on opposite face
        // of dice
        int ans = 7 - n;
        return ans;
    }

    public static void main(String[] args) {
        int n = 2;
        System.out.println(oppositeFaceOfDice(n));
    }
}
Python
def oppositeFaceOfDice(n):
    
    # Stores number on opposite face
    # of dice
    ans = 7 - n
    return ans

n = 2
print(oppositeFaceOfDice(n))
C#
using System;

class GfG {
    public static int OppositeFaceOfDice(int n) {
        
        // Stores number on opposite face
        // of dice
        int ans = 7 - n;
        return ans;
    }

    static void Main() {
        int n = 2;
        Console.WriteLine(OppositeFaceOfDice(n));
    }
}
JavaScript
function oppositeFaceOfDice(n) {
    // Stores number on opposite face
    // of dice
    let ans = 7 - n;
    return ans;
}

let n = 2;
console.log(oppositeFaceOfDice(n));

Output
5

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


Article Tags :

Explore