# Maximum length of rod for Q-th person

Given lengths of n rods in an array **a[]**. If any person picks any rod, half of the longest rod (or (max + 1) / 2 ) is assigned and remaining part (max – 1) / 2 is put back. It may be assumed that sufficient number of rods are always available, answer M queries given in an array q[] to find the largest length of rod available for **q ^{ith}** person, provided

**q**is a valid person number starting from 1.

^{i}Examples :

Input : a[] = {6, 5, 9, 10, 12} q[] = {1, 3} Output : 12 9 The first person gets maximum length as 12. We remove 12 from array and put back (12 -1) / 2 = 5. Second person gets maximum length as 10. We put back (10 - 1)/2 which is 4. Third person gets maximum length as 9. Input : a[] = {6, 5, 9, 10, 12} q[] = {3, 1, 2, 7, 4, 8, 9, 5, 10, 6} Output : 9 12 10 5 6 4 3 6 3 5

**Approach : **

Use a stack and a queue. First sort all the lengths and push them onto a stack. Now, take the top element of stack, and divide by 2 and push the remaining length to queue. Now, from next customer onwards :

- If stack is empty, pop front queue and push back to queue. It’s half (front / 2), if non zero.
- If queue is empty, pop from stack and push to queue it’s half (top / 2), if non zero.
- If both are non empty, compare top and front, which ever is larger should be popped, divided by 2 and then pushed back.
- If both are empty, store is empty! Stop here!

At each step above store the length available to i^{th} customer in separate array, say “ans”. Now, start answering the queries by giving ans[Q_{i}] as output.

**Below is the implementation of above approach :**

`// CPP code to find the length of largest ` `// rod available for Q-th customer ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find largest length of ` `// rod available for Q-th customer ` `vector<` `int` `> maxRodLength(` `int` `ar[], ` ` ` `int` `n, ` `int` `m) ` `{ ` ` ` `queue<` `int` `> q; ` ` ` ` ` `// sort the rods according to lengths ` ` ` `sort(ar, ar + n); ` ` ` ` ` `// Push sorted elements to a stack ` ` ` `stack<` `int` `> s; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `s.push(ar[i]); ` ` ` ` ` `vector<` `int` `> ans; ` ` ` ` ` `while` `(!s.empty() || !q.empty()) { ` ` ` `int` `val; ` ` ` ` ` `// If queue is empty -> pop from stack ` ` ` `// and push to queue it’s half(top/2), ` ` ` `// if non zero. ` ` ` `if` `(q.empty()) { ` ` ` `val = s.top(); ` ` ` `ans.push_back(val); ` ` ` `s.pop(); ` ` ` `val /= 2; ` ` ` ` ` `if` `(val) ` ` ` `q.push(val); ` ` ` `} ` ` ` `// If stack is empty -> pop front from ` ` ` `// queue and push back to queue it’s ` ` ` `// half(front/2), if non zero. ` ` ` `else` `if` `(s.empty()) { ` ` ` `val = q.front(); ` ` ` `ans.push_back(val); ` ` ` `q.pop(); ` ` ` `val /= 2; ` ` ` `if` `(val != 0) ` ` ` `q.push(val); ` ` ` `} ` ` ` `// If both are non empty -> ` ` ` `// compare top and front, whichsoever is ` ` ` `// larger should be popped, divided by 2 ` ` ` `// and then pushed back. ` ` ` `else` `{ ` ` ` `val = s.top(); ` ` ` `int` `fr = q.front(); ` ` ` `if` `(fr > val) { ` ` ` `ans.push_back(fr); ` ` ` `q.pop(); ` ` ` `fr /= 2; ` ` ` `if` `(fr) ` ` ` `q.push(fr); ` ` ` `} ` ` ` `else` `{ ` ` ` `ans.push_back(val); ` ` ` `s.pop(); ` ` ` `val /= 2; ` ` ` `if` `(val) ` ` ` `q.push(val); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// n : number of rods ` ` ` `// m : number of queries ` ` ` `int` `n = 5, m = 10; ` ` ` ` ` `int` `ar[n] = { 6, 5, 9, 10, 12 }; ` ` ` ` ` `vector<` `int` `> ans = maxRodLength(ar, n, m); ` ` ` ` ` `int` `query[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; ` ` ` `int` `size = ` `sizeof` `(query) / ` `sizeof` `(query[0]); ` ` ` `for` `(` `int` `i = 0; i < size; i++) ` ` ` `cout << ans[query[i] - 1] << ` `" "` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

12 10 9 6 6 5 5 4 3 3

**Time complexity :** O(N log(N))

## Recommended Posts:

- Find maximum average subarray of k length
- Find maximum sum array of length less than or equal to m
- Find the Increasing subsequence of length three with maximum product
- Maximum Length Bitonic Subarray | Set 2 (O(n) time and O(1) Space)
- Maximum Length Bitonic Subarray | Set 1 (O(n) tine and O(n) space)
- Length of the smallest sub-string consisting of maximum distinct characters
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Length of the longest alternating subarray
- Count unique subsequences of length K
- Length of Longest sub-string that can be removed
- Length of the longest valid substring
- Find the length of largest subarray with 0 sum
- Length of the longest Subarray with only Even Elements
- Length of longest increasing circular subarray

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.