C++ program to find longest possible time not greater than T to solve all problems


Suppose we have an array A with N elements. Have another number T. Consider Amal is trying to participate in a programming contest. It lasts for T minutes and present N problems. He has A[i] time to solve ith problem. He will choose zero or more problems to solve from the N problems, so that it takes no longer T minutes in total to solve them. We have to find the longest possible time it will take to solve his choice of problems.

So, if the input is like T = 17; A = [2, 3, 5, 7, 11], then the output will be 17, because if he chooses the first four problems, it takes him 2 + 3 + 5 + 7 = 17 minutes in total to solve them, which is the longest possible time not exceeding T minutes.

Steps

To solve this, we will follow these steps −

n := size of A
Define an array b of size (n / 2) and c of size (n - n/2)
for initialize i := 0, when i < n / 2, update (increase i by 1), do:
   b[i] := A[i]
for initialize i := n / 2, when i < n, update (increase i by 1), do:
   c[i - n / 2] = A[i]
Define an array B, C
for bit in range 0 to 2^(n/2), increase bit after each iteration, do
   p := 0
   for initialize i := 0, when i < n / 2, update (increase i by 1), do:
      if bit AND 2^i is non zero, then
         p := p + b[i]
   insert p at the end of B
for bit in range 0 to 2^(n - n/2), increase bit after each iteration
   p := 0
   for initialize i := 0, when i < n - n / 2, update (increase i by 1), do:
      if bit AND 2^i is non-zero, then
         p := p + c[i]
   insert p at the end of C
mx := 0
sort the array C
for initialize i := 0, when i < size of B, update (increase i by 1), do:
   if t - B[i] < 0, then:
      Ignore following part, skip to the next iteration
   itr = next larger element of (t - B[i]) in C
   (decrease itr by 1)
   mx := maximum of mx and (itr + B[i])
return mx

Example

Let us see the following implementation to get better understanding −

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

int solve(int t, vector<int> A){
   int n = A.size();
   vector<int> b(n / 2), c(n - n / 2);
   for (int i = 0; i < n / 2; i++)
      b[i] = A[i];
   for (int i = n / 2; i < n; i++)
      c[i - n / 2] = A[i];
   vector<int> B, C;
   for (int bit = 0; bit < (1 << (n / 2)); bit++){
      int p = 0;
      for (int i = 0; i < n / 2; i++){
         if (bit & (1 << i))
            p += b[i];
      }
      B.push_back(p);
   }
   for (int bit = 0; bit < (1 << (n - n / 2)); bit++){
      int p = 0;
      for (int i = 0; i < n - n / 2; i++){
         if (bit & (1 << i))
            p += c[i];
      }
      C.push_back(p);
   }
   int mx = 0;
   sort(C.begin(), C.end());
   for (int i = 0; i < B.size(); i++){
      if (t - B[i] < 0)
         continue;
      auto itr = upper_bound(C.begin(), C.end(), t - B[i]);
      itr--;
      mx = max(mx, *itr + B[i]);
   }
   return mx;
}
int main(){
   int T = 17;
   vector<int> A = { 2, 3, 5, 7, 11 };
   cout << solve(T, A) << endl;
}

Input

17, { 2, 3, 5, 7, 11 }

Output

17

Updated on: 03-Mar-2022

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements