Minimum Operations to Make Every Array Element One
Reported by candidates from Otter.ai's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Otter.ai reported this one in October 2026, and the detail that matters is in Example 2: every element shares a gcd of 2, so the answer is -1 and no operation can ever create a 1. That's the whole problem in miniature. It's an array problem with gcd math underneath, and the array is capped at 50 elements, so brute force over subarrays is fine. If you blank on the counting formula during the live OA, StealthCoder runs invisibly as a safety net and reads the problem for you. But the logic is short enough to own before you sit down.
The problem
Given an array of positive integers, one operation chooses two adjacent elements and replaces either one of them with their greatest common divisor. Return the minimum number of operations needed to make every element equal to 1, or -1 when it is impossible. Function minOperationsToOne(nums: int[]) → int Examples Example 1 nums = [2,6,3,4] return = 4 The shortest adjacent subarray with gcd 1 has length 3; creating one 1 takes two operations and spreading it to the other three positions takes three more, but one position was already in the subarray, for four total. Example 2 nums = [2,10,6,14] return = -1 The gcd of every element is 2, so no operation can create a 1. Constraints 2 <= nums.length <= 50. 1 <= nums[i] <= 10^6.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick has three cases. First, if the array already has k ones, the answer is n - k, since each non-one element needs one operation to absorb a neighboring 1. Second, if the gcd of the whole array is greater than 1, return -1. Third, find the shortest subarray whose gcd is 1, with length L. Creating the first 1 costs L - 1 operations, then spreading it across the other n - 1 positions costs n - 1 more. Total is (L - 1) + (n - 1). The common pitfall is forgetting the existing-ones case, or double counting the position that already became 1. With n at most 50, an O(n^2) scan with a running gcd is plenty. Don't overthink it. Compute the gcd incrementally from each start index and break as soon as it hits 1. StealthCoder is the hedge if the formula slips under pressure.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Minimum Operations to Make Every Array Element One cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Otter.ai's OA.
Otter.ai reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Operations to Make Every Array Element One FAQ
What's the trick to Minimum Operations to Make Every Array Element One?+
Find the shortest contiguous subarray with gcd 1, length L. Making one 1 costs L - 1 operations. Spreading it to the remaining n - 1 positions costs n - 1. Add them. If the array already contains ones, skip all that and return n minus the count of ones.
When is the answer -1?+
When the gcd of the entire array is greater than 1. Any gcd of a subarray is a multiple of the overall gcd, so no subarray can ever reach 1. Example 2 shows this: every value is divisible by 2, so a 1 can't be created.
How hard is this really for an Otter.ai OA?+
Easy to medium. The constraints are tiny, n up to 50, so an O(n^2) scan with a running gcd passes. The difficulty is spotting the three cases and getting the operation count right, not performance or fancy data structures.
What are the edge cases I should test?+
Test an array that already has multiple ones, an array with a single 1, one where the whole array gcd is above 1, and one where the only gcd-1 subarray is the whole array. Also check n equals 2 with two coprime values.
How do I prepare for this in 48 hours?+
Write the gcd-of-subarray loop from scratch twice. Practice the three-case structure: existing ones, impossible, shortest gcd-1 window. Use a built-in or Euclidean gcd, and break the inner loop early once the running gcd reaches 1. That covers the whole problem.