Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Non-Negative Product CodeChef Solution

Problem – Non-Negative Product CodeChef Solution

Alice has an array of N integers — A1​,A2​,…,AN​. She wants the product of all the elements of the array to be a non-negative integer. That is, it can be either 0 or positive. But she doesn’t want it to be negative.

To do this, she is willing to remove some elements of the array. Determine the minimum number of elements that she will have to remove to make the product of the array’s elements non-negative.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
    • The first line of each test case contains a single integer N — the number of elements in the array originally.
    • The next line contains N space-separated integers — A1​,A2​,…,AN​, which are the original array elements.

Output Format

For each test case, output on a new line the minimum number of elements that she has to remove from the array.

Constraints

  • 1≤T≤100
  • 2≤N≤10000
  • −1000≤Ai​≤1000

Sample 1:

Input:
4
3
1 9 8
4
2 -1 9 100
4
2 -1 0 100
4
2 -1 -1 100
Output:
0
1
0
0

Explanation:

Test case 1: The product of the elements of the array is 1×9×8=72, which is already non-negative. Hence no element needs to be removed, and so the answer is 0.

Test case 2: The product of the elements of the array is 2×−1×9×100=−1800, which is negative. Alice can remove the element −1, and the product then becomes non-negative. Hence the answer is 1.

Test case 3: The product of the elements of the array is 2×−1×0×100=0, which is already non-negative. Hence no element needs to be removed, and so the answer is 0.

Test case 4: The product of the elements of the array is 2×−1×−1×100=200, which is already non-negative. Hence no element needs to be removed, and so the answer is 0.

Non-Negative Product CodeChef Solution in C++14

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long> vll;
#define endl "\n"

int main() {
	// your code goes here
	ll test;
	cin>>test;
	while(test--) {
	    int n;
	    cin>>n;
	    vector<int> arr(n,0);
	    int neg = 0,zero = 0;
	    for(int i=0;i<n;i++) {
	        cin>>arr[i];
	        if(arr[i] == 0) {
	            zero++;
	        } else if(arr[i] < 0) {
	            neg++;
	        }
	    }
	    if(zero > 0 || neg%2 == 0) {
	        cout<<"0\n";
	    } else if(neg%2 == 1) {
	        cout<<"1\n";
	    }
	}
	return 0;
}

Non-Negative Product CodeChef Solution in Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		int t =sc.nextInt();
		
		while(t-->0){
		    int n =sc.nextInt();
		    int[]arr = new int[n];
		    
		   
		    int neg=0;
		    int count=0;
		    for(int i=0;i<n;i++){
		        arr[i]=sc.nextInt();
		       
		        
		        if(arr[i]<0){
		            neg++;
		        }
		        
		        if(arr[i]==0){
		            count++;
		        }
		    }
		    
		    if(count >0){
		        System.out.println(0);
		    }else if(neg%2!=0){
		        System.out.println(1);
		    }else{
		         System.out.println(0);
		    }
		}
	}
}

Non-Negative Product CodeChef Solution in Pyth 3

# cook your dish here
t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    c=0
    for x in (a):
        if (x)<0:
            c+=1
        elif (x)==0:
            c=0
            break
    if c%2==1:
        print(1)
    else:
        print(0)
Non-Negative Product CodeChef Solution Review:

In our experience, we suggest you solve this Non-Negative Product CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Non-Negative Product CodeChef Solution

Find on CodeChef

Conclusion:

I hope this Non-Negative Product CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *