Distribute the white and black objects into maximum groups under certain constraints
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 possiblevoid 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 codeint 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 approachimport 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 possibledef 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 codeif __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 approachusing 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> |
YES
Time Complexity: O(1)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


