📌  相关文章
📜  查询是否可以通过将符号恰好翻转K次来将所有元素都设置为正值

📅  最后修改于: 2021-04-21 22:14:31             🧑  作者: Mango

给定一个整数数组arr [],以及一些由整数K组成的查询,任务是通过精确地将整数的符号翻转K次来确定是否有可能使所有整数为正。我们可以多次翻转整数的符号。如果可能,请打印“是”,否则打印“否”

例子:

方法:以下是我们将使用的算法:

  1. 计算数组中负整数的数量并将其存储在变量cnt中
  2. 如果数组中没有零:
    • 如果K≥cnt,则答案为
    • 如果K = cnt(K – cnt)%2 = 0,则答案为
    • 否则,答案为
  3. 如果数组中存在零,则答案仅在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