Count Nice Pairs in an Array
A medium-tier problem at 49% community acceptance, tagged with Array, Hash Table, Math. Reported in interviews at Block and 1 others.
You're in an Uber or Block OA and land on Count Nice Pairs. It's medium difficulty with a 48% acceptance rate, which means half the people who see it blank or timeout. The problem sounds innocent: pairs (i, j) where i < j and arr[i] - i == arr[j] - j. Sounds like nested loops, right. That's the trap. The trick lives in the math, not brute force. StealthCoder catches this pattern instantly if you freeze on the live assessment.
Companies that ask "Count Nice Pairs in an Array"
Count Nice Pairs in an Array 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 a senior engineer who knows the OA is theater. This is the script.
Get StealthCoderThe mistake is implementing O(n^2) nested iteration. The real solution rearranges the equation: if arr[i] - i == arr[j] - j, you're actually counting how many times the same transformed value appears. Compute arr[k] - k for every element, dump the results into a hash table, and count pairs from the frequency map. This collapses the problem to O(n) time. The insight is recognizing you don't need to compare pairs directly. When the index arithmetic doesn't jump out immediately, most candidates burn time on indexing bugs. StealthCoder surfaces the hash-map reformulation in seconds, undetectable during screen share.
Pattern tags
You know the problem.
Make sure you actually pass it.
Count Nice Pairs in an Array recycles across companies for a reason. It's medium-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 a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Nice Pairs in an Array interview FAQ
Is this still asked at Block and Uber?+
Yes. Both companies appear in the data for this problem. It's a medium-difficulty favorite for testing whether candidates recognize transformation-to-hash-table patterns. Expect it in technical screens, not always first round.
What's the actual trick everyone misses?+
The equation rearranges to a count problem, not a comparison problem. Compute arr[i] - i for each element, use a hash table to track frequencies, then calculate pairs from the counts. Most people get stuck on nested loops and run out of time.
How hard is this really for a medium?+
48% acceptance is below average for medium problems. It's not complex code, but the leap from nested loops to hash-table counting isn't obvious. You need to see the algebraic transformation first, or you're lost.
What do I need to know before the OA?+
Understand that arithmetic identity arr[i] - i == arr[j] - j means you can group by the transformed value. Know how to build a hash table and iterate through it to count pair combinations. That's 90% of the win.
Will knowing the Array and Hash Table topics help me?+
Yes. This problem is pure hash table application disguised as an array counting problem. If you're solid on frequency maps and identity transformations, you'll spot the pattern. Otherwise it reads like a brute-force trap.
Want the actual problem statement? View "Count Nice Pairs in an Array" on LeetCode →