Open In App

Distribute the white and black objects into maximum groups under certain constraints

Last Updated : 12 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

 Given W white objects and B black objects and a number D, the task is to find if it is possible to distribute the white and black objects into the maximum number of groups such that each group contains at least one of each type of object and the difference between the number of objects of different types in each group does not exceed D.

Examples:

Input: W=2, B=5, D=2
Output:
YES
Explanation:
The distribution can be as follows: {W, B, B, B} and {W, B, B}.
Each group contains at least one W and at least one B, and the difference in each group does not exceed D.

Input: W=2, B=7, D=2
Output:
NO

Approach: The maximum number of groups possible is min(W, B). Let us consider, W<B, then, there can at most W groups, and in each group there is only one W. The maximum number of B in each group will be D+1. Thus, the maximum value of B should be less than W*(D+1). This is the only necessary condition. Follow the steps below to solve the problem:

  • If W is greater than B, swap W and B. (Swapping does not alter the answer)
  • Check if B is greater than W*(D+1), print NO.
  • Otherwise, print YES.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if it is possible to distribute W and B
// into maximum groups possible
void isPossible(int W, int B, int D)
{
    // If W is greater than B, swap them
    if (W > B)
        swap(W, B);
    // Distribution is not possible
    if (B > W * (D + 1))
        cout << "NO" << endl;
    // Distribution is possible
    else
        cout << "YES" << endl;
}
// Driver code
int main()
{
    // Input
    int W = 2;
    int B = 5;
    int D = 2;
 
    // Function call
    isPossible(W, B, D);
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG
{
 
  // Function to check if it is possible to distribute W
  // and B
  // into maximum groups possible
  public static void isPossible(int W, int B, int D)
  {
 
    // If W is greater than B, swap them
    if (W > B) {
      int temp = W;
      W = B;
      B = temp;
    }
 
    // Distribution is not possible
    if (B > W * (D + 1))
      System.out.println("NO");
     
    // Distribution is possible
    else
      System.out.println("YES");
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    // Input
    int W = 2;
    int B = 5;
    int D = 2;
 
    // Function call
    isPossible(W, B, D);
     
  }
}
 
 // This code is contributed by Potta Lokesh


Python3




# Python 3 program for the above approach
 
# Function to check if it is possible to distribute W and B
# into maximum groups possible
def isPossible(W, B, D):
    # If W is greater than B, swap them
    if (W > B):
        temp = W
        W = B
        B = temp
         
    # Distribution is not possible
    if (B > W * (D + 1)):
        print("NO")
    # Distribution is possible
    else:
        print("YES")
 
# Driver code
if __name__ == '__main__':
    # Input
    W = 2
    B = 5
    D = 2
 
    # Function call
    isPossible(W, B, D)
     
    # This code is contributed by bgangwar59.
    


C#




// C# program for the above approach
using System;
 
class GFG {
 
    // Function to check if it is possible to distribute W
    // and B
    // into maximum groups possible
    static void isPossible(int W, int B, int D)
    {
 
        // If W is greater than B, swap them
        if (W > B) {
            int temp = W;
            W = B;
            B = temp;
        }
 
        // Distribution is not possible
        if (B > W * (D + 1))
            Console.WriteLine("NO");
 
        // Distribution is possible
        else
            Console.WriteLine("YES");
    }
 
    // Driver code
    public static void Main()
    {
 
        // Input
        int W = 2;
        int B = 5;
        int D = 2;
 
        // Function call
        isPossible(W, B, D);
    }
}
 
// This code is contributed by rishavmahato348.


Javascript




<script>
      // JavaScript program for the above approach
 
      // Function to check if it is possible to distribute W and B
      // into maximum groups possible
      function isPossible(W, B, D)
      {
       
          // If W is greater than B, swap them
          if (W > B)
          {
              let temp = W;
              W = B;
              B = temp;
          }
           
          // Distribution is not possible
          if (B > W * (D + 1))
              document.write("NO");
               
          // Distribution is possible
          else
              document.write("YES");
      }
      // Driver code
 
      // Input
      let W = 2;
      let B = 5;
      let D = 2;
 
      // Function call
      isPossible(W, B, D);
 
// This code is contributed by Potta Lokesh
  </script>


 
 

Output

YES

 

Time Complexity: O(1)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads