
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum length of rod for Qth person in C++n
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
#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
- Related Articles
- Program to find maximum profit by cutting the rod of different length in C++
- Python Program for Cutting a Rod
- Maximum Length Chain of Pairs
- Maximum Length Chain of Pairs in C++
- Maximum Length of Pair Chain in C++
- Maximum number of 3-person teams formed from two groups in C++
- Program to find maximum length of k ribbons of same length in Python
- Finding maximum length of common subarray in JavaScript
- Maximum length product of unique words in JavaScript
- Rod Cutting
- What is the maximum length of string in Python?
- Find maximum average subarray of k length in C++
- Maximum length of mountain in an array using JavaScript
- Maximum average of a specific length of subarray in JavaScript
- Advantages of CRM software for a sales person

Advertisements