Find the maximum among the count of positive or negative integers in the array
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:
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
Naive approach:
This approach to solve the problem is to count the occurrences of positive and negative numbers by traversing the array once. Finally, return the maximum of the counts of positives and negatives.
Algorithm:
- Initialize variables cntpositive and cntnegative as 0.
- Traverse the array from 0 to size-1:
a. If the current element arr[i] is greater than 0, increment cntpositive.
b. Else if the current element arr[i] is less than 0, increment cntnegative.
- Compute the maximum of cntpositive and cntnegative using the max() function.
- Return the maximum count.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
int findMaximum( int arr[], int size) {
int cntpositive = 0, cntnegative = 0;
for ( int i = 0; i < size; i++) {
if ( arr[i] > 0 )
cntpositive++;
else if ( arr[i] < 0 )
cntnegative++;
}
return max(cntpositive, cntnegative);
}
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
import java.util.Arrays;
public class Main {
static int findMaximum( int [] arr) {
int cntPositive = 0 , cntNegative = 0 ;
for ( int num : arr) {
if (num > 0 )
cntPositive++;
else if (num < 0 )
cntNegative++;
}
return Math.max(cntPositive, cntNegative);
}
public static void main(String[] args) {
int [] arr = { - 9 , - 7 , - 4 , 1 , 5 , 8 , 9 };
System.out.println(findMaximum(arr));
}
}
|
Python3
def find_maximum(arr):
cnt_positive = 0
cnt_negative = 0
for i in arr:
if i > 0 :
cnt_positive + = 1
elif i < 0 :
cnt_negative + = 1
return max (cnt_positive, cnt_negative)
def main():
arr = [ - 9 , - 7 , - 4 , 1 , 5 , 8 , 9 ]
print (find_maximum(arr))
if __name__ = = "__main__" :
main()
|
C#
using System;
public class GFG {
public static int FindMaximum( int [] arr, int size)
{
int cntPositive = 0, cntNegative = 0;
for ( int i = 0; i < size; i++) {
if (arr[i] > 0)
cntPositive++;
else if (arr[i] < 0)
cntNegative++;
}
return Math.Max(cntPositive, cntNegative);
}
public static void Main( string [] args)
{
int [] arr = { -9, -7, -4, 1, 5, 8, 9 };
int N = arr.Length;
Console.WriteLine(FindMaximum(arr, N));
}
}
|
Javascript
function findMaximum(arr) {
let cntPositive = 0, cntNegative = 0;
for (let num of arr) {
if (num > 0)
cntPositive++;
else if (num < 0)
cntNegative++;
}
return Math.max(cntPositive, cntNegative);
}
let arr = [-9, -7, -4, 1, 5, 8, 9];
console.log(findMaximum(arr));
|
Time Complexity: O(N) as we are traversing entire array once. Here, N is size of input array.
Auxiliary Space: O(1) as no extra space has been used.
Efficient 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++
#include "bits/stdc++.h"
using namespace std;
int findMaximum( int arr[], int size)
{
int i = 0, j = size - 1, mid;
while (i <= j) {
mid = i + (j - i) / 2;
if (arr[mid] < 0)
i = mid + 1;
else if (arr[mid] > 0)
j = mid - 1;
}
return max(i, size - i);
}
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
import java.io.*;
public class GFG {
static int findMaximum( int arr[], int size)
{
int i = 0 , j = size - 1 , mid;
while (i <= j) {
mid = i + (j - i) / 2 ;
if (arr[mid] < 0 )
i = mid + 1 ;
else if (arr[mid] > 0 )
j = mid - 1 ;
}
return Math.max(i, size - i);
}
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));
}
}
|
Python3
def findMaximum(arr, size):
i = 0
j = size - 1
while (i < = j):
mid = i + (j - i) / / 2
if (arr[mid] < 0 ):
i = mid + 1
elif (arr[mid] > 0 ):
j = mid - 1
return max (i, size - i)
if __name__ = = "__main__" :
arr = [ - 9 , - 7 , - 4 , 1 , 5 , 8 , 9 ]
N = len (arr)
print (findMaximum(arr, N))
|
C#
using System;
public class GFG
{
static int findMaximum( int []arr, int size)
{
int i = 0, j = size - 1, mid;
while (i <= j) {
mid = i + (j - i) / 2;
if (arr[mid] < 0)
i = mid + 1;
else if (arr[mid] > 0)
j = mid - 1;
}
return Math.Max(i, size - i);
}
public static void Main ( string [] args)
{
int []arr = { -9, -7, -4, 1, 5, 8, 9 };
int N = arr.Length;
Console.WriteLine(findMaximum(arr, N));
}
}
|
Javascript
<script>
function findMaximum(arr, size)
{
let i = 0,
j = size - 1,
mid;
while (i <= j)
{
mid = i + Math.floor((j - i) / 2);
if (arr[mid] < 0) i = mid + 1;
else if (arr[mid] > 0) j = mid - 1;
}
return Math.max(i, size - i);
}
let arr = [-9, -7, -4, 1, 5, 8, 9];
let N = arr.length;
document.write(findMaximum(arr, N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
06 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...