检查是否可以通过将对替换为它们的产品来使 Array 的 GCD 大于 1
给定三个整数L 、 R和K。考虑一个由L到R的所有元素组成的数组arr[] ,任务是检查是否可以使用最多 K 次操作使数组的GCD大于 1 。操作定义如下:
- 从数组中选择任意两个数字
- 从数组中删除它们
- 将他们的产品重新插入阵列
例子:
Input: L = 4, R = 10, K = 3
Output: true
Explanation: Array will be arr[] = {4, 5, 6, 7, 8, 9, 10}
Choose arr[0], arr[1]: arr[] = {20, 6, 7, 8, 9, 10}
Choose arr[1], arr[2]: arr[] = {20, 42, 8, 9, 10}
Choose arr[2], arr[3]: arr[] = {20, 42, 72, 10}
GCD of the formed array = 2
Input: L = 3, R = 5, K = 1
Output: false
Explanation: Array will be arr[] = {3, 4, 5}]
Operation on arr[0], arr[1]: arr[] = {12, 5}, GCD = 1, or
Operation on arr[1], arr[2]: arr[] = {3, 20}, GCD = 1, or
Operation on arr[0], arr[2]: arr[] = {4, 15}, GCD = 1
方法:可以通过将所有奇数数组元素转换为偶数来解决该任务,从而使数组的整体 GCD 变为偶数,即大于 1 。要检查是否可以遵循以下情况:
- 情况 1:如果 L = R = 1,则 GCD 将始终为 1,返回false
- 情况 2:如果 L = R(且 L≠1)则 GCD = L,返回true
- 情况 3:如果K大于等于范围 L 和 R 之间的赔率数,则返回true
- 如果上述任何一种情况都没有暗示,则返回false 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find that GCD of array is
// greater than 1 or
// not after at most K operations
bool gcdOfArray(int L, int R, int K)
{
// Finding number of integers
// between L and R
int range = (R - L + 1);
int even = 0;
int odd = 0;
// Finding number of odd and even integers
// in the given range
if (range % 2 == 0) {
even = range / 2;
odd = range - even;
}
else {
if (L % 2 != 0 || R % 2 != 0) {
odd = (range / 2) + 1;
even = range - odd;
}
else {
odd = range / 2;
even = range - odd;
}
}
// Case 1
if (L == R && L == 1)
return false;
// Case 2
if (L == R)
return true;
// Case 3
if (K >= odd)
return true;
// Otherwise not possible
else
return false;
}
// Driver Code
int main()
{
int L = 4;
int R = 10;
int K = 3;
bool isPossible = gcdOfArray(L, R, K);
if (isPossible)
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
// Function to find that GCD of array is
// greater than 1 or
// not after at most K operations
public static boolean gcdOfArray(int L, int R, int K)
{
// Finding number of integers
// between L and R
int range = (R - L + 1);
int even = 0;
int odd = 0;
// Finding number of odd and even integers
// in the given range
if (range % 2 == 0) {
even = range / 2;
odd = range - even;
}
else {
if (L % 2 != 0 || R % 2 != 0) {
odd = (range / 2) + 1;
even = range - odd;
}
else {
odd = range / 2;
even = range - odd;
}
}
// Case 1
if (L == R && L == 1)
return false;
// Case 2
if (L == R)
return true;
// Case 3
if (K >= odd)
return true;
// Otherwise not possible
else
return false;
}
// Driver Code
public static void main(String[] args)
{
int L = 4;
int R = 10;
int K = 3;
boolean isPossible = gcdOfArray(L, R, K);
if (isPossible)
System.out.println("true");
else
System.out.println("false");
}
}
// This code is contributed by Taranpreet
Python3
# Python program for the above approach
# Function to find that GCD of array is
# greater than 1 or
# not after at most K operations
def gcdOfArray(L, R, K):
# Finding number of integers
# between L and R
range = (R - L + 1)
even = 0
odd = 0
# Finding number of odd and even integers
# in the given range
if (range % 2 == 0):
even = range // 2
odd = range - even
else:
if (L % 2 != 0 or R % 2 != 0):
odd = (range // 2) + 1
even = range - odd
else:
odd = range // 2
even = range - odd
# Case 1
if (L == R and L == 1):
return False
# Case 2
if (L == R):
return True
# Case 3
if (K >= odd):
return True
# Otherwise not possible
else:
return False
# Driver Code
L = 4
R = 10
K = 3
isPossible = gcdOfArray(L, R, K)
if (isPossible):
print("true")
else:
print("false")
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
public class GFG{
// Function to find that GCD of array is
// greater than 1 or
// not after at most K operations
public static bool gcdOfArray(int L, int R, int K)
{
// Finding number of integers
// between L and R
int range = (R - L + 1);
int even = 0;
int odd = 0;
// Finding number of odd and even integers
// in the given range
if (range % 2 == 0) {
even = range / 2;
odd = range - even;
}
else {
if (L % 2 != 0 || R % 2 != 0) {
odd = (range / 2) + 1;
even = range - odd;
}
else {
odd = range / 2;
even = range - odd;
}
}
// Case 1
if (L == R && L == 1)
return false;
// Case 2
if (L == R)
return true;
// Case 3
if (K >= odd)
return true;
// Otherwise not possible
else
return false;
}
// Driver Code
static public void Main (){
int L = 4;
int R = 10;
int K = 3;
bool isPossible = gcdOfArray(L, R, K);
if (isPossible)
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
// This code is contributed by Shubham Singh
Javascript
true
时间复杂度: O(1)
辅助空间: O(1)