Question: Largest Possible Square For Cut Sticks: -Want To Cut Sticks So That We Achieve 4 Sticks Of The Same Length. (There Can Be Leftover Pieces). What Is The Longest Square Side That We Can Achieve? -Use This Function Def Square(A, B) Input: Two Integers A, B Output: Return The Side Length Of The Largest Square That We Can Have, If Not Possible Function Should return 0.
Largest possible square for cut sticks:
-want to cut sticks so that we achieve 4 sticks of the same length. (there can be leftover pieces). What is the longest square side that we can achieve?
-use this function def square(A, B)
Input: two integers A, B
Output: Return the side length of the largest square that we can have, if not possible function should return 0.
ex1:
Input: A = 13, B = 11
Output: function will return 5 because we can cut two sticks of length 5 from each of the given sticks.
ex2:
Input: Given A = 10, B = 21
Output: the function will return 7 because we can split the second stick B into three sticks of length 7 and shorten the first stick A by 3.
ex3:
Input: Given A = 2, B = 1
Output: the function will return 0 as it is not possible to make any square from the sticks provided
ex4:
Input: Given A = 1, B = 8
Output: the function will return 2 because we can cut stick B into 4 parts
class Solution {
public int solution(int A, int B) {
int totalSticks = A + B;
int maxLenghthTogether = totalSticks / 4;
while(maxLenghthTogether > 0) {
int maxLenghthA = A / maxLenghthTogether;
int maxLenghthB = B / maxLenghthTogether;
if(maxLenghthA + maxLenghthB >= 4) {
return maxLenghthTogether;
}
maxLenghthTogether--;
}
return 0;
}
}