Minimum possible value of x so that Gcd of x is 1 with each element
Last Updated :
24 Apr, 2023
Given an array arr[], the task is to find the minimum positive integer x (x > 1) such that for every ai for (0 ≤ i ≤ n-1),
gcd(ai, x) = 1.
Examples:
Input: item[] = [10, 3, 1]
Output: 7
Explanation:
- gcd(10, 7) = 1
- gcd(3, 7) = 1
- gcd(1, 7) = 1
Input: item[] = [2, 4, 6]
Output: 5
Explanation:
- gcd(2, 5) = 1
- gcd(4, 5) = 1
- gcd(6, 5) = 1
Approach: The problem can be solved based on the following idea:
In order to have the gcd as 1 of any element x with all the array elements, we can find all the prime factors of all the array elements and can store them in a map. Now, We can start iterating through the map and will mark all its multiples as false as these numbers can not have gcd as 1 because of common factors. After marking up all the multiples of all the factors as false, we can now start iterating from 2 and if the current number is found to be marked as true, print it. The number marked as true is not the common factor of all the array elements.
Follow the steps mentioned below to implement the idea:
- Declare a map that stores all the prime factors of all the array elements.
- Start Iterating through this map and mark all its key multiples as false as these numbers can not have gcd as 1.
- Start iterating from 2 and if any element is found to be marked as true, print it.
- For storing the prime factors of a number n, keep dividing it and storing the current number as long as it’s divided by the current number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
map< int , int > mpp;
const int N = 1e5;
bool prime[N + 1];
void sieve()
{
memset (prime, true , sizeof (prime));
prime[0] = false ;
for ( auto it : mpp) {
if (prime[it.first] == true ) {
prime[it.first] = false ;
for ( int j = it.first * 2; j <= N;
j += it.first) {
prime[j] = false ;
}
}
}
}
void facts( int n)
{
for ( int d = 2; d * d <= n; d++) {
while (n % d == 0) {
mpp[d]++;
n /= d;
}
}
if (n > 1)
mpp[n]++;
}
int main()
{
vector< int > arr = { 10, 3, 1 };
int n = arr.size();
for ( int i = 0; i < n; i++) {
facts(arr[i]);
}
sieve();
for ( int i = 2; i <= 1e5; i++) {
if (prime[i] == true ) {
cout << i << endl;
break ;
}
}
}
|
Java
import java.util.*;
class Main {
static Map<Integer, Integer> mpp
= new HashMap<Integer, Integer>();
static final int N = 100000 ;
static boolean [] prime = new boolean [N + 1 ];
static void sieve()
{
Arrays.fill(prime, true );
prime[ 0 ] = false ;
for (Map.Entry<Integer, Integer> it :
mpp.entrySet()) {
int key = it.getKey();
if (prime[key] == true ) {
prime[key] = false ;
for ( int j = key * 2 ; j <= N; j += key) {
prime[j] = false ;
}
}
}
}
static void facts( int n)
{
for ( int d = 2 ; d * d <= n; d++) {
while (n % d == 0 ) {
mpp.put(d, mpp.getOrDefault(d, 0 ) + 1 );
n /= d;
}
}
if (n > 1 )
mpp.put(n, mpp.getOrDefault(n, 0 ) + 1 );
}
public static void main(String[] args)
{
List<Integer> arr = new ArrayList<Integer>();
arr.add( 10 );
arr.add( 3 );
arr.add( 1 );
int n = arr.size();
for ( int i = 0 ; i < n; i++) {
facts(arr.get(i));
}
sieve();
for ( int i = 2 ; i <= N; i++) {
if (prime[i] == true ) {
System.out.println(i);
break ;
}
}
}
}
|
Python3
from collections import defaultdict
mpp = defaultdict( int )
N = 100000
prime = [ True for i in range (N + 1 )]
def sieve():
prime[ 0 ] = False
for it in mpp.items():
if prime[it[ 0 ]] = = True :
prime[it[ 0 ]] = False
for j in range (it[ 0 ] * 2 , N + 1 , it[ 0 ]):
prime[j] = False
def facts(n):
d = 2
while d * d < = n:
while n % d = = 0 :
mpp[d] + = 1
n / / = d
d + = 1
if n > 1 :
mpp[n] + = 1
arr = [ 10 , 3 , 1 ]
n = len (arr)
for i in range (n):
facts(arr[i])
sieve()
for i in range ( 2 , N + 1 ):
if prime[i] = = True :
print (i)
break
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static Dictionary< int , int > mpp
= new Dictionary< int , int >();
const int N = 100000;
static bool [] prime = new bool [N + 1];
static void Sieve()
{
Array.Fill(prime, true );
prime[0] = false ;
foreach (KeyValuePair< int , int > it in mpp)
{
int key = it.Key;
if (prime[key] == true ) {
prime[key] = false ;
for ( int j = key * 2; j <= N; j += key) {
prime[j] = false ;
}
}
}
}
static void Facts( int n)
{
for ( int d = 2; d * d <= n; d++) {
while (n % d == 0) {
if (mpp.ContainsKey(d))
mpp[d]++;
else
mpp.Add(d, 1);
n /= d;
}
}
if (n > 1) {
if (mpp.ContainsKey(n))
mpp[n]++;
else
mpp.Add(n, 1);
}
}
static public void Main()
{
List< int > arr = new List< int >();
arr.Add(10);
arr.Add(3);
arr.Add(1);
int n = arr.Count;
for ( int i = 0; i < n; i++) {
Facts(arr[i]);
}
Sieve();
for ( int i = 2; i <= N; i++) {
if (prime[i] == true ) {
Console.WriteLine(i);
break ;
}
}
}
}
|
Javascript
let mpp = {};
const N = 100000;
let prime = new Array(N + 1).fill( true );
function sieve() {
prime[0] = false ;
for (let it of Object.entries(mpp)) {
let key = parseInt(it[0]);
if (prime[key] == true )
{
prime[key] = false ;
for (let j = key * 2; j <= N; j += key) {
prime[j] = false ;
}
}
}
}
function facts(n) {
let d = 2;
while (d * d <= n) {
while (n % d == 0) {
mpp[d] = (mpp[d] || 0) + 1;
n /= d;
}
d += 1;
}
if (n > 1) {
mpp[n] = (mpp[n] || 0) + 1;
}
}
let arr = [10, 3, 1];
let n = arr.length;
for (let i = 0; i < n; i++) {
facts(arr[i]);
}
sieve();
for (let i = 2; i <= N; i++) {
if (prime[i] == true ) {
console.log(i);
break ;
}
}
|
Time Complexity: O(N*log(log(N)))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...