Leetcode Problem 2936. Number of Equal Numbers Blocks

2936. Number of Equal Numbers Blocks

Leetcode Solutions

Exponential Search with Binary Search

  1. Initialize i to 0 and ans to 0.
  2. Get the size of the array using nums.size() and store it in size.
  3. While i is less than size: a. Retrieve the value at index i using nums.at(i) and store it in x. b. Initialize di to 2 (the initial step size for exponential search). c. Calculate the remaining size of the array from index i as size - i. d. While di is less than the remaining size and the value at index i + di is equal to x, double di. e. Perform a binary search between i + (di // 2) and min(i + di, size) to find the first index where the value is not x. f. Update i to the index found by the binary search. g. Increment ans by 1.
  4. Return ans as the number of blocks.
UML Thumbnail

Simple Binary Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...