Open In App

Minimum possible value of x so that Gcd of x is 1 with each element

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Declare map for storing the prime
// factors of the array elements
map<int, int> mpp;
const int N = 1e5;
bool prime[N + 1];
 
// Function for marking up the multiples
// of the number as false.
void sieve()
{
 
    memset(prime, true, sizeof(prime));
    prime[0] = false;
    for (auto it : mpp) {
        if (prime[it.first] == true) {
 
            // Marking the current number
            // as false.
            prime[it.first] = false;
 
            // Marking all the multiples
            // of a number as false.
            for (int j = it.first * 2; j <= N;
                 j += it.first) {
 
                prime[j] = false;
            }
        }
    }
}
 
// Function for storing the
// prime factors of a number
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]++;
}
 
// Driver code
int main()
{
 
    vector<int> arr = { 10, 3, 1 };
    int n = arr.size();
 
    // Storing prime factors of all
    // the array elements in map
    for (int i = 0; i < n; i++) {
        facts(arr[i]);
    }
 
    // Function call
    sieve();
 
    // Start iterating from 2 and if we
    // found any number as true, print it.
    for (int i = 2; i <= 1e5; i++) {
 
        if (prime[i] == true) {
            cout << i << endl;
            break;
        }
    }
}


Java




// Java code of the above approach
import java.util.*;
 
class Main {
 
    // Declare map for storing the prime
    // factors of the array elements
    static Map<Integer, Integer> mpp
        = new HashMap<Integer, Integer>();
    static final int N = 100000;
    static boolean[] prime = new boolean[N + 1];
 
    // Function for marking up the multiples
    // of the number as false.
    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) {
 
                // Marking the current number
                // as false.
                prime[key] = false;
 
                // Marking all the multiples
                // of a number as false.
                for (int j = key * 2; j <= N; j += key) {
                    prime[j] = false;
                }
            }
        }
    }
 
    // Function for storing the
    // prime factors of a number
    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);
    }
 
    // Driver code
    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();
 
        // Storing prime factors of all
        // the array elements in map
        for (int i = 0; i < n; i++) {
            facts(arr.get(i));
        }
 
        // Function call
        sieve();
 
        // Start iterating from 2 and if we
        // found any number as true, print it.
        for (int i = 2; i <= N; i++) {
 
            if (prime[i] == true) {
                System.out.println(i);
                break;
            }
        }
    }
}


Python3




# Python code of the above approach
 
from collections import defaultdict
 
# Declare defaultdict for storing the prime
# factors of the array elements
mpp = defaultdict(int)
N = 100000
prime = [True for i in range(N + 1)]
 
# Function for marking up the multiples
# of the number as false.
def sieve():
    prime[0] = False
    for it in mpp.items():
        if prime[it[0]] == True:
 
            # Marking the current number
            # as false.
            prime[it[0]] = False
 
            # Marking all the multiples
            # of a number as false.
            for j in range(it[0] * 2, N+1, it[0]):
                prime[j] = False
 
# Function for storing the
# prime factors of a number
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
 
# Driver code
arr = [10, 3, 1]
n = len(arr)
 
# Storing prime factors of all
# the array elements in map
for i in range(n):
    facts(arr[i])
 
# Function call
sieve()
 
# Start iterating from 2 and if we
# found any number as true, print it.
for i in range(2, N+1):
    if prime[i] == True:
        print(i)
        break


C#




// C# code of the above approach
 
using System;
using System.Collections.Generic;
 
public class GFG {
 
    // Declare map for storing the prime factors of the
    // array elements
    static Dictionary<int, int> mpp
        = new Dictionary<int, int>();
    const int N = 100000;
    static bool[] prime = new bool[N + 1];
 
    // Function for marking up the multiples of the number
    // as false.
    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) {
 
                // Marking the current number as false.
                prime[key] = false;
 
                // Marking all the multiples of a number as
                // false.
                for (int j = key * 2; j <= N; j += key) {
                    prime[j] = false;
                }
            }
        }
    }
 
    // Function for storing the prime factors of a number
    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()
    {
 
        // Code
        List<int> arr = new List<int>();
        arr.Add(10);
        arr.Add(3);
        arr.Add(1);
        int n = arr.Count;
 
        // Storing prime factors of all the array elements
        // in map
        for (int i = 0; i < n; i++) {
            Facts(arr[i]);
        }
 
        // function call
        Sieve();
 
        // start iterating from 2 and if we found any number
        // as true, print it.
        for (int i = 2; i <= N; i++) {
            if (prime[i] == true) {
                Console.WriteLine(i);
                break;
            }
        }
    }
}
 
// This code is contributed by karthik.


Javascript




// JavaScript code of the above approach
let mpp = {};
const N = 100000;
let prime = new Array(N + 1).fill(true);
 
// Function for marking up the multiples
// of the number as false.
function sieve() {
    prime[0] = false;
    for (let it of Object.entries(mpp)) {
        let key = parseInt(it[0]);
        if (prime[key] == true)
        {
         
            // Marking the current number
            // as false.
            prime[key] = false;
 
            // Marking all the multiples
            // of a number as false.
            for (let j = key * 2; j <= N; j += key) {
                prime[j] = false;
            }
        }
    }
}
 
// Function for storing the
// prime factors of a number
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;
    }
}
 
// Driver code
let arr = [10, 3, 1];
let n = arr.length;
 
// Storing prime factors of all
// the array elements in object
for (let i = 0; i < n; i++) {
    facts(arr[i]);
}
 
// Function call
sieve();
 
// Start iterating from 2 and if we
// found any number as true, print it.
for (let i = 2; i <= N; i++) {
    if (prime[i] == true) {
        console.log(i);
        break;
    }
}
 
// This code is contributed by lokesh.


Output

7

Time Complexity: O(N*log(log(N)))
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads