 Open in App
Not now

# Maximize count of sheets possible by repeatedly reducing its area to half

• Difficulty Level : Basic
• Last Updated : 23 Mar, 2021

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++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to calculate the``// maximum number of sheets``// possible by given operations``int` `maxSheets(``int` `A, ``int` `B)``{``    ``int` `area = A * B;` `    ``// Initial count of sheets``    ``int` `count = 1;` `    ``// Keep dividing the``    ``// sheets into half``    ``while` `(area % 2 == 0) {` `        ``// Reduce area by half``        ``area /= 2;` `        ``// Increase count by twice``        ``count *= 2;``    ``}` `    ``return` `count;``}` `// Driver Code``int` `main()``{` `    ``int` `A = 5, B = 10;``    ``cout << maxSheets(A, B);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG``{` `  ``// Function to calculate the``  ``// maximum number of sheets``  ``// possible by given operations``  ``static` `int` `maxSheets(``int` `A, ``int` `B)``  ``{``    ``int` `area = A * B;` `    ``// Initial count of sheets``    ``int` `count = ``1``;` `    ``// Keep dividing the``    ``// sheets into half``    ``while` `(area % ``2` `== ``0``)``    ``{` `      ``// Reduce area by half``      ``area /= ``2``;` `      ``// Increase count by twice``      ``count *= ``2``;``    ``}``    ``return` `count;``  ``}`  `  ``// Driver Code``  ``public` `static` `void` `main(String args[])``  ``{``    ``int` `A = ``5``, B = ``10``;``    ``System.out.println(maxSheets(A, B));``  ``}``}` `// This code is contributed by jana_sayantan.`

## Python3

 `# Python program for the above approach` `# Function to calculate the``# maximum number of sheets``# possible by given operations``def` `maxSheets( A, B):``    ``area ``=` `A ``*` `B` `    ``# Initial count of sheets``    ``count ``=` `1` `    ``# Keep dividing the``    ``# sheets into half``    ``while` `(area ``%` `2` `=``=` `0``):` `        ``# Reduce area by half``        ``area ``/``/``=` `2` `        ``# Increase count by twice``        ``count ``*``=` `2``    ``return` `count` `# Driver Code``A ``=` `5``B ``=` `10``print``(maxSheets(A, B))` `# This code is contributed by rohitsingh07052.`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG``{` `    ``// Function to calculate the``    ``// maximum number of sheets``    ``// possible by given operations``    ``static` `int` `maxSheets(``int` `A, ``int` `B)``    ``{``        ``int` `area = A * B;` `        ``// Initial count of sheets``        ``int` `count = 1;` `        ``// Keep dividing the``        ``// sheets into half``        ``while` `(area % 2 == 0)``        ``{` `            ``// Reduce area by half``            ``area /= 2;` `            ``// Increase count by twice``            ``count *= 2;``        ``}` `        ``return` `count;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int` `A = 5, B = 10;``        ``Console.WriteLine(maxSheets(A, B));``    ``}``}` `// This code is contributed by chitranayal.`

## Javascript

 ```  `

Output:

`2`

Time Complexity: O(log2(A * B))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up