Guest User

Untitled

a guest
Jan 1st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. /**
  6.  * Created by bugkiller on 31/12/17.
  7.  */
  8.  
  9. class TotalDiamonds {
  10.  
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  13.         int t, n;
  14.         t = Integer.parseInt(br.readLine());
  15.         while (t-- > 0) {
  16.             n = Integer.parseInt(br.readLine());
  17.             System.out.println(solve(n));
  18.         }
  19.     }
  20.  
  21.     private static int solve(int n) {
  22.         return getTotalDiamonds(n);
  23.     }
  24.  
  25.     private static int getTotalDiamonds(int n) {
  26.         int totalDiamonds = 0, prevSum = 0, currentSum = 0;
  27.  
  28.         //Calculate no. of diamonds in first row
  29.         for (int i = 1; i <=n; i++)
  30.             totalDiamonds += getDiamonds(i + 1);
  31.  
  32.         prevSum = totalDiamonds;
  33.  
  34.         for (int i = 2; i <=n; i++) {
  35.             currentSum = prevSum - getDiamonds(i) + getDiamonds(n + i);
  36.             totalDiamonds += currentSum;
  37.             prevSum = currentSum;
  38.         }
  39.         return totalDiamonds;
  40.     }
  41.  
  42.     private static int getDiamonds(int roomNo) {
  43.         int evenSum = 0, oddSum = 0, temp;
  44.  
  45.         while (roomNo > 0) {
  46.             temp = roomNo % 10;
  47.  
  48.             if (temp % 2 == 0) evenSum += temp;
  49.             else oddSum += temp;
  50.  
  51.             roomNo /= 10;
  52.         }
  53.         return Math.abs(oddSum-evenSum);
  54.     }
  55. }
Add Comment
Please, Sign In to add comment