Codechef April challange : Chef and Dice solution

 Solving codechef April challange :



    Chef and Dice Solution in Java.

Problem statement -

Chef has N 6-sided standard dice. Each die has dimensions 1×1×1. Since Chef is bored during the quarantine, he decides to stack dice for fun.

First, Chef forms four vertical stacks of dice (not necessarily with the same height; empty stacks are allowed) on his table, which together make up a pile of dice with base area up to 2×2. Among all such structures, the total visible surface area of Chef's structure must be the smallest possible.

Then, Chef calculates the number of pips on the visible faces of all dice in the structure. A face of a die is visible if it does not touch the table or another die.

Now, he is wondering: among all possible arrangements of dice, what is the maximum possible total number of visible pips? Since he is busy cooking, he is asking you to solve this.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first and only line of each test case contains a single integer N.

Output

For each test case, print a single line containing one integer ― the maximum possible number of visible pips.

Constraints

  • 1T105


  import java.util.*;
  class cf
  {
    static long calc(long n) {
      if(n==1) return 20;
      if(n==2) return 36;
      if(n==3) return 51;
      long div = n/4;
      long mod  = n % 4;
      long remain =n - ((n-4)+(4-mod));
      long a = 11*(n-4);
      long b = 15*(4-mod);
      long c = 18*(remain);
      long alt = (mod==3)?-3:(mod==1)?2:0;
      long ans = a+b+c+alt;
      // System.out.println(ans+" : "+remain );
      return ans;
    }
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
      try
      {
        int t = input.nextInt();
        while(t-->0) {
          long n = input.nextLong();
          long ans = calc(n);
          System.out.println(ans);
        }
      }
      catch(Exception e){
        return;
      }
    }
  }

Output :

  5
  1
  -> 20
  
  7
  -> 99
  
  100
  -> 1116
  
  12345
  -> 135816
  
  13
  -> 164 

Popular Posts

java:17: error: local variables referenced from a lambda expression must be final or effectively final count ++ ;

Family Tree Project in Java

Creating basic tic tac toe android app using java