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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
vector<int> isPrime(100001, 1);
// Function to find the prime numbers
void sieve(int n)
{
// Mark 0 and 1 as non-prime
isPrime[0] = 0;
isPrime[1] = 0;
// Mark all multiples of 2 as
// non-prime
for (int i = 4; i <= n; i += 2)
isPrime[i] = 0;
// Mark all non-prime numbers
for (int i = 3; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * i; j <= n;
j += i)
isPrime[j] = 0;
}
}
}
// Find the GCD of the non-prime numbers
int nonPrimeGCD(vector<int>& arr, int n)
{
int i = 0;
// Find all non-prime numbers till n
// using sieve of Eratosthenes
sieve(n);
// Find first non - prime number
// in the array
while (isPrime[arr[i]] and i < n)
i++;
// If not found return -1
if (i == n)
return -1;
// Initialize GCD as the first
// non prime number
int gcd = arr[i];
// Take gcd of all non-prime numbers
for (int j = i + 1; j < n; j++) {
if (!isPrime[arr[j]])
gcd = __gcd(gcd, arr[j]);
}
return gcd;
}
// Driver code
int main()
{
int N = 6;
vector<int> arr = { 2, 4, 6, 12, 3, 5 };
// Find non Prime GCD
cout << nonPrimeGCD(arr, N);
return 0;
}
Java
// Java code to implement the approach
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];
// Function to find the prime numbers
public static void sieve(int n)
{
for (int i = 0; i <= n; i++) {
isPrime[i] = 1;
}
// Mark 0 and 1 as non-prime
isPrime[0] = 0;
isPrime[1] = 0;
// Mark all multiples of 2 as
// non-prime
for (int i = 4; i <= n; i += 2)
isPrime[i] = 0;
// Mark all non-prime numbers
for (int i = 3; i * i <= n; i++) {
if (isPrime[i] != 0) {
for (int j = i * i; j <= n; j += i)
isPrime[j] = 0;
}
}
}
// Find the GCD of the non-prime numbers
public static int nonPrimeGCD(int arr[], int n)
{
int i = 0;
// Find all non-prime numbers till n
// using sieve of Eratosthenes
sieve(n);
// Find first non - prime number
// in the array
while (isPrime[arr[i]] != 0 && i < n)
i++;
// If not found return -1
if (i == n)
return -1;
// Initialize GCD as the first
// non prime number
int gg = arr[i];
// Take gcd of all non-prime numbers
for (int j = i + 1; j < n; j++) {
if (isPrime[arr[j]] == 0)
gg = gcd(gg, arr[j]);
}
return gg;
}
// Driver Code
public static void main(String[] args)
{
int N = 6;
int arr[] = { 2, 4, 6, 12, 3, 5 };
// Find non Prime GCD
System.out.print(nonPrimeGCD(arr, N));
}
}
// This code is contributed by Rohit Pradhan
Python3
# python3 code to implement the approach
import math
isPrime = [1 for _ in range(1000011)]
# Function for __gcd
def __gcd(a, b):
if b == 0:
return a
return __gcd(b, a % b)
# Function to find the prime numbers
def sieve(n):
global isPrime
# Mark 0 and 1 as non-prime
isPrime[0] = 0
isPrime[1] = 0
# Mark all multiples of 2 as
# non-prime
for i in range(4, n+1, 2):
isPrime[i] = 0
# Mark all non-prime numbers
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
# Find the GCD of the non-prime numbers
def nonPrimeGCD(arr, n):
i = 0
# Find all non-prime numbers till n
# using sieve of Eratosthenes
sieve(n)
# Find first non - prime number
# in the array
while (isPrime[arr[i]] and i < n):
i += 1
# If not found return -1
if (i == n):
return -1
# Initialize GCD as the first
# non prime number
gcd = arr[i]
# Take gcd of all non-prime numbers
for j in range(i+1, n):
if (not isPrime[arr[j]]):
gcd = __gcd(gcd, arr[j])
return gcd
# Driver code
if __name__ == "__main__":
N = 6
arr = [2, 4, 6, 12, 3, 5]
# Find non Prime GCD
print(nonPrimeGCD(arr, N))
# This code is contributed by rakeshsahni
C#
// C# program for above approach
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];
// Function to find the prime numbers
public static void sieve(int n)
{
for (int i = 0; i <= n; i++) {
isPrime[i] = 1;
}
// Mark 0 and 1 as non-prime
isPrime[0] = 0;
isPrime[1] = 0;
// Mark all multiples of 2 as
// non-prime
for (int i = 4; i <= n; i += 2)
isPrime[i] = 0;
// Mark all non-prime numbers
for (int i = 3; i * i <= n; i++) {
if (isPrime[i] != 0) {
for (int j = i * i; j <= n; j += i)
isPrime[j] = 0;
}
}
}
// Find the GCD of the non-prime numbers
public static int nonPrimeGCD(int[] arr, int n)
{
int i = 0;
// Find all non-prime numbers till n
// using sieve of Eratosthenes
sieve(n);
// Find first non - prime number
// in the array
while (isPrime[arr[i]] != 0 && i < n)
i++;
// If not found return -1
if (i == n)
return -1;
// Initialize GCD as the first
// non prime number
int gg = arr[i];
// Take gcd of all non-prime numbers
for (int j = i + 1; j < n; j++) {
if (isPrime[arr[j]] == 0)
gg = gcd(gg, arr[j]);
}
return gg;
}
// Driver Code
public static void Main()
{
int N = 6;
int[] arr = { 2, 4, 6, 12, 3, 5 };
// Find non Prime GCD
Console.Write(nonPrimeGCD(arr, N));
}
}
// This code is contributed by code_hunt.
JavaScript
<script>
// JavaScript code to implement the approach
let isPrime = new Array(100001).fill(1)
// Function to find the prime numbers
function sieve(n)
{
// Mark 0 and 1 as non-prime
isPrime[0] = 0;
isPrime[1] = 0;
// Mark all multiples of 2 as
// non-prime
for (let i = 4; i <= n; i += 2)
isPrime[i] = 0;
// Mark all non-prime numbers
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);
}
// Find the GCD of the non-prime numbers
function nonPrimeGCD(arr,n)
{
let i = 0;
// Find all non-prime numbers till n
// using sieve of Eratosthenes
sieve(n);
// Find first non - prime number
// in the array
while (isPrime[arr[i]] && i < n)
i++;
// If not found return -1
if (i == n)
return -1;
// Initialize GCD as the first
// non prime number
let gcd = arr[i];
// Take gcd of all non-prime numbers
for (let j = i + 1; j < n; j++) {
if (!isPrime[arr[j]])
gcd = GCD(gcd, arr[j]);
}
return gcd;
}
// Driver code
let N = 6
let arr = [ 2, 4, 6, 12, 3, 5 ]
// Find non Prime GCD
document.write(nonPrimeGCD(arr, N))
// This code is contributed by shinjanpatra
</script>
Time Complexity: O(N * log(log( N )))
Auxiliary Space: O(N)
Similar Reads
Find the GCD of LCM of all unique pairs in an Array Given an integer array arr[] of size N, the task is to find the GCD of LCM of all unique pair (i, j) of the array, such that i < j.Examples: Input: arr[] = {10, 24, 40, 80} Output: 40 Explanation: LCM of all unique pairs following given conditions are: LCM(10, 24) = 120 LCM(10, 40) = 40 LCM(10, 8
9 min read
Find the GCD of an array made up of numeric strings Given an array arr[] consisting of numeric strings, the task is to calculate Greatest Common Divisor of the given array. Considering strings 'A' and 'B', "B divides A" if and only if A is a concatenation of B more than once. Find the largest string which divides both A and B. Examples: Input: arr[]
5 min read
Queries for GCD of all numbers of an array except elements in a given range Given an array of n numbers and a number of queries are also given. Each query will be represented by two integers l, r. The task is to find out the GCD of all the numbers of the array excluding the numbers given in the range l, r (both inclusive) for each query.Examples: Input : arr[] = {2, 6, 9} R
10 min read
Remove an element to maximize the GCD of the given array Given an array arr[] of length N ⥠2. The task is to remove an element from the given array such that the GCD of the array after removing it is maximized. Examples: Input: arr[] = {12, 15, 18} Output: 6 Remove 12: GCD(15, 18) = 3 Remove 15: GCD(12, 18) = 6 Remove 18: GCD(12, 15) = 3 Input: arr[] = {
13 min read
Sum of array elements which are prime factors of a given number Given an array arr[] of size N and a positive integer K, the task is to find the sum of all array elements which are prime factors of K. Examples: Input: arr[] = {1, 2, 3, 5, 6, 7, 15}, K = 35Output: 12Explanation: From the given array, 5 and 7 are prime factors of 35. Therefore, required sum = 5 +
8 min read
GCD of more than two (or array) numbers Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read