Queries to check whether all the elements can be made positive by flipping signs exactly K times
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... |
No Yes No Yes
Recommended Posts:
- Maximum subarray sum by flipping signs of at most K array elements
- Queries to check whether all the elements in the given range occurs even number of times
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Maximum score after flipping a Binary Matrix atmost K times
- Check if all elements of binary array can be made 1
- Check if all the elements can be made equal on dividing with X and Y
- Check if elements of array can be made equal by multiplying given prime numbers
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- Check if array sum can be made K by three operations on it
- Check whether the string S1 can be made equal to S2 with the given operation
- Check if a given string is made up of two alternating characters
- Check whether the number can be made perfect square after adding 1
- Check if two strings can be made equal by swapping one character among each other
- Check if string can be made lexicographically smaller by reversing any substring
- Check whether an array can be made strictly decreasing by modifying at most one element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


