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++
// 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) { int cntpositive = 0, cntnegative = 0; // traverse the array to count // occurrences of positive and negative numbers for ( int i = 0; i < size; i++) { if ( arr[i] > 0 ) cntpositive++; else if ( arr[i] < 0 ) cntnegative++; } // return maximum among positive // and negative count return max(cntpositive, cntnegative); } // 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
import java.util.Arrays; public class Main { // Function to find the maximum of the // count of positive or negative elements static int findMaximum( int [] arr) { int cntPositive = 0 , cntNegative = 0 ; // traverse the array to count // occurrences of positive and negative numbers for ( int num : arr) { if (num > 0 ) cntPositive++; else if (num < 0 ) cntNegative++; } // return maximum among positive // and negative count return Math.max(cntPositive, cntNegative); } // Driver Code public static void main(String[] args) { int [] arr = { - 9 , - 7 , - 4 , 1 , 5 , 8 , 9 }; // Function call System.out.println(findMaximum(arr)); } } |
Python3
# Function to find the maximum of the # count of positive or negative elements def find_maximum(arr): cnt_positive = 0 cnt_negative = 0 # Traverse the array to count occurrences of positive and negative numbers for i in arr: if i > 0 : cnt_positive + = 1 elif i < 0 : cnt_negative + = 1 # Return the maximum among positive and negative count return max (cnt_positive, cnt_negative) def main(): arr = [ - 9 , - 7 , - 4 , 1 , 5 , 8 , 9 ] print (find_maximum(arr)) if __name__ = = "__main__" : main() |
4
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++
// 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> |
4
Time Complexity: O(log N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!