Skip to content
Related Articles

Related Articles

Improve Article

Queries to check whether all the elements can be made positive by flipping signs exactly K times

  • Difficulty Level : Medium
  • Last Updated : 10 May, 2021

Given an integer array arr[], and some queries consisting of an integer K, the task is to determine if its possible to make all the integers positive by flipping signs of integers exactly K times. We can flip the sign of an integer more than once. If possible, then print Yes else print No.
Examples: 
 

Input: arr[] = {-1, 2, -3, 4, 5}, q[] = {1, 2} 
Output: 
No 
Yes 
Query 1: Only the sign of either -1 or -3 
can be changed and not both. 
Query 2: Signs of both the negative numbers 
can be changed to positive.
Input: arr[] = {-1, -1, 0, 6}, q[] = {1, 2, 3, 4} 
Output: 
No 
Yes 
Yes 
Yes 
 

 

Approach: Following will be the algorithm that we will use: 
 

  1. Count number of negative integers in the array and store it in a variable cnt.
  2. If there is no zero in the array: 
    • If K ≥ cnt then answer will be Yes.
    • If K = cnt and (K – cnt) % 2 = 0 then answer will be Yes.
    • Else answer will be No.
  3. If there exists a zero in the array then the answer will only be No if K < cnt else the answer is always Yes.

Below is the implementation of the above approach: 
 



C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// To store the count of
// negative integers
int cnt_neg;
 
// Check if zero exists
bool exists_zero;
 
// Function to find the count of
// negative integers and check
// if zero exists in the array
void preProcess(int arr[], int n)
{
    for (int i = 0; i < n; i++) {
        if (arr[i] < 0)
            cnt_neg++;
        if (arr[i] == 0)
            exists_zero = true;
    }
}
 
// Function that returns true if array
// elements can be made positive by
// changing signs exactly k times
bool isPossible(int k)
{
    if (!exists_zero) {
        if (k >= cnt_neg and (k - cnt_neg) % 2 == 0)
            return true;
        else
            return false;
    }
    else {
        if (k >= cnt_neg)
            return true;
        else
            return false;
    }
}
 
// Driver code
int main()
{
    int arr[] = { -1, 2, -3, 4, 5 };
    int n = sizeof(arr) / sizeof(int);
 
    // Pre-processing
    preProcess(arr, n);
 
    int queries[] = { 1, 2, 3, 4 };
    int q = sizeof(queries) / sizeof(int);
 
    // Perform queries
    for (int i = 0; i < q; i++) {
        if (isPossible(queries[i]))
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
 
    return 0;
}

Java




// Java implementation of the approach
import java.io.*;
 
class GFG
{
 
 
// To store the count of
// negative integers
static int cnt_neg;
 
// Check if zero exists
static boolean exists_zero;
 
// Function to find the count of
// negative integers and check
// if zero exists in the array
static void preProcess(int []arr, int n)
{
    for (int i = 0; i < n; i++)
    {
        if (arr[i] < 0)
            cnt_neg++;
        if (arr[i] == 0)
            exists_zero = true;
    }
}
 
// Function that returns true if array
// elements can be made positive by
// changing signs exactly k times
static boolean isPossible(int k)
{
    if (!exists_zero)
    {
        if (k >= cnt_neg && (k - cnt_neg) % 2 == 0)
            return true;
        else
            return false;
    }
    else
    {
        if (k >= cnt_neg)
            return true;
        else
            return false;
    }
}
 
// Driver code
public static void main (String[] args)
{
    int arr[] = { -1, 2, -3, 4, 5 };
    int n = arr.length;
 
    // Pre-processing
    preProcess(arr, n);
 
    int queries[] = { 1, 2, 3, 4 };
    int q = arr.length;
 
    // Perform queries
    for (int i = 0; i < q; i++)
    {
        if (isPossible(queries[i]))
            System.out.println( "Yes");
        else
            System.out.println( "No");
    }
}
}
 
// This code is contributed by anuj_67..

Python3




# Python3 implementation of the approach
 
# To store the count of
# negative integers
cnt_neg = 0;
 
# Check if zero exists
exists_zero = None;
 
# Function to find the count of
# negative integers and check
# if zero exists in the array
def preProcess(arr, n) :
    global cnt_neg
     
    for i in range(n) :
        if (arr[i] < 0) :
            cnt_neg += 1;
         
        if (arr[i] == 0) :
            exists_zero = True;
 
# Function that returns true if array
# elements can be made positive by
# changing signs exactly k times
def isPossible(k) :
 
    if (not exists_zero) :
        if (k >= cnt_neg and (k - cnt_neg) % 2 == 0) :
            return True;
        else :
            return False;
     
    else :
        if (k >= cnt_neg) :
            return True;
        else :
            return False;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ -1, 2, -3, 4, 5 ];
    n = len(arr);
 
    # Pre-processing
    preProcess(arr, n);
 
    queries = [ 1, 2, 3, 4 ];
    q = len(queries);
 
    # Perform queries
    for i in range(q) :
        if (isPossible(queries[i])) :
            print("Yes");
        else :
            print("No");
 
# This code is contributed by AnkitRai01

C#




// C# implementation of the approach
using System;
 
class GFG
{
     
// To store the count of
// negative integers
static int cnt_neg;
 
// Check if zero exists
static bool exists_zero ;
 
// Function to find the count of
// negative integers and check
// if zero exists in the array
static void preProcess(int []arr, int n)
{
    for (int i = 0; i < n; i++)
    {
        if (arr[i] < 0)
            cnt_neg++;
        if (arr[i] == 0)
            exists_zero = true;
    }
}
 
// Function that returns true if array
// elements can be made positive by
// changing signs exactly k times
static bool isPossible(int k)
{
    if (!exists_zero)
    {
        if (k >= cnt_neg && (k - cnt_neg) % 2 == 0)
            return true;
        else
            return false;
    }
    else
    {
        if (k >= cnt_neg)
            return true;
        else
            return false;
    }
}
 
// Driver code
static public void Main ()
{
     
    int []arr = { -1, 2, -3, 4, 5 };
    int n = arr.Length;
     
    // Pre-processing
    preProcess(arr, n);
     
    int []queries = { 1, 2, 3, 4 };
    int q = arr.Length;
     
    // Perform queries
    for (int i = 0; i < q; i++)
    {
        if (isPossible(queries[i]))
            Console.WriteLine( "Yes");
        else
            Console.WriteLine( "No");
    }
}
}
 
// This code is contributed by ajit...

Javascript




<script>
 
// Javascript implementation of the approach
 
// To store the count of
// negative integers
let cnt_neg = 0;
 
// Check if zero exists
let exists_zero = false;
 
// Function to find the count of
// negative integers and check
// if zero exists in the array
function preProcess(arr, n)
{
    for (let i = 0; i < n; i++) {
        if (arr[i] < 0)
            cnt_neg++;
        if (arr[i] == 0)
            exists_zero = true;
    }
}
 
// Function that returns true if array
// elements can be made positive by
// changing signs exactly k times
function isPossible(k)
{
    if (!exists_zero) {
        if (k >= cnt_neg && (k - cnt_neg) % 2 == 0)
            return true;
        else
            return false;
    }
    else {
        if (k >= cnt_neg)
            return true;
        else
            return false;
    }
}
 
// Driver code
    let arr = [ -1, 2, -3, 4, 5 ];
    let n = arr.length;
 
    // Pre-processing
    preProcess(arr, n);
 
    let queries = [ 1, 2, 3, 4 ];
    let q = queries.length;
 
    // Perform queries
    for (let i = 0; i < q; i++) {
        if (isPossible(queries[i]))
            document.write("Yes<br>");
        else
            document.write("No<br>");
    }
 
</script>
Output: 
No
Yes
No
Yes

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :