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++
// 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> |
PHP
<?php // To store the count of // negative integers $cnt_neg = 0; // Check if zero exists $exists_zero = false; // Function to find the count of // negative integers and check // if zero exists in the array 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 that returns true if array // elements can be made positive by // changing signs exactly k times 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; } } } // Driver code $arr = array (-1, 2, -3, 4, 5); $n = sizeof( $arr ) / sizeof( $arr [0]); // Pre-processing preProcess( $arr , $n ); $queries = array (1, 2, 3, 4); $q = sizeof( $queries ) / sizeof( $queries [0]); // Perform queries for ( $i = 0; $i < $q ; $i ++) { if (isPossible( $queries [ $i ])) { echo "Yes\n" ; } else { echo "No\n" ; } } ?> |
No Yes No Yes
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!