Given a rectangle of dimensions L x B find the minimum number (N) of identical squares of maximum side that can be cut out from that rectangle so that no residue remains in the rectangle. Also find the dimension K of that square. Example 1: Input: L = 2, B = 4 Output: N = 2, K = 2 Explaination: 2 squares of 2x2 dimension. Example 2: Input: L = 6, B = 3 Output: N = 2, K = 3 Explaintion: 2 squares of 3x3 dimension.
Constraints: 1 ≤ L, B ≤ 109 ---------------------------------- #adSOLUTION OF THE QUESTION ---------------------------------- See DSA Course : https://trainings.internshala.com/data-structures-algorithms-course/?tracking_source=ISRP14182585 SOLUTION in CPP/JAVA #CPP:- class Solution{ |
public: |
vector<long long int> minimumSquares(long long int L, long long int B) |
{ |
// code here |
long long int x=__gcd(L,B); |
vector<long long int>ans; |
ans.push_back((L*B)/(x*x)); |
ans.push_back(x); |
return ans; |
|
} |
JAVA :-
//{ Driver Code Starts |
//Initial Template for Java |
|
import java.io.*; |
import java.util.*; |
import java.lang.*; |
|
class GFG{ |
public static void main(String args[])throws IOException |
{ |
BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); |
int t = Integer.parseInt(read.readLine()); |
while(t-- > 0){ |
String input_line[] = read.readLine().trim().split("\\s+"); |
long L = Long.parseLong(input_line[0]); |
long B = Long.parseLong(input_line[1]); |
|
Solution ob = new Solution(); |
List<Long> ans = new ArrayList<Long>(); |
ans = ob.minimumSquares(L, B); |
System.out.print(ans.get(0)+" "); |
System.out.println(ans.get(1)); |
} |
} |
} |
// } Driver Code Ends |
|
|
//User function Template for Java |
|
class Solution{ |
static List<Long> minimumSquares(long L, long B) |
{ |
// code here |
List<Long> al = new ArrayList<>(); |
long len = gcd(L,B); |
|
long no_of_sq = (L*B)/(len*len); |
|
al.add(no_of_sq); |
al.add(len); |
|
return al; |
|
} |
static long gcd (long a, long b) |
{ |
if(b==0) return a; |
return gcd(b, a%b); |
} |
}
#tags : POTD , Problem of the day, code , c++ ,potd solution, 3rd march 2023,Java
Learn DSA In most affordable price
Use CODE : ISRP14182585 / CLick Me to Avail MAX Discount
Learn DSA In most affordable price
Use CODE : ISRP14182585 / CLick Me to Avail MAX Discount