Leetcode Problem 2148. Count Elements With Strictly Smaller and Greater Elements

2148. Count Elements With Strictly Smaller and Greater Elements

Leetcode Solutions

Single Traversal with Min/Max Tracking

  1. Initialize mini and maxi to the first element of the array.
  2. Initialize cntmin and cntmax to 1, representing the count of the minimum and maximum elements respectively.
  3. Iterate through the array starting from the second element. a. If the current element is less than mini, update mini and reset cntmin to 1. b. If the current element is equal to mini, increment cntmin. c. If the current element is greater than maxi, update maxi and reset cntmax to 1. d. If the current element is equal to maxi, increment cntmax.
  4. If mini is equal to maxi, return 0 since all elements are the same.
  5. Return the total number of elements minus the count of minimum and maximum elements (n - (cntmin + cntmax)).
UML Thumbnail

Sorting and Counting

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...