Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find the maximum among the count of positive or negative integers in the array

  • Difficulty Level : Hard
  • Last Updated : 10 Nov, 2021

Given a sorted array arr[] consisting of N integers, the task is to find the maximum among the count of positive or negative integers in the array arr[].

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = {-9, -7, -4, 1, 5, 8, 9}
Output: 4
Explanation:
The count of positive numbers is 4 and the count of negative numbers is 3. So, the maximum among 4, 3 is 4. Therefore, print 4.



Input: arr[] = {-8, -6, 10, 15}
Output: 2

Approach: The given problem can be solved by using Binary Search, the idea is to find the first index whose value is positive and then print the maximum of idx and (N – idx) as the result. Follow the steps below to solve the given problem:

  • Initialize two variables, say low as 0 and high as (N – 1).
  • Perform the Binary Search on the given array arr[] by iterating until low <= high and follow the below steps:
    • Find the value of mid as (low + high) / 2.
    • If the value of arr[mid] is positive, then skip the right half by updating the value of high to (mid – 1). Otherwise, skip the left half by updating the value of low to (mid + 1).
  • After completing the above steps, print the maximum of low and (N – low) as the result.

 Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
// Function to find the maximum of the
// count of positive or negative elements
int findMaximum(int arr[], int size)
{
 
    // Initialize the pointers
    int i = 0, j = size - 1, mid;
 
    while (i <= j) {
 
        // Find the value of mid
        mid = i + (j - i) / 2;
 
        // If element is negative then
        // ignore the left half
        if (arr[mid] < 0)
            i = mid + 1;
 
        // If element is positive then
        // ignore the right half
        else if (arr[mid] > 0)
            j = mid - 1;
    }
 
    // Return maximum among the count
    // of positive & negative element
    return max(i, size - i);
}
 
// Driver Code
int main()
{
    int arr[] = { -9, -7, -4, 1, 5, 8, 9 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << findMaximum(arr, N);
 
    return 0;
}

Java




// Java program for the above approach
import java.io.*;
 
public class GFG {
     
    // Function to find the maximum of the
    // count of positive or negative elements
    static int findMaximum(int arr[], int size)
    {
     
        // Initialize the pointers
        int i = 0, j = size - 1, mid;
     
        while (i <= j) {
     
            // Find the value of mid
            mid = i + (j - i) / 2;
     
            // If element is negative then
            // ignore the left half
            if (arr[mid] < 0)
                i = mid + 1;
     
            // If element is positive then
            // ignore the right half
            else if (arr[mid] > 0)
                j = mid - 1;
        }
     
        // Return maximum among the count
        // of positive & negative element
        return Math.max(i, size - i);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr[] = { -9, -7, -4, 1, 5, 8, 9 };
        int N = arr.length;
     
        System.out.println(findMaximum(arr, N));
 
    }
 
}
 
// This code is contributed by AnkThon

Python3




# python program for the above approach
 
# Function to find the maximum of the
# count of positive or negative elements
def findMaximum(arr, size):
 
    # Initialize the pointers
    i = 0
    j = size - 1
 
    while (i <= j):
 
         # Find the value of mid
        mid = i + (j - i) // 2
 
        # If element is negative then
        # ignore the left half
        if (arr[mid] < 0):
            i = mid + 1
 
            # If element is positive then
            # ignore the right half
        elif (arr[mid] > 0):
            j = mid - 1
 
        # Return maximum among the count
        # of positive & negative element
    return max(i, size - i)
 
# Driver Code
if __name__ == "__main__":
 
    arr = [-9, -7, -4, 1, 5, 8, 9]
    N = len(arr)
 
    print(findMaximum(arr, N))
 
    # This code is contributed by rakeshsahni

C#




// C# program for the above approach
using System;
 
public class GFG
{
     
    // Function to find the maximum of the
    // count of positive or negative elements
    static int findMaximum(int []arr, int size)
    {
     
        // Initialize the pointers
        int i = 0, j = size - 1, mid;
     
        while (i <= j) {
     
            // Find the value of mid
            mid = i + (j - i) / 2;
     
            // If element is negative then
            // ignore the left half
            if (arr[mid] < 0)
                i = mid + 1;
     
            // If element is positive then
            // ignore the right half
            else if (arr[mid] > 0)
                j = mid - 1;
        }
     
        // Return maximum among the count
        // of positive & negative element
        return Math.Max(i, size - i);
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
        int []arr = { -9, -7, -4, 1, 5, 8, 9 };
        int N = arr.Length;
     
        Console.WriteLine(findMaximum(arr, N));
    }
}
 
// This code is contributed by AnkThon

Javascript




<script>
// Javascript program for the above approach
 
// Function to find the maximum of the
// count of positive or negative elements
function findMaximum(arr, size)
{
 
  // Initialize the pointers
  let i = 0,
    j = size - 1,
    mid;
 
  while (i <= j)
  {
   
    // Find the value of mid
    mid = i + Math.floor((j - i) / 2);
 
    // If element is negative then
    // ignore the left half
    if (arr[mid] < 0) i = mid + 1;
     
    // If element is positive then
    // ignore the right half
    else if (arr[mid] > 0) j = mid - 1;
  }
 
  // Return maximum among the count
  // of positive & negative element
  return Math.max(i, size - i);
}
 
// Driver Code
let arr = [-9, -7, -4, 1, 5, 8, 9];
let N = arr.length;
 
document.write(findMaximum(arr, N));
 
// This code is contributed by saurabh_jaiswal.
</script>
Output: 
4

 

Time Complexity: O(log N)
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :