Find the only positive or only negative number in the given Array
Given an array arr[] of either entirely positive integers or entirely negative integers except for one number. The task is to find that number.
Examples:
Input: arr[] = {3, 5, 2, 8, -7, 6, 9}
Output: -7
Explanation: Except -7 all the numbers in arr[] are posisitve integers.Input: arr[] = {-3, 5, -9}
Output: 5
Approach: The given problem can be solved by just comparing the first three numbers of arr[]. After that apply Linear Search and find the number. Follow the steps below to solve the problem.
- If the size of arr[] is smaller than 3, then return 0.
- Initialize the variables Cp and Cn.
- Where Cp = Count of positive integers and Cn = Count of negative integers.
- Iterate over the first 3 element
- If (arr[i]>0), Increment Cp by 1.
- Else Increment Cn by 1.
- Cp can be 2 or 3 and similarly, Cn can also either 2 or 3.
- If Cp<Cn, then the single element is positive, apply linear search and find it.
- If Cn<Cp, then the single element is negative, apply linear search and find it.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <iostream>using namespace std;// Function to return the single elementint find(int* a, int N){ // Size can not be smaller than 3 if (N < 3) return 0; // Initialize the variable int i, Cp = 0, Cn = 0; // Check the single element is // positive or negative for (i = 0; i < 3; i++) { if (a[i] > 0) Cp++; else Cn++; } // Check for positive single element if (Cp < Cn) { for (i = 0; i < N; i++) if (a[i] > 0) return a[i]; } // Check for negative single element else { for (i = 0; i < N; i++) if (a[i] < 0) return a[i]; }}// Driver Codeint main(){ int arr[] = { 3, 5, 2, 8, -7, 6, 9 }; int N = sizeof(arr) / sizeof(arr[0]); cout << find(arr, N);} |
Java
// Java program for the above approachimport java.util.*;public class GFG {// Function to return the single elementstatic int find(int []a, int N){ // Size can not be smaller than 3 if (N < 3) return 0; // Initialize the variable int i, Cp = 0, Cn = 0; // Check the single element is // positive or negative for (i = 0; i < 3; i++) { if (a[i] > 0) Cp++; else Cn++; } // Check for positive single element if (Cp < Cn) { for (i = 0; i < N; i++) if (a[i] > 0) return a[i]; } // Check for negative single element else { for (i = 0; i < N; i++) if (a[i] < 0) break; } return a[i];}// Driver Codepublic static void main(String args[]){ int []arr = { 3, 5, 2, 8, -7, 6, 9 }; int N = arr.length; System.out.println(find(arr, N));}}// This code is contributed by Samim Hossain Mondal. |
Python3
# python program for the above approach# Function to return the single elementdef find(a, N): # Size can not be smaller than 3 if (N < 3): return 0 # Initialize the variable Cp = 0 Cn = 0 # Check the single element is # positive or negative for i in range(0, 3): if (a[i] > 0): Cp += 1 else: Cn += 1 # Check for positive single element if (Cp < Cn): for i in range(0, N): if (a[i] > 0): return a[i] # Check for negative single element else: for i in range(0, N): if (a[i] < 0): return a[i]# Driver Codeif __name__ == "__main__": arr = [3, 5, 2, 8, -7, 6, 9] N = len(arr) print(find(arr, N)) # This code is contributed by rakeshsahni |
C#
// C# program for the above approachusing System;class GFG {// Function to return the single elementstatic int find(int []a, int N){ // Size can not be smaller than 3 if (N < 3) return 0; // Initialize the variable int i, Cp = 0, Cn = 0; // Check the single element is // positive or negative for (i = 0; i < 3; i++) { if (a[i] > 0) Cp++; else Cn++; } // Check for positive single element if (Cp < Cn) { for (i = 0; i < N; i++) if (a[i] > 0) return a[i]; } // Check for negative single element else { for (i = 0; i < N; i++) if (a[i] < 0) break; } return a[i];}// Driver Codepublic static void Main(){ int []arr = { 3, 5, 2, 8, -7, 6, 9 }; int N = arr.Length; Console.Write(find(arr, N));}}// This code is contributed by Samim Hossain Mondal. |
Javascript
<script>// Javascript program for the above approach// Function to return the single elementfunction find(a, N){ // Size can not be smaller than 3 if (N < 3) return 0; // Initialize the variable let i, Cp = 0, Cn = 0; // Check the single element is // positive or negative for (i = 0; i < 3; i++) { if (a[i] > 0) Cp++; else Cn++; } // Check for positive single element if (Cp < Cn) { for (i = 0; i < N; i++) if (a[i] > 0) return a[i]; } // Check for negative single element else { for (i = 0; i < N; i++) if (a[i] < 0) return a[i]; }}// Driver Codelet arr = [ 3, 5, 2, 8, -7, 6, 9 ];let N = arr.length;document.write(find(arr, N));// This code is contributed Samim Hossain Mondal.</script> |
Output
-7
Time Complexity: O(N)
Auxiliary Space: O(1)



