Given an array arr[] of N integers, the task is to find an integer X such that the integers that are divisible by X and the integers that are not divisible by X are alternative to each other in the array. If there is no such value print -1.
Examples:
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
Approach: The approach is based on GCD since one set of alternating elements whether at odd indices or even indices should be completely divisible by an integer the only number that divides all the numbers is the GCD of that set of elements. The GCD of one set should not be equal to that of the other set then only that GCD becomes the answer. Follow the steps below to solve this problem:
- Traverse the array arr[] and calculate the GCD of the elements at odd indices (gcd1) and elements at even indices (gcd2) separately.
- If both the GCDs are not equal then do the following:
- Check if there is an integer in even indices that is divisible by gcd1. If any such integer found then gcd1 is not the required value.
- Check if there is an integer in odd indices that is divisible by gcd2. If any such integer found then gcd2 is not the required value.
- If any of the above two conditions is false, the corresponding GCD value is the answer. Else no such X exists.
- Otherwise no such X is possible.
Below is the implementation of the above approach.
// C++ code for the above approach #include <bits/stdc++.h> 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 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 |
# 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# 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 |
<script> // Javascript code for the above approach // Recursive function to return gcd of a and b function __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 function gcdofarray(arr, start, N)
{ var result = arr[start];
for ( var 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 function check( arr, start, gcd, N)
{ for ( var 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 function find_divisor(arr, N)
{ // Find gcds of values at odd indices
// and at even indices separately
var gcd1 = gcdofarray(arr, 1, N);
var gcd2 = gcdofarray(arr, 0, N);
// If both the gcds are not same
if (gcd1 != gcd2)
{
if (check(arr, 0, gcd1, N))
{
var x = gcd1;
document.write(x);
return ;
}
if (check(arr, 1, gcd2, N))
{
var x = gcd2;
document.write(x);
return ;
}
}
// If both the gcds are same print -1
document.write(-1);
} // Driver Code // Initialize the array var arr = [ 6, 5, 9, 10, 12, 15 ];
var N = arr.length;
// Call the function find_divisor(arr, N); // This code is contributed by Shubham Singh </script> |
5
Time Complexity: O(N)
Auxiliary Space: O(1)