Maximum GCD-Sum of a Subarray
A hard-tier problem at 36% community acceptance, tagged with Array, Math, Binary Search. Reported in interviews at ThoughtWorks and 0 others.
Maximum GCD-Sum of a Subarray is the kind of problem that hits you in ThoughtWorks assessments and stops most candidates cold. You've got an array, you need to find a contiguous subarray, and the twist is you're maximizing the sum of elements multiplied by their GCD. The obvious greedy approach fails. The trick involves recognizing that GCD shrinks as you expand a subarray, so there are only O(log n) distinct GCD values to track. At 35% acceptance, this is a filtering problem. If this hits your live OA and you blank on the GCD-constraint optimization, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Maximum GCD-Sum of a Subarray"
Maximum GCD-Sum of a Subarray is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.
Get StealthCoderMost candidates start by iterating all subarrays and computing sum times GCD, which times out immediately. The insight is that as you expand a subarray by adding elements, the GCD either stays the same or drops. This means each element can only be the start of O(log n) subarrays with distinct GCD values. You iterate through the array, and for each position, track the previous GCD states. When you add a new element, compute the GCD with each existing state and prune duplicates. This drops the problem from O(n^3) to O(n log^2 n) or better. Number Theory and Binary Search become necessary when you need to reason about divisibility patterns and optimize comparisons. Most preparation materials skip this optimization entirely, which is why the acceptance rate stays low. If you haven't drilled the distinct-GCD pattern before, StealthCoder's real-time walkthrough bypasses the gap.
Pattern tags
You know the problem.
Make sure you actually pass it.
Maximum GCD-Sum of a Subarray recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum GCD-Sum of a Subarray interview FAQ
Why does the naive O(n^3) solution time out?+
You're computing GCD and sum for every single subarray. With n up to 10^5, that's 10^15 operations. The breakthrough is recognizing GCD shrinks, so there are only O(log n) unique GCD values per starting position, not O(n).
Is this still asked at companies like ThoughtWorks?+
ThoughtWorks is the only known company in the problem data. It appears in their harder assessment tier. If you're applying there, expect this or similar GCD-optimization problems in the technical screen.
What's the core trick?+
When you extend a subarray rightward, the GCD either stays the same or drops. This means for each starting position, you only track O(log n) states. Use a map or vector to store (GCD value, sum) pairs and prune when GCD repeats.
How do Array, Math, and Number Theory connect here?+
Array iteration is the frame. Number Theory (GCD, divisibility) is the constraint. Math/Binary Search optimize lookups and bounds. You're not just summing an array; you're exploiting properties of the GCD function to reduce state space.
What's a common pitfall?+
Forgetting that GCD can be computed incrementally as you extend the subarray. Many candidates recompute GCD from scratch for each subarray, which tanks time complexity. Cache it by tracking previous GCD values.
Want the actual problem statement? View "Maximum GCD-Sum of a Subarray" on LeetCode →