Choose atleast two elements from array such that their GCD is 1 and cost is minimum

Given two integer arrays arr[] and cost[] where cost[i] is the cost of choosing arr[i]. The task is to choose a subset with at least two elements such that the GCD of all the elements from the subset is 1 and the cost of choosing those elements is as minimum as possible then print the minimum cost.
Examples:Â
Input: arr[] = {5, 10, 12, 1}, cost[] = {2, 1, 2, 6}Â
Output: 4Â
{5, 12} is the required subset with cost = 2 + 2 = 4Input: arr[] = {50, 100, 150, 200, 300}, cost[] = {2, 3, 4, 5, 6}Â
Output: -1Â
No subset possible with gcd = 1Â
Approach: Add GCD of any two elements to a map, now for every element arr[i] calculate its gcd with all the gcd values found so far (saved in the map) and update map[gcd] = min(map[gcd], map[gcd] + cost[i]). If in the end, map doesn’t contain any entry for gcd = 1 then print -1 else print the stored minimum cost.
Below is the implementation of the above approach:Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the minimum cost requiredint getMinCost(int arr[], int n, int cost[]){Â
    // Map to store <gcd, cost> pair where    // cost is the cost to get the current gcd    map<int, int> mp;    mp.clear();    mp[0] = 0;Â
    for (int i = 0; i < n; i++) {        for (auto it : mp) {            int gcd = __gcd(arr[i], it.first);Â
            // If current gcd value already exists in map            if (mp.count(gcd) == 1)Â
                // Update the minimum cost                // to get the current gcd                mp[gcd] = min(mp[gcd], it.second + cost[i]);Â
            else                mp[gcd] = it.second + cost[i];        }    }Â
    // If there can be no sub-set such that    // the gcd of all the elements is 1    if (mp[1] == 0)        return -1;    else        return mp[1];}Â
// Driver codeint main(){Â Â Â Â int arr[] = { 5, 10, 12, 1 };Â Â Â Â int cost[] = { 2, 1, 2, 6 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â
    cout << getMinCost(arr, n, cost);    return 0;} |
Java
// Java implementation of the approachÂ
import java.util.*;import java.util.concurrent.ConcurrentHashMap;Â
class GFG{  // Function to return the minimum cost requiredstatic int getMinCost(int arr[], int n, int cost[]){      // Map to store <gcd, cost> pair where    // cost is the cost to get the current gcd    Map<Integer,Integer> mp = new ConcurrentHashMap<Integer,Integer>();    mp.clear();    mp.put(0, 0);      for (int i = 0; i < n; i++) {        for (Map.Entry<Integer,Integer> it : mp.entrySet()){            int gcd = __gcd(arr[i], it.getKey());              // If current gcd value already exists in map            if (mp.containsKey(gcd))                  // Update the minimum cost                // to get the current gcd                mp.put(gcd, Math.min(mp.get(gcd), it.getValue() + cost[i]));              else                mp.put(gcd,it.getValue() + cost[i]);        }    }      // If there can be no sub-set such that    // the gcd of all the elements is 1    if (!mp.containsKey(1))        return -1;    else        return mp.get(1);}static int __gcd(int a, int b) {     return b == 0? a:__gcd(b, a % b);    } // Driver codepublic static void main(String[] args){    int arr[] = { 5, 10, 12, 1 };    int cost[] = { 2, 1, 2, 6 };    int n = arr.length;      System.out.print(getMinCost(arr, n, cost));}}Â
// This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approachfrom math import gcd as __gcdÂ
# Function to return the minimum cost requireddef getMinCost(arr, n, cost):Â
    # Map to store <gcd, cost> pair where    # cost is the cost to get the current gcd    mp = dict()    mp[0] = 0Â
    for i in range(n):        for it in list(mp):            gcd = __gcd(arr[i], it)Â
            # If current gcd value             # already exists in map            if (gcd in mp):Â
                # Update the minimum cost                # to get the current gcd                mp[gcd] = min(mp[gcd],                               mp[it] + cost[i])Â
            else:                mp[gcd] = mp[it] + cost[i]Â
    # If there can be no sub-set such that    # the gcd of all the elements is 1    if (mp[1] == 0):        return -1    else:        return mp[1]Â
# Driver codearr = [ 5, 10, 12, 1]cost = [ 2, 1, 2, 6]n = len(arr)Â
print(getMinCost(arr, n, cost))Â
# This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System;using System.Collections.Generic;using System.Linq;class GFG{Â Â static int __gcd(int a, int b)Â Â Â Â {Â Â Â Â Â Â return b == 0? a:__gcd(b, a % b);Â Â Â Â Â Â Â }Â
  // Function to return the minimum cost required   static int getMinCost(int[] arr, int n, int[] cost)   {Â
    // Map to store <gcd, cost> pair where     // cost is the cost to get the current gcd     Dictionary<int, int> mp = new Dictionary<int, int>();    mp.Add(0, 0);    for (int i = 0; i < n; i++)    {Â
      foreach (int it in mp.Keys.ToList())      {        int gcd = __gcd(arr[i], it);Â
        // If current gcd value already exists in map         if(mp.ContainsKey(gcd))        {Â
          // Update the minimum cost           // to get the current gcd           mp[gcd] = Math.Min(mp[gcd], mp[it] + cost[i]);        }        else        {          mp.Add(gcd, mp[it] + cost[i]);        }      }     }Â
    // If there can be no sub-set such that     // the gcd of all the elements is 1     if (mp[1] == 0)    {      return -1;    }    else    {      return mp[1];     }         }Â
  // Driver code  static public void Main ()  {    int[] arr = { 5, 10, 12, 1 };    int[] cost = { 2, 1, 2, 6 };    int n = arr.Length;     Console.WriteLine(getMinCost(arr, n, cost));  }}Â
// This code is contributed by avanitrachhadiya2155 |
Javascript
<script>Â
// JavaScript implementation of the approachÂ
// Function to return the minimum cost requiredfunction getMinCost(arr,n,cost){    // Map to store <gcd, cost> pair where    // cost is the cost to get the current gcd    let mp = new Map();         mp.set(0, 0);       for (let i = 0; i < n; i++) {        for (let [key, value] of mp.entries()){            let gcd = __gcd(arr[i], key);               // If current gcd value already exists in map            if (mp.has(gcd))                   // Update the minimum cost                // to get the current gcd                mp.set(gcd, Math.min(mp.get(gcd), value + cost[i]));               else                mp.set(gcd,value + cost[i]);        }    }       // If there can be no sub-set such that    // the gcd of all the elements is 1    if (!mp.has(1))        return -1;    else        return mp.get(1);}Â
function __gcd(a,b){Â Â Â Â return b == 0? a:__gcd(b, a % b);}Â
// Driver codelet arr=[5, 10, 12, 1 ];let cost=[2, 1, 2, 6 ];let n = arr.length;document.write(getMinCost(arr, n, cost));Â
Â
// This code is contributed by unknown2108Â
</script> |
4
Complexity Analysis:
- Time Complexity: O(n2 log(n)) where n is the number of elements.
- Auxiliary Space: O(n)Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



