Queries to check whether all the elements can be made positive by flipping signs exactly K times
Last Updated :
29 Apr, 2023
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:
- Count number of negative integers in the array and store it in a variable cnt.
- 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.
- 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++
#include <bits/stdc++.h>
using namespace std;
int cnt_neg;
bool exists_zero;
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 ;
}
}
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 ;
}
}
int main()
{
int arr[] = { -1, 2, -3, 4, 5 };
int n = sizeof (arr) / sizeof ( int );
preProcess(arr, n);
int queries[] = { 1, 2, 3, 4 };
int q = sizeof (queries) / sizeof ( int );
for ( int i = 0; i < q; i++) {
if (isPossible(queries[i]))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int cnt_neg;
static boolean exists_zero;
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 ;
}
}
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 ;
}
}
public static void main (String[] args)
{
int arr[] = { - 1 , 2 , - 3 , 4 , 5 };
int n = arr.length;
preProcess(arr, n);
int queries[] = { 1 , 2 , 3 , 4 };
int q = arr.length;
for ( int i = 0 ; i < q; i++)
{
if (isPossible(queries[i]))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
}
|
Python3
cnt_neg = 0 ;
exists_zero = None ;
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 ;
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 ;
if __name__ = = "__main__" :
arr = [ - 1 , 2 , - 3 , 4 , 5 ];
n = len (arr);
preProcess(arr, n);
queries = [ 1 , 2 , 3 , 4 ];
q = len (queries);
for i in range (q) :
if (isPossible(queries[i])) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static int cnt_neg;
static bool exists_zero ;
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 ;
}
}
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 ;
}
}
static public void Main ()
{
int []arr = { -1, 2, -3, 4, 5 };
int n = arr.Length;
preProcess(arr, n);
int []queries = { 1, 2, 3, 4 };
int q = arr.Length;
for ( int i = 0; i < q; i++)
{
if (isPossible(queries[i]))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
let cnt_neg = 0;
let exists_zero = false ;
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 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 ;
}
}
let arr = [ -1, 2, -3, 4, 5 ];
let n = arr.length;
preProcess(arr, n);
let queries = [ 1, 2, 3, 4 ];
let q = queries.length;
for (let i = 0; i < q; i++) {
if (isPossible(queries[i]))
document.write( "Yes<br>" );
else
document.write( "No<br>" );
}
</script>
|
PHP
<?php
$cnt_neg = 0;
$exists_zero = false;
function preProcess( $arr , $n ) {
global $cnt_neg , $exists_zero ;
for ( $i = 0; $i < $n ; $i ++) {
if ( $arr [ $i ] < 0) {
$cnt_neg ++;
}
if ( $arr [ $i ] == 0) {
$exists_zero = true;
}
}
}
function isPossible( $k ) {
global $cnt_neg , $exists_zero ;
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;
}
}
}
$arr = array (-1, 2, -3, 4, 5);
$n = sizeof( $arr ) / sizeof( $arr [0]);
preProcess( $arr , $n );
$queries = array (1, 2, 3, 4);
$q = sizeof( $queries ) / sizeof( $queries [0]);
for ( $i = 0; $i < $q ; $i ++) {
if (isPossible( $queries [ $i ])) {
echo "Yes\n" ;
} else {
echo "No\n" ;
}
}
?>
|
Time Complexity: O(n + q), where n and q represents the size of the given arrays.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...