- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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

- Related Questions & Answers
- Program to find number not greater than n where all digits are non-decreasing in python
- Program to find length of longest possible stick in Python?
- C++ Program to find minimum possible unlancedness of generated string T
- C++ program to find in how many bases we can represent the number not greater than M
- Queries for greater than and not less than using C++
- Program to filter all values which are greater than x in an array
- Program to find expected growth of virus after time t in Python
- Program to find lowest sum of pairs greater than given target in Python
- Python - Find words greater than given length
- Python program to print Rows where all its Elements’ frequency is greater than K
- Python Program to find out the number of sets greater than a given value
- Program to find all possible IP address after restoration in C++
- Program to find minimum time to complete all tasks in python
- Program to find minimum time to finish all jobs in Python
- Program to find longest substring of all vowels in order in Python