Maximize count of sheets possible by repeatedly reducing its area to half
Given two integers A and B, representing the length and the breadth of a sheet, the task is to find the maximum number of sheets that can be generated from it by repeatedly reducing the area to half until it is not divisible by 2.
Examples:
Input: A = 5, B = 10
Output: 2
Explanation:
- Initial Area = 5 * 10. Count = 0.
- Area / 2 = 5 * 5. Count = 2.
Input: A = 1, B = 8
Output: 8
Approach: Follow the steps below to solve the problem:
- Calculate the total area of the initial sheet provided.
- Now, keep dividing the area of the sheet by 2 until it becomes odd.
- After every division, increase the count to twice its value.
- Finally, print the count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSheets( int A, int B)
{
int area = A * B;
int count = 1;
while (area % 2 == 0) {
area /= 2;
count *= 2;
}
return count;
}
int main()
{
int A = 5, B = 10;
cout << maxSheets(A, B);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxSheets( int A, int B)
{
int area = A * B;
int count = 1 ;
while (area % 2 == 0 )
{
area /= 2 ;
count *= 2 ;
}
return count;
}
public static void main(String args[])
{
int A = 5 , B = 10 ;
System.out.println(maxSheets(A, B));
}
}
|
Python3
def maxSheets( A, B):
area = A * B
count = 1
while (area % 2 = = 0 ):
area / / = 2
count * = 2
return count
A = 5
B = 10
print (maxSheets(A, B))
|
C#
using System;
class GFG
{
static int maxSheets( int A, int B)
{
int area = A * B;
int count = 1;
while (area % 2 == 0)
{
area /= 2;
count *= 2;
}
return count;
}
public static void Main()
{
int A = 5, B = 10;
Console.WriteLine(maxSheets(A, B));
}
}
|
Javascript
<script>
function maxSheets(A, B)
{
let area = A * B;
let count = 1;
while (area % 2 == 0) {
area /= 2;
count *= 2;
}
return count;
}
let A = 5, B = 10;
document.write(maxSheets(A, B));
</script>
|
Time Complexity: O(log2(A * B))
Auxiliary Space: O(1)
Last Updated :
23 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...