# 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);` `    ``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

 ``
Output:
`4`

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

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.6289955,"math_prob":0.97525036,"size":5611,"snap":"2021-43-2021-49","text_gpt3_token_len":1683,"char_repetition_ratio":0.14660247,"word_repetition_ratio":0.4520906,"special_character_ratio":0.33666015,"punctuation_ratio":0.13978495,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.999116,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-01T03:37:37Z\",\"WARC-Record-ID\":\"<urn:uuid:a9ec946f-06bf-445c-a80b-74e0c11234d8>\",\"Content-Length\":\"157015\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1c41ac7a-efba-4e5f-a2fd-43a1dcd8291d>\",\"WARC-Concurrent-To\":\"<urn:uuid:f3b189c4-3b4b-4cd5-bab2-03dab185719c>\",\"WARC-IP-Address\":\"23.207.202.39\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/find-the-maximum-among-the-count-of-positive-or-negative-integers-in-the-array/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:7F7E5OHE43MLOQYATQD36ZTLCFC4PCBI\",\"WARC-Block-Digest\":\"sha1:7BZXNYSNBHWJNO6SOWBKQPQHEIR6L5NP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964359082.78_warc_CC-MAIN-20211201022332-20211201052332-00380.warc.gz\"}"}