找到 X 使得给定 Array 中仅备用索引处的元素可以被 X 整除
给定一个包含N个整数的数组arr[] ,任务是找到一个整数X ,使得可以被X整除的整数和不能被X整除的整数在数组中彼此交替。如果没有这样的值打印-1 。
例子:
Input: arr[] = {6, 5, 9, 10, 12, 15}
Output : 5
Explanation: The x = 5 divides all the elements at odd indices (0 based indexing)
but doesn’t divide the elements at even indices so the answer is 5.
Input: {10, 20, 40, 30}
Output: -1
方法:该方法基于 GCD,因为一组交替元素,无论是奇数索引还是偶数索引,都应该被一个整数完全整除,唯一能除所有数字的数字是该组元素的 GCD。一组的 GCD 不应该等于另一组的 GCD,那么只有 GCD 成为答案。请按照以下步骤解决此问题:
- 遍历数组arr[]并分别计算奇数索引(gcd1)处的元素和偶数索引( gcd2) 处的元素的 GCD。
- 如果两个 GCD 不相等,则执行以下操作:
- 检查偶数索引中是否存在可被gcd1整除的整数。如果找到任何这样的整数,则gcd1不是所需的值。
- 检查奇数索引中是否存在可被gcd2整除的整数。如果找到任何这样的整数,则gcd2不是所需的值。
- 如果以上两个条件中的任何一个为假,则对应的 GCD 值就是答案。否则不存在这样的 X。
- 否则不可能有这样的X。
下面是上述方法的实现。
C++
// C++ code for the above approach
#include
using namespace std;
// Function to find the gcd of the array
int gcdofarray(int arr[], int start, int N)
{
int result = arr[start];
for (int i = start + 2; i < N; i += 2)
result = __gcd(arr[i], result);
return result;
}
// Function to check if the whole set
// is not divisible by gcd
bool check(int arr[], int start, int gcd,
int N)
{
for (int i = start; i < N; i += 2) {
// If any element is divisible
// by gcd return 0
if (arr[i] % gcd == 0) {
return 0;
}
}
return 1;
}
// Function to find the value x
void find_divisor(int arr[], int N)
{
// Find gcds of values at odd indices
// and at even indices separately
int gcd1 = gcdofarray(arr, 1, N);
int gcd2 = gcdofarray(arr, 0, N);
// If both the gcds are not same
if (gcd1 != gcd2) {
if (check(arr, 0, gcd1, N) != 0) {
int x = gcd1;
cout << x << endl;
return;
}
if (check(arr, 1, gcd2, N) != 0) {
int x = gcd2;
cout << x << endl;
return;
}
}
// If both the gcds are same print -1
cout << -1 << endl;
}
// Driver Code
int main()
{
// Initialize the array
int arr[] = { 6, 5, 9, 10, 12, 15 };
int N = sizeof(arr) / sizeof(arr[0]);
// Call the function
find_divisor(arr, N);
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// Base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
// Function to find the gcd of the array
static int gcdofarray(int arr[], int start, int N)
{
int result = arr[start];
for(int i = start + 2; i < N; i += 2)
result = __gcd(arr[i], result);
return result;
}
// Function to check if the whole set
// is not divisible by gcd
static boolean check(int arr[], int start,
int gcd, int N)
{
for(int i = start; i < N; i += 2)
{
// If any element is divisible
// by gcd return 0
if (arr[i] % gcd == 0)
{
return false;
}
}
return true;
}
// Function to find the value x
static void find_divisor(int arr[], int N)
{
// Find gcds of values at odd indices
// and at even indices separately
int gcd1 = gcdofarray(arr, 1, N);
int gcd2 = gcdofarray(arr, 0, N);
// If both the gcds are not same
if (gcd1 != gcd2)
{
if (check(arr, 0, gcd1, N))
{
int x = gcd1;
System.out.println(x);
return;
}
if (check(arr, 1, gcd2, N))
{
int x = gcd2;
System.out.println(x);
return;
}
}
// If both the gcds are same print -1
System.out.print(-1);
}
// Driver Code
public static void main(String args[])
{
// Initialize the array
int arr[] = { 6, 5, 9, 10, 12, 15 };
int N = arr.length;
// Call the function
find_divisor(arr, N);
}
}
// This code is contributed by sanjoy_62
Python3
# Python # Java code for the above approach
# Recursive function to return gcd of a and b
def __gcd(a, b):
# Everything divides 0
if (a == 0):
return b
if (b == 0):
return a
# Base case
if (a == b):
return a
# a is greater
if (a > b):
return __gcd(a - b, b)
return __gcd(a, b - a)
# Function to find the gcd of the array
def gcdofarray(arr, start, N):
result = arr[start]
for i in range(start + 2, N, 2):
result = __gcd(arr[i], result)
return result
# Function to check if the whole set
# is not divisible by gcd
def check(arr, start, gcd, N):
for i in range(start, N, 2):
# If any element is divisible
# by gcd return 0
if (arr[i] % gcd == 0):
return False
return True
# Function to find the value x
def find_divisor(arr, N):
# Find gcds of values at odd indices
# and at even indices separately
gcd1 = gcdofarray(arr, 1, N)
gcd2 = gcdofarray(arr, 0, N)
# If both the gcds are not same
if (gcd1 != gcd2):
if (check(arr, 0, gcd1, N)):
x = gcd1
print(x)
return
if (check(arr, 1, gcd2, N)):
x = gcd2
print(x)
return
# If both the gcds are same print-1
print(-1)
# Driver Code
# Initialize the array
arr = [6, 5, 9, 10, 12, 15]
N = len(arr)
# Call the function
find_divisor(arr, N)
# This code is contributed by Shubham Singh
C#
// C# code for the above approach
using System;
class GFG
{
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// Base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
// Function to find the gcd of the array
static int gcdofarray(int[] arr, int start, int N)
{
int result = arr[start];
for (int i = start + 2; i < N; i += 2)
result = __gcd(arr[i], result);
return result;
}
// Function to check if the whole set
// is not divisible by gcd
static bool check(int[] arr, int start,
int gcd, int N)
{
for (int i = start; i < N; i += 2)
{
// If any element is divisible
// by gcd return 0
if (arr[i] % gcd == 0)
{
return false;
}
}
return true;
}
// Function to find the value x
static void find_divisor(int[] arr, int N)
{
// Find gcds of values at odd indices
// and at even indices separately
int gcd1 = gcdofarray(arr, 1, N);
int gcd2 = gcdofarray(arr, 0, N);
// If both the gcds are not same
if (gcd1 != gcd2)
{
if (check(arr, 0, gcd1, N))
{
int x = gcd1;
Console.WriteLine(x);
return;
}
if (check(arr, 1, gcd2, N))
{
int x = gcd2;
Console.WriteLine(x);
return;
}
}
// If both the gcds are same print -1
Console.Write(-1);
}
// Driver Code
public static void Main()
{
// Initialize the array
int[] arr = { 6, 5, 9, 10, 12, 15 };
int N = arr.Length;
// Call the function
find_divisor(arr, N);
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出
5
时间复杂度: O(N)
辅助空间: O(1)