给定一个整数数组arr [],以及一些由整数K组成的查询,任务是通过精确地将整数的符号翻转K次来确定是否有可能使所有整数为正。我们可以多次翻转整数的符号。如果可能,请打印“是”,否则打印“否” 。
例子:
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
方法:以下是我们将使用的算法:
- 计算数组中负整数的数量并将其存储在变量cnt中。
- 如果数组中没有零:
- 如果K≥cnt,则答案为是。
- 如果K = cnt且(K – cnt)%2 = 0,则答案为是。
- 否则,答案为否。
- 如果数组中存在零,则答案仅在K
为否,否则答案始终为是。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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