Maximum length of rod for Qth person in C++


Problem statement

Given lengths of n rods in an array. 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 qith person, provided qi is a valid person number starting from 1

Example

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.

If input array is {6, 5, 9, 10, 12} and

Queries array is {1, 3} then output will be 12 and 9 as −

  • First person gets rod with maximum length i.e. 12
  • Then we remove 12 from array and put back (12 – 1) / 2 = 5
  • Second person gets rod with maximum length i.e. 10
  • Then we put back (10 – 1) / 2 = 4
  • Third person gets rod with maximum length i.e. 9

Algorithm

  • First sort all the lengths and push them onto a stack
  • Take the top element of stack, and divide by 2 and push the remaining length to queue
  • 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, whichever is larger should be popped, divided by 2 and then pushed back
  • Stop when both stack and queue are empty

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
vector<int> getMaxRodLength(int *arr, int n, int m) {
   queue<int> q;
   sort(arr, arr + n);
   stack<int> s;
   for (int i = 0; i < n; ++i) {
      s.push(arr[i]);
   }
   vector<int> result;
   while (!s.empty() || !q.empty()) {
      int val;
      if (q.empty()) {
         val = s.top();
         result.push_back(val);
         s.pop();
         val = val / 2;
         if (val) {
            q.push(val);
         }
      } else if (s.empty()) {
         val = q.front();
         result.push_back(val);
         q.pop();
         val = val / 2;
         if (val != 0) {
            q.push(val);
         }
      } else {
         val = s.top();
         int fr = q.front();
         if (fr > val) {
            result.push_back(fr);
            q.pop();
            fr = fr / 2;
            if (fr) {
               q.push(fr);
            }
         } else {
            result.push_back(val);
            s.pop();
            val = val / 2;
            if (val) {
               q.push(val);
            }
         }
      }
   }
   return result;
}
int main() {
   int rods = 5;
   int queries = 10;
   int arr[rods] = {6, 5, 9, 10, 12};
   vector<int> result = getMaxRodLength(arr, rods, queries);
   int query[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   int n_query = sizeof(query) / sizeof(query[0]);
   cout << "Rod length = ";
   for (int i = 0; i < n_query; ++i) {
      cout << result[query[i] - 1] << " ";
   }
   cout << endl;
   return 0;
}

Output

When you compile and execute above program. It generates following output −

Rod length = 12 10 9 6 6 5 5 4 3 3

Updated on: 10-Jan-2020

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements