Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- /**
- * Created by bugkiller on 31/12/17.
- */
- class TotalDiamonds {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int t, n;
- t = Integer.parseInt(br.readLine());
- while (t-- > 0) {
- n = Integer.parseInt(br.readLine());
- System.out.println(solve(n));
- }
- }
- private static int solve(int n) {
- return getTotalDiamonds(n);
- }
- private static int getTotalDiamonds(int n) {
- int totalDiamonds = 0, prevSum = 0, currentSum = 0;
- //Calculate no. of diamonds in first row
- for (int i = 1; i <=n; i++)
- totalDiamonds += getDiamonds(i + 1);
- prevSum = totalDiamonds;
- for (int i = 2; i <=n; i++) {
- currentSum = prevSum - getDiamonds(i) + getDiamonds(n + i);
- totalDiamonds += currentSum;
- prevSum = currentSum;
- }
- return totalDiamonds;
- }
- private static int getDiamonds(int roomNo) {
- int evenSum = 0, oddSum = 0, temp;
- while (roomNo > 0) {
- temp = roomNo % 10;
- if (temp % 2 == 0) evenSum += temp;
- else oddSum += temp;
- roomNo /= 10;
- }
- return Math.abs(oddSum-evenSum);
- }
- }
Add Comment
Please, Sign In to add comment