Combinations from n arrays picking one element from each array

By | September 25, 2023

One combination of made up of N different things. You are given an array of length N which indicate the amount of N different things which one combination contain. You are given another array of same N things you already have. Find number of combination we can make.

Example:

Input:
2 4 8
16 16 16 
Output: 2
Explanation: 
Here one combination needed 2 unit of first element 
and 4 and 8 unit of second and the third element respectively. 
We have initialamount of all the element is 16. 
If we will make a two combination than we have left the amount of all the elements {12, 8, 0}. 
Now we can not make any new combination.

Naive Approach:
We will check for the any no that the combination is possible or not. If it is possible than we will increase the no and check for the higher no and if is not possible than last possible no is our ans. So, we have to start to check from the one to the maximum number of possible combination.

Time Complexity:

O(N*max(a[0].......a[N])) 

Efficient Approach:
The main point is to see if the combination for the element t is not possible than for all the number greater than t we can not make a combination. So here we can use a binary search. The time complexity will be reduced to the order of log.

Time Complexity:

O(N*log(max(a[0].......a[N]))) 

Implementation in C++:

#include <bits/stdc++.h>
using namespace std;

bool possible(long long combo[], long long initial[], int n, long long mid) {

  for (int i = 0; i < n; ++i) {

    // cheking for the we have enough amount of things to make 
    // mid number of combination 
    long long x = initial[i] - combo[i] * mid;

    // it indicate that we have not enough amount of 
    // things 
    if (x < 0)
      return false;
  }

  // indicate we can make mid amount of combination
  return true;

}

long long maxNoOfCombination(long long combo[], long long initial[], int n) {

  long long low = 1; // lowest possible ans
  long long high = 0; // highest possible ans

  for (int i = 0; i < n; ++i) {

    high = max(high, initial[i]);
  }

  while (low <= high) {

    long long mid = low + (high - low) / 2;

    // cheking for the mid no of combination is
    // possible or not
    // if it is possible than we will check for the higher no 
    // and if it is not possible than we will check for the
    // lower no than the mid
    if (possible(combo, initial, n, mid))
      low = mid + 1;
    else
      high = mid - 1;
  }

  return low - 1; // returning ans
}
int main() {
  // combination array of N different things
  long long combo[] = {1, 2, 3, 4};

  // initial amount of same N different thins we have 
  long long initial[] = {4, 3, 3, 4};

  // size of the array 
  int n = sizeof(combo) / sizeof(combo[0]);

  // returns the max no of combination we can make 
  cout << maxNoOfCombination(combo, initial, n) << "\n";

}

Output:

1
Author: Mithlesh Upadhyay

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.