Leetcode Problem 2529. Maximum Count of Positive Integer and Negative Integer

2529. Maximum Count of Positive Integer and Negative Integer

Leetcode Solutions

Binary Search Approach

  1. Initialize left to 0 and right to the length of nums minus 1.
  2. Perform a binary search:
    • While left is less than or equal to right:
      • Calculate mid as the average of left and right (integer division).
      • If nums[mid] is negative, set left to mid + 1.
      • Otherwise, set right to mid - 1.
  3. After the loop, left will be at the first non-negative number's index.
  4. Calculate the count of negative numbers as left.
  5. Calculate the count of positive numbers as the length of nums minus left.
  6. Return the maximum of the count of negative and positive numbers.
UML Thumbnail

Two Pointer Approach

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...