P2149-Solution

Introduction

In this problem, we are given three integers A, B, X that denote length of the rectangle, width of the rectangle and edge-length of square respectively we are supposed to make the area of the rectangle less than or equal to the area of square and finally we need to print the minimum cost required to achieve that, here costs refers to change in dimension of the rectangle

Approach

After taking the input if the area of rectangle and square are already equal or the area of the rectangle is less than square, then we are just going to print 0 as we don’t need to further change the dimension
moving further, if either length or breadth of rectangle is smaller than the area of square then we are going to print 1 as we only need to change any one of them to meet our condition, if both of them are greater than the area of the square then we are going to print 2 as we need to change both of them to meet our condition

Time Complexity

O(t), where t is the number of testcases because the loop runs t times, excluding that the time complexity is O(1) because we are making constant time operations in the loop

Space complexity

O(1) because we are not using extra data structure to store our answers making it a constant space approach

Code

import java.lang.*;
import java.io.*;

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
    Scanner s = new Scanner(System.in);
    // test cases input
    int t = s.nextInt();
    // test cases valueneed to be greater 0
    while(t-->0){
      
      // input for A
      int A = s.nextInt(); // red length
      // input for B
      int B = s.nextInt();  // red width
      // input for x
      int X = s.nextInt(); // blue edge length
      
      int square = X * X;
      // if multiply A and B which it should less than square
      if(A * B <= square ){
        System.out.println("0");
      }
      // the A and B value need to be less than square
      else if(A <= square || B <= square){
        System.out.println("1");
      }
      else{
        // if A and B ARE GREATER Than square  
        System.out.println("2");
      }
    }
	}
}