Count the Number of Arrays with K Matching Adjacent Elements
A hard-tier problem at 59% community acceptance, tagged with Math, Combinatorics. Reported in interviews at PhonePe and 0 others.
You get an array of length n and a threshold k. Count how many arrays exist where exactly k pairs of adjacent elements are equal. PhonePe has asked this. It's a combinatorics problem disguised as a counting problem, and most candidates miss the structure on first read. The acceptance rate sits at 58.6%, which sounds okay until you realize half the people who attempt it get it wrong under time pressure. This is the kind of problem where the naive approach gets you nowhere, and the mathematical insight is the only path. If this hits your live OA and you blank on the combinatorial formula, StealthCoder solves it in seconds, invisible to the proctor.
Companies that ask "Count the Number of Arrays with K Matching Adjacent Elements"
Count the Number of Arrays with K Matching Adjacent Elements 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. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE.
Get StealthCoderThe trick is recognizing this isn't about brute force simulation. You need to count valid arrangements using combinatorics. Think of it as placing k 'match' events among n-1 possible adjacent pairs, then accounting for how many distinct values satisfy those constraints. The pitfall: trying to generate or iterate through arrays. The insight: most people conflate 'counting arrays' with 'finding one array.' You're computing a formula, not building structures. This lands squarely in Math and Combinatorics because the algorithmic work is just translating the combinatorial logic into code. Once you see the pattern (choosing positions for matches, then distributing values), the implementation becomes straightforward. Under exam pressure, the pattern doesn't surface easily. That's where StealthCoder becomes your safety net, surfacing the working formula the moment you hit the wall.
Pattern tags
You know the problem.
Make sure you actually pass it.
Count the Number of Arrays with K Matching Adjacent Elements 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. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count the Number of Arrays with K Matching Adjacent Elements interview FAQ
Is this really asked at companies like PhonePe?+
Yes. PhonePe's data is in the record. It's a Hard problem, which tracks with companies hiring for strong math and backend roles. It's not a common warm-up, so if you see it, you're in a screening or technical round where they expect depth.
What's the trick I'm missing if brute force doesn't work?+
You're not generating arrays; you're counting them. The trick is combinatorics: choose which k of the n-1 adjacent pairs have matching elements, then count value assignments. Brute force enumeration will time out on any reasonable n.
Does this require deep combinatorics knowledge?+
Not PhD-level, but you do need to understand combinations, factorials, and how to avoid double-counting. The Math and Combinatorics tags aren't ornamental. If you haven't practiced these patterns, the problem feels alien on first contact.
How does this differ from other counting problems?+
Most counting problems are about permutations or subsets. This one adds a structural constraint: adjacency. You're not just choosing elements; you're arranging matches along a sequence, which changes the counting logic significantly.
If I freeze on this during the OA, what's my move?+
Ask for a hint or clarify the math with the interviewer if it's a live call. If you're flying solo on a timed assessment, StealthCoder reads the problem and surfaces the working combinatorial formula in real time, invisible to the proctor, so you unblock without panic.
Want the actual problem statement? View "Count the Number of Arrays with K Matching Adjacent Elements" on LeetCode →