Given an integer array arr[] of size N. Let P is the product of all elements in the array. Return 1 if P is positive, 0 if P is equal to 0, and -1 if P is negative.
Examples:
Input: arr[] = [-1, -2, -3, -4, 3, 2, 1]
Output: 1
Explanation: The product of all elements in the array is 144 which is positive.Input: arr[] = [1, 9, 0, 5, -7]
Output: 0
Explanation: The product of all elements in the array is 0.
Approach: This can be solved with the following idea:
Just keep on the count of negative elements such that whenever it appears change the resulting sign.
Below are the steps involved in the implementation of the code:
- Iterate in array arr[].
- Whenever 0 appears in the array, the resulting ans will also be zero.
- If a negative sign appears change the resulting sign every time.
- Return the resulting sign,
Below is the implementation of the above approach:
// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
// Function to find sign of product of
// array elements
int getSign(vector<int>& arr, int N)
{
// Intialize product P by 1
int flag = 1;
// Iterate in array arr[]
for (int i = 0; i < N; i++) {
if (arr[i] == 0)
return 0;
if (arr[i] < 0)
flag = -flag;
}
// Return flag
return flag;
}
// Driver code
int main()
{
int N = 7;
vector<int> arr = { -1, -2, -3, -4, 3, 2, 1 };
// Function call
int ans = getSign(arr, N);
if (ans > 0)
cout << "Sign of product is +ve" << endl;
else if (ans < 0)
cout << "Sign of product is -ve" << endl;
else
cout << "NO sign" << endl;
return 0;
}
// java code for the above approach:
import java.util.*;
public class GFG {
// Function to find sign of product of array elements
public static int getSign(List<Integer> arr, int N) {
// Intialize product P by 1
int flag = 1;
// Iterate in array arr[]
for (int i = 0; i < N; i++) {
if (arr.get(i) == 0)
return 0;
if (arr.get(i) < 0)
flag = -flag;
}
// Return flag
return flag;
}
// Driver code
public static void main(String[] args) {
int N = 7;
List<Integer> arr = new ArrayList<>(Arrays.asList(-1, -2, -3, -4, 3, 2, 1));
// Function call
int ans = getSign(arr, N);
if (ans > 0)
System.out.println("Sign of product is +ve");
else if (ans < 0)
System.out.println("Sign of product is -ve");
else
System.out.println("NO sign");
}
}
# Function to find sign of product of
# array elements
def getSign(arr, N):
# Initialize product p by 1
flag = 1
# Iterate in array arr[]
for i in range(N):
if arr[i] == 0:
return 0
if arr[i] < 0:
flag = -flag
# Return flag
return flag
# Test case
N = 7
arr = [-1, -2, -3, -4, 3, 2, 1]
ans = getSign(arr, N)
if ans > 0:
print("Sign of product is +ve")
elif ans < 0:
print("Sign of product is -ve")
else:
print("NO sign")
using System;
using System.Collections.Generic;
namespace GFG
{
class Geek
{
// Function to find the sign of the
// product of array elements
static int GetSign(List<int> arr, int N)
{
// Initialize product P by 1
int flag = 1;
// Iterate through the array arr[]
for (int i = 0; i < N; i++)
{
if (arr[i] == 0)
return 0;
if (arr[i] < 0)
flag = -flag;
}
// Return flag
return flag;
}
static void Main(string[] args)
{
int N = 7;
List<int> arr = new List<int> { -1, -2, -3, -4, 3, 2, 1 };
// Function call
int ans = GetSign(arr, N);
if (ans > 0)
Console.WriteLine("Sign of product is +ve");
else if (ans < 0)
Console.WriteLine("Sign of product is -ve");
else
Console.WriteLine("No sign");
Console.ReadLine();
}
}
}
// Function to find sign of product of
// array elements
function getSign(arr, N) {
// Intialize product P by 1
let flag = 1;
// Iterate in array arr[]
for (let i = 0; i < N; i++) {
if (arr[i] === 0)
return 0;
if (arr[i] < 0)
flag = -flag;
}
// Return flag
return flag;
}
// Test case
const N = 7;
const arr = [ -1, -2, -3, -4, 3, 2, 1 ];
const ans = getSign(arr, N);
if (ans > 0)
console.log("Sign of product is +ve");
else if (ans < 0)
console.log("Sign of product is -ve");
else
console.log("NO sign");
Output
Sign of product is +ve
Time complexity: O(N)
Auxiliary Space: O(1)