Number of Equal Numbers Blocks - Problem

You are given a 0-indexed array of integers, nums. The following property holds for nums: All occurrences of a value are adjacent. In other words, if there are two indices i < j such that nums[i] == nums[j], then for every index k that i < k < j, nums[k] == nums[i].

Since nums is a very large array, you are given an instance of the class BigArray which has the following functions:

  • int at(long long index): Returns the value of nums[index]
  • void size(): Returns nums.length

Let's partition the array into maximal blocks such that each block contains equal values. Return the number of these blocks.

Note: If you want to test your solution using a custom test, behavior for tests with nums.length > 10 is undefined.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,1,3,3,2,2]
Output: 3
💡 Note: Three blocks: [1,1], [3,3], [2,2]. Each block contains equal adjacent values.
Example 2 — Single Elements
$ Input: nums = [1,2,3,4]
Output: 4
💡 Note: Four blocks: [1], [2], [3], [4]. Each element forms its own block.
Example 3 — All Same
$ Input: nums = [5,5,5,5,5]
Output: 1
💡 Note: One block: [5,5,5,5,5]. All elements are the same.

Constraints

  • 1 ≤ nums.length ≤ 1015
  • 1 ≤ nums[i] ≤ 100
  • All occurrences of each value are adjacent

Visualization

Tap to expand
Count Equal Numbers BlocksPartition array into maximal blocks of equal values113322Equal values are grouped togetherBoundary 1Boundary 2Block 1Block 2Block 3Output: 3 blocks
Understanding the Visualization
1
Input
Array with grouped equal values: [1,1,3,3,2,2]
2
Process
Find boundaries where values change
3
Output
Count of blocks: 3
Key Takeaway
🎯 Key Insight: Since equal values are adjacent, we only need to count where values change to find block boundaries
Asked in
Google 15 Facebook 8 Microsoft 6
12.4K Views
Medium Frequency
~15 min Avg. Time
187 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen