Find the GCD of all the non-prime numbers of given Array
Last Updated :
10 May, 2022
Given an array arr[] having N integers, the task is to find the GCD of all the numbers in the array which are not prime. If all the numbers are prime return -1.
Examples:
Input: N = 3, arr[ ] = {2, 3, 5}
Output: -1
Explanation: All the numbers are prime.
Hence answer will be -1.
Input: N = 6, arr[ ] = { 2, 4, 6, 12, 3, 5 }
Output: 2
Explanation: Non-prime numbers present in the array are 4, 6, and 12.
Their GCD is 2.
Approach: This problem can be solved by finding the prime numbers in the array using the Sieve of Eratosthenes.
Follow the below steps to solve the problem:
- Find all the prime numbers in the range of minimum of the array and maximum of the array using the sieve Of Eratosthenes.
- Now iterate through the given array and find the non-prime numbers.
- Take GCD of all the non-prime numbers.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > isPrime(100001, 1);
void sieve( int n)
{
isPrime[0] = 0;
isPrime[1] = 0;
for ( int i = 4; i <= n; i += 2)
isPrime[i] = 0;
for ( int i = 3; i * i <= n; i++) {
if (isPrime[i]) {
for ( int j = i * i; j <= n;
j += i)
isPrime[j] = 0;
}
}
}
int nonPrimeGCD(vector< int >& arr, int n)
{
int i = 0;
sieve(n);
while (isPrime[arr[i]] and i < n)
i++;
if (i == n)
return -1;
int gcd = arr[i];
for ( int j = i + 1; j < n; j++) {
if (!isPrime[arr[j]])
gcd = __gcd(gcd, arr[j]);
}
return gcd;
}
int main()
{
int N = 6;
vector< int > arr = { 2, 4, 6, 12, 3, 5 };
cout << nonPrimeGCD(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int [] isPrime = new int [ 100001 ];
public static void sieve( int n)
{
for ( int i = 0 ; i <= n; i++) {
isPrime[i] = 1 ;
}
isPrime[ 0 ] = 0 ;
isPrime[ 1 ] = 0 ;
for ( int i = 4 ; i <= n; i += 2 )
isPrime[i] = 0 ;
for ( int i = 3 ; i * i <= n; i++) {
if (isPrime[i] != 0 ) {
for ( int j = i * i; j <= n; j += i)
isPrime[j] = 0 ;
}
}
}
public static int nonPrimeGCD( int arr[], int n)
{
int i = 0 ;
sieve(n);
while (isPrime[arr[i]] != 0 && i < n)
i++;
if (i == n)
return - 1 ;
int gg = arr[i];
for ( int j = i + 1 ; j < n; j++) {
if (isPrime[arr[j]] == 0 )
gg = gcd(gg, arr[j]);
}
return gg;
}
public static void main(String[] args)
{
int N = 6 ;
int arr[] = { 2 , 4 , 6 , 12 , 3 , 5 };
System.out.print(nonPrimeGCD(arr, N));
}
}
|
Python3
import math
isPrime = [ 1 for _ in range ( 1000011 )]
def __gcd(a, b):
if b = = 0 :
return a
return __gcd(b, a % b)
def sieve(n):
global isPrime
isPrime[ 0 ] = 0
isPrime[ 1 ] = 0
for i in range ( 4 , n + 1 , 2 ):
isPrime[i] = 0
for i in range ( 3 , int (math.sqrt(n)) + 1 ):
if (isPrime[i]):
for j in range (i * i, n + 1 , i):
isPrime[j] = 0
def nonPrimeGCD(arr, n):
i = 0
sieve(n)
while (isPrime[arr[i]] and i < n):
i + = 1
if (i = = n):
return - 1
gcd = arr[i]
for j in range (i + 1 , n):
if ( not isPrime[arr[j]]):
gcd = __gcd(gcd, arr[j])
return gcd
if __name__ = = "__main__" :
N = 6
arr = [ 2 , 4 , 6 , 12 , 3 , 5 ]
print (nonPrimeGCD(arr, N))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int [] isPrime = new int [100001];
public static void sieve( int n)
{
for ( int i = 0; i <= n; i++) {
isPrime[i] = 1;
}
isPrime[0] = 0;
isPrime[1] = 0;
for ( int i = 4; i <= n; i += 2)
isPrime[i] = 0;
for ( int i = 3; i * i <= n; i++) {
if (isPrime[i] != 0) {
for ( int j = i * i; j <= n; j += i)
isPrime[j] = 0;
}
}
}
public static int nonPrimeGCD( int [] arr, int n)
{
int i = 0;
sieve(n);
while (isPrime[arr[i]] != 0 && i < n)
i++;
if (i == n)
return -1;
int gg = arr[i];
for ( int j = i + 1; j < n; j++) {
if (isPrime[arr[j]] == 0)
gg = gcd(gg, arr[j]);
}
return gg;
}
public static void Main()
{
int N = 6;
int [] arr = { 2, 4, 6, 12, 3, 5 };
Console.Write(nonPrimeGCD(arr, N));
}
}
|
Javascript
<script>
let isPrime = new Array(100001).fill(1)
function sieve(n)
{
isPrime[0] = 0;
isPrime[1] = 0;
for (let i = 4; i <= n; i += 2)
isPrime[i] = 0;
for (let i = 3; i * i <= n; i++) {
if (isPrime[i]) {
for (let j = i * i; j <= n;
j += i)
isPrime[j] = 0;
}
}
}
function GCD(x,y){
if (!y) {
return x;
}
return GCD(y, x % y);
}
function nonPrimeGCD(arr,n)
{
let i = 0;
sieve(n);
while (isPrime[arr[i]] && i < n)
i++;
if (i == n)
return -1;
let gcd = arr[i];
for (let j = i + 1; j < n; j++) {
if (!isPrime[arr[j]])
gcd = GCD(gcd, arr[j]);
}
return gcd;
}
let N = 6
let arr = [ 2, 4, 6, 12, 3, 5 ]
document.write(nonPrimeGCD(arr, N))
</script>
|
Time Complexity: O(N * log(log( N )))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...