menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Data Science News

>

2529. Maxi...
source image

Dev

1M

read

247

img
dot

Image Credit: Dev

2529. Maximum Count of Positive Integer and Negative Integer

  • Given an array nums sorted in non-decreasing order, return the maximum between the number of positive integers and the number of negative integers.
  • Binary Search for First Non-Negative Element: We use binary search to find the first index where the element is greater than or equal to zero. This index helps us determine the count of negative numbers, as all elements before this index are negative.
  • Binary Search for First Positive Element: Similarly, we use binary search to find the first index where the element is strictly positive. This index helps us determine the count of positive numbers, as all elements from this index to the end of the array are positive.
  • Efficiency: Both binary searches operate in O(log n) time, making the solution efficient even for the upper constraint of 2000 elements. The overall time complexity is O(log n) due to the two binary searches, and the space complexity is O(1) as we use only a few extra variables.

Read Full Article

like

14 Likes

For uninterrupted reading, download the app