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

// C++ program for the above approach
#include <bits/stdc++.h>
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

<script>
 
    // Javascript program for the above approach
     
    // Function to calculate the
    // maximum number of sheets
    // possible by given operations
    function maxSheets(A, B)
    {
        let area = A * B;
 
        // Initial count of sheets
        let 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
     
     let A = 5, B = 10;
    document.write(maxSheets(A, B));
     
</script>
   

 
 

Output: 

2

 

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




Reffered: https://www.geeksforgeeks.org


Geometric

Related
Minimum number of steps required to reach origin from a given point Minimum number of steps required to reach origin from a given point
Find the segment that overlaps with maximum number of segments Find the segment that overlaps with maximum number of segments
Check if given polygon is a convex polygon or not Check if given polygon is a convex polygon or not
Minimum points to be selected such that removal of line segments passing through them empties given array Minimum points to be selected such that removal of line segments passing through them empties given array
Area of the largest rectangle formed by lines parallel to X and Y axis from given set of points Area of the largest rectangle formed by lines parallel to X and Y axis from given set of points

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12