Bishu and Soldiers
Pattern: Binary search on sorted arrays
Sort the powers once and build prefix sums. For each query, the upper bound of Bishu's power is how many soldiers he beats, and the prefix sum at that point is their total power.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
There are soldiers with given powers. Bishu fights them in several rounds, each with his own power m. In a round he
beats every soldier whose power is at most m. For each round, return two numbers: how many soldiers he beats, and the
sum of their powers. For example, powers [1, 2, 3, 4, 5, 6, 7] with rounds [3, 10, 2] give [[3, 6], [7, 28], [2, 3]].
This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name
bishu and the input as two arrays are this guide's own. Each round is separate: soldiers are not used up.
Think first
- With the powers sorted, which soldiers does Bishu beat in one round?
- How do you count them fast?
- How do you get the sum of the first
kvalues in O(1)?
Hints
Brute force, and why it fails
For each round, scan every soldier: O(n × q). Sorting once and using prefix sums makes each round O(log n).
The key insight
Many questions about "everything at most m" on the same data become one sort plus a binary search per question.
Prefix sums turn "sum of a prefix" into one lookup.
Dry run
powers = [1, 2, 3, 4, 5, 6, 7], already sorted. Prefix sums are 0, 1, 3, 6, 10, 15, 21, 28.
The plan
sort powers
prefix[0] = 0; prefix[i + 1] = prefix[i] + powers[i]
for each m in queries:
k = upperBound(powers, m)
add [k, prefix[k]] to answers
return answers
Complexity
- Time
O((n + q) log n)- Space
O(n)
Sorting is O(n log n), and each of the q rounds is one binary search. The prefix array takes O(n).
Edge cases
- Bishu weaker than everyone:
[0, 0]. - Repeated powers equal to
m: all of them count, which is why it is the upper bound. - Large inputs: keep the sums in a 64-bit number when powers and counts are large.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public List<int[]> bishu(int[] powers, int[] queries) {
int[] p = powers.clone();
Arrays.sort(p);
long[] prefix = new long[p.length + 1]; // prefix[k] = sum of the k smallest powers
for (int i = 0; i < p.length; i++) prefix[i + 1] = prefix[i] + p[i];
List<int[]> out = new ArrayList<>();
for (int m : queries) {
int lo = 0, hi = p.length; // first index with p[i] > m = how many are <= m
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (p[mid] > m) hi = mid;
else lo = mid + 1;
}
out.add(new int[]{lo, (int) prefix[lo]});
}
return out;
}
}C++
class Solution {
public:
vector<vector<int>> bishu(vector<int>& powers, vector<int>& queries) {
vector<int> p = powers;
sort(p.begin(), p.end());
vector<long long> prefix(p.size() + 1, 0); // prefix[k] = sum of the k smallest powers
for (size_t i = 0; i < p.size(); i++) prefix[i + 1] = prefix[i] + p[i];
vector<vector<int>> out;
for (int m : queries) {
int lo = 0, hi = static_cast<int>(p.size()); // first index with p[i] > m = how many are <= m
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (p[mid] > m) hi = mid;
else lo = mid + 1;
}
out.push_back({lo, static_cast<int>(prefix[lo])});
}
return out;
}
};Python
class Solution:
def bishu(self, powers: list[int], queries: list[int]) -> list[list[int]]:
p = sorted(powers)
prefix = [0] # prefix[k] = sum of the k smallest powers
for v in p:
prefix.append(prefix[-1] + v)
out = []
for m in queries:
lo, hi = 0, len(p) # first index with p[i] > m = how many are <= m
while lo < hi:
mid = (lo + hi) // 2
if p[mid] > m:
hi = mid
else:
lo = mid + 1
out.append([lo, prefix[lo]])
return outCommon mistakes
- Using the lower bound, which leaves out soldiers whose power equals Bishu's.
- Rebuilding the sum for every round.
- Sorting the input array in place when the caller still needs it in its original order.