Hiking Club Fastest Pace
Updated
Problem statement
A hiking club takes sign-ups in groups. Each group is given as groups[i] = [pace1, pace2]:
- If
pace2 > 0, the group is two friends who insist on hiking together. Their paces arepace1andpace2. - If
pace2 == 0, the group is one hiker who hikes alone, with pacepace1.
A larger pace means a faster hiker. The guide takes whole groups only, so the two friends of a pair are taken together or not at all. The guide needs at least m hikers, and the hike moves at the pace of its slowest hiker.
Choose the groups so that the hike is as fast as possible, and return that pace. If fewer than m hikers signed up in total, return -1.
Function
fastestHikePace(groups: int[][], m: int) → intExamples
Example 1
groups = [[5,0]]m = 1return = 5The only group is one hiker with pace 5. Taking that group gives 1 hiker, which is enough, so the hike moves at pace 5.
Example 2
groups = [[6,4],[5,0]]m = 2return = 4The pair moves at pace 4, the slower of 6 and 4. The solo hiker with pace 5 is only 1 hiker, which is not enough on its own. Taking the pair gives 2 hikers at pace 4, and taking both groups gives 3 hikers, still at pace 4. So the answer is 4.
Example 3
groups = [[9,2],[7,0],[6,0]]m = 2return = 6Take the two solo hikers with paces 7 and 6. That is 2 hikers, and the hike moves at pace 6. Any choice that includes the pair also brings its slower member with pace 2, so that hike would move at pace 2. Taking only the hikers with paces 9 and 7 would give pace 7, but that splits the pair, which is not allowed. So the answer is 6.
Constraints
1 <= groups.length <= 10^4groups[i].length == 20 <= groups[i][j] <= 10^9groups[i][0] >= 11 <= m <= 2 * 10^4
More Infosys Online Assessment questions
- Lowest Common Ancestor with a Changing RootOA · Seen Nov 2026
- Minimum Leaf Path Sum over All RootsOA · Seen Nov 2026
- Split Array to Minimize the Largest SumOA · Seen Nov 2026
- Cherry PickupOA · Seen Nov 2026
- Lexicographically Smallest Grid Path with One Letter ChangeOA · Seen Nov 2026
- Minimum Cost to Supply Water with Wells and PipesOA · Seen Nov 2026
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3OA · Seen Nov 2026
- Minimum Rounds to Complete All TasksOA · Seen Nov 2026
- Lift Total TravelOA · Seen Nov 2026
- Count Equal-Distinct Character PartitionsOA · Seen Oct 2026
Similar questions at other companies
- Find Minimum TimeAlphaSense · OA
- Find Minimum TimeBlackRock · OA
- Maximum Easy Tasks After RearrangingIBM · OA
- Minimum Cost K-Capable ModelsMicrosoft · OA
- Minimum Cost of Every K-Capable Model SetTikTok · OA
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
- Maximize Revenue from Decreasing InventoryChubb · OA
- Minimum Distinct Prefix CostGoldman Sachs · OA
- Maximize Minimum Deployment DifficultyJP Morgan Chase · OA
- Latest Arrival to Catch a BusAutodesk · OA