Find an integer X which is divisor of all except exactly one element in an array
Given an array of integers. Find an integer X which is the divisor of all except for exactly one element in the given array.
Note: The GCD of all the elements is not 1.
Examples:
Input : arr[] = {6, 18, 3, 12}
Output : 6
6 is the divisor of all except 3.
Input : arr[] = {40, 15, 30, 42}
Output : 3
3 is the divisor of all except 40.
Approach: Make a prefix array P such that index i contains the GCD of all the elements from 1 to i. Similarly, make a suffix array S such that index i contains the GCD of all the elements from i to n-1 (last index). If the GCD of P[i-1] and S[i+1] is not the divisor of the element at i, then it is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getDivisor( int a[], int n)
{
if (n == 1)
return (a[0] + 1);
int P[n], S[n];
P[0] = a[0];
for ( int i = 1; i < n; i++)
P[i] = __gcd(a[i], P[i - 1]);
S[n-1] = a[n-1];
for ( int i = n - 2; i >= 0; i--)
S[i] = __gcd(S[i + 1], a[i]);
for ( int i = 0; i <= n; i++) {
int cur;
if (i == 0)
cur = S[i + 1];
else if (i == n - 1)
cur = P[i - 1];
else
cur = __gcd(P[i - 1], S[i + 1]);
if (a[i] % cur != 0)
return cur;
}
return 0;
}
int main()
{
int a[] = { 12, 6, 18, 12, 16 };
int n = sizeof (a) / sizeof (a[0]);
cout << getDivisor(a, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
static int getDivisor( int a[], int n)
{
if (n == 1 )
return (a[ 0 ] + 1 );
int P[] = new int [n];
int S[] = new int [n];
P[ 0 ] = a[ 0 ];
for ( int i = 1 ; i < n; i++)
P[i] = __gcd(a[i], P[i - 1 ]);
S[n- 1 ] = a[n- 1 ];
for ( int i = n - 2 ; i >= 0 ; i--)
S[i] = __gcd(S[i + 1 ], a[i]);
for ( int i = 0 ; i < n; i++) {
int cur;
if (i == 0 )
cur = S[i + 1 ];
else if (i == n - 1 )
cur = P[i - 1 ];
else
cur = __gcd(P[i - 1 ], S[i + 1 ]);
if (a[i] % cur != 0 )
return cur;
}
return 0 ;
}
public static void main (String[] args) {
int a[] = { 12 , 6 , 18 , 12 , 16 };
int n = a.length;
System.out.println(getDivisor(a, n));
}
}
|
Python 3
from math import gcd
def getDivisor(a, n):
if (n = = 1 ):
return (a[ 0 ] + 1 )
P = [ 0 ] * n
S = [ 0 ] * n
P[ 0 ] = a[ 0 ]
for i in range ( 1 , n):
P[i] = gcd(a[i], P[i - 1 ])
S[n - 1 ] = a[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
S[i] = gcd(S[i + 1 ], a[i])
for i in range ( 0 , n + 1 ):
cur = 0
if (i = = 0 ):
cur = S[i + 1 ]
elif (i = = n - 1 ):
cur = P[i - 1 ]
else :
cur = gcd(P[i - 1 ], S[i + 1 ])
if (a[i] % cur ! = 0 ):
return cur
return 0 ;
if __name__ = = '__main__' :
a = [ 12 , 6 , 18 , 12 , 16 ]
n = len (a)
print (getDivisor(a, n))
|
C#
using System;
class GFG
{
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
static int getDivisor( int [] a, int n)
{
if (n == 1)
return (a[0] + 1);
int [] P = new int [n];
int [] S = new int [n];
P[0] = a[0];
for ( int i = 1; i < n; i++)
P[i] = __gcd(a[i], P[i - 1]);
S[n - 1] = a[n - 1];
for ( int i = n - 2; i >= 0; i--)
S[i] = __gcd(S[i + 1], a[i]);
for ( int i = 0; i <= n; i++)
{
int cur;
if (i == 0)
cur = S[i + 1];
else if (i == n - 1)
cur = P[i - 1];
else
cur = __gcd(P[i - 1], S[i + 1]);
if (a[i] % cur != 0)
return cur;
}
return 0;
}
public static void Main ()
{
int [] a = { 12, 6, 18, 12, 16 };
int n = a.Length;
Console.WriteLine(getDivisor(a, n));
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0)
return b;
if ( $b == 0)
return $a ;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b );
return __gcd( $a , $b - $a );
}
function getDivisor( $a , $n )
{
if ( $n == 1)
return ( $a [0] + 1);
$P = array () ;
$S = array () ;
$P [0] = $a [0];
for ( $i = 1; $i < $n ; $i ++)
$P [ $i ] = __gcd( $a [ $i ], $P [ $i - 1]);
$S [ $n - 1] = $a [ $n - 1];
for ( $i = $n - 2; $i >= 0; $i --)
$S [ $i ] = __gcd( $S [ $i + 1], $a [ $i ]);
for ( $i = 0; $i <= $n ; $i ++)
{
if ( $i == 0)
$cur = $S [ $i + 1];
else if ( $i == $n - 1)
$cur = $P [ $i - 1];
else
$cur = __gcd( $P [ $i - 1], $S [ $i + 1]);
if ( $a [ $i ] % $cur != 0)
return $cur ;
}
return 0;
}
$a = array ( 12, 6, 18, 12, 16 );
$n = sizeof( $a );
echo getDivisor( $a , $n );
?>
|
Javascript
<script>
function __gcd(a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
function getDivisor(a, n)
{
if (n == 1)
return (a[0] + 1);
var P = new Array(n);
var S = new Array(n);
P[0] = a[0];
for ( var i = 1; i < n; i++)
P[i] = __gcd(a[i], P[i - 1]);
S[n-1] = a[n-1];
for ( var i = n - 2; i >= 0; i--)
S[i] = __gcd(S[i + 1], a[i]);
for ( var i = 0; i < n; i++) {
var cur;
if (i == 0)
cur = S[i + 1];
else if (i == n - 1)
cur = P[i - 1];
else
cur = __gcd(P[i - 1], S[i + 1]);
if (a[i] % cur != 0)
return cur;
}
return 0;
}
var a = [ 12, 6, 18, 12, 16 ];
var n = a.length;
document.write(getDivisor(a, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(nlogn)
- Auxiliary Space: O(n)
Last Updated :
12 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...