Thursday, March 5, 2015

HackerRank. Contest 101. Marble Cut

Problem Statement

Given a marble of size l * b, your task is to find out if it can be cut into pieces of size 3×1 or not.


Input Format

First line will contain T i.e. number of the test cases.
Next T lines will contain pair of integers l and b separated by a single space.


Constraints

1≤T≤10^5
1≤l,b≤10^9


Output Format

Print "YES" if it is possible to cut the given marble without any area left, else print "NO" followed by remaining area.
Link: https://www.hackerrank.com/contests/101hack22/challenges/marble-cut



Problem with some pitfalls.
There are 3 cases:
  1. At least one side % 3 == 0, then "Yes".
  2. At least one side < 3. then "No" and remaining area will be (a % 3) * (b % 3).
  3. Otherwise, "No", remaining area = (a * b) % 3.

Code:

import java.util.Scanner;

/*
 * https://www.hackerrank.com/contests/101hack22/challenges/marble-cut
*/
public class P1_MarbleCut {

 public static void main(String[] args) {
  new P1_MarbleCut().doJob();
 }

 private void doJob() {
  try (Scanner sc = new Scanner(System.in)) {
   int t = sc.nextInt();
   StringBuilder res = new StringBuilder();
   while (t-- > 0) {
    long a = sc.nextInt();
    long b = sc.nextInt();
    if (a % 3 == 0 || b % 3 == 0) {
     res.append("YES");
    } else if (a < 3 || b < 3) {
     res.append("NO ").append((a % 3) * (b % 3));
    } else {
     res.append("NO ").append((a * b) % 3);
    }
    res.append(System.lineSeparator());
   }
   System.out.println(res.toString().trim());
  }
 }
}

No comments :

Post a Comment