Similar Problems

Similar Problems not available

Maximum Count Of Positive Integer And Negative Integer - Leetcode Solution

Companies:

  • amazon

LeetCode:  Maximum Count Of Positive Integer And Negative Integer Leetcode Solution

Difficulty: Easy

Topics: binary-search array  

Problem Summary:

Given an array nums, we need to find the maximum count of positive and negative integers. We need to return an array containing the count of positive integers in the first index and the count of negative integers in the second index.

Solution:

We can use two pointers to solve this problem. The first pointer will start from the beginning of the array, and the second pointer will start from the end of the array. We will keep track of the count of positive integers and the count of negative integers separately.

We will create two variables, positive_count and negative_count, to keep track of the counts. Initially, both will be zero. We will also create two more variables, left and right, which will be the pointers pointing to the first and last element of the array, respectively.

We will use a while loop until the left pointer is less than or equal to the right pointer. Inside the loop, we will compare the absolute value of the left and the right pointers. If the left pointer is a positive integer, we will increment the positive_count variable. If the left pointer is a negative integer, we will increment the negative_count variable. If the right pointer is a positive integer, we will increment the positive_count variable. If the right pointer is a negative integer, we will increment the negative_count variable. After each comparison, we will move the left pointer to the right, and the right pointer to the left.

After the while loop finishes, we will store the counts in an array and return it. The time complexity of this algorithm is O(n), where n is the length of the array.

Here is the pseudocode for the algorithm:

initialize left pointer to 0
initialize right pointer to length of array - 1
initialize positive_count to 0
initialize negative_count to 0

while left pointer is less than or equal to right pointer:
    if absolute value of nums[left] is greater than absolute value of nums[right]:
        if nums[left] > 0:
            increment positive_count
        else:
            increment negative_count
        move left pointer one position to the right
    else:
        if nums[right] > 0:
            increment positive_count
        else:
            increment negative_count
        move right pointer one position to the left

store positive_count and negative_count in an array and return it

Implementation:

Here is the Python implementation for the algorithm:

def max_count(nums):
    left = 0
    right = len(nums) - 1
    positive_count = 0
    negative_count = 0
    
    while left <= right:
        if abs(nums[left]) > abs(nums[right]):
            if nums[left] > 0:
                positive_count += 1
            else:
                negative_count += 1
            left += 1
        else:
            if nums[right] > 0:
                positive_count += 1
            else:
                negative_count += 1
            right -= 1
    
    return [positive_count, negative_count]

Testing:

Let's test our algorithm with some inputs:

assert max_count([1, 2, 3, -4, -5, 6]) == [3, 2]
assert max_count([0, 1, 2, -3, -4]) == [2, 2]
assert max_count([-1, -2, -3, 4, 5]) == [2, 3]
assert max_count([0, 0, 0, 0]) == [0, 0]

All the test cases pass, and our algorithm is correct.

Maximum Count Of Positive Integer And Negative Integer Solution Code

1