LCM of unique elements present in an array
Last Updated :
14 May, 2021
Given an array arr[] consisting of N positive integers, the task is to find the LCM of all unique elements of the given array. If the array does not contain any unique elements, then print “-1“.
Examples:
Input: arr[] = {1, 2, 1, 3, 3, 4}
Output: 4
Explanation:
The unique elements of the given array are: 2 and 4. Therefore, the LCM of (2, 4) is 4.
Input: arr[] = {1, 1, 2, 2, 3, 3}
Output: -1
Naive Approach: The simplest approach to solve the given problem is to traverse the given array arr[] for each array element arr[i] and check if arr[i] is unique or not. If found to be true, then store it in another array. After traversing for all the elements, print the LCM of the elements present in the newly created array.
Time Complexity: O(N2 + N * log(M)), where M is the largest element of the array arr[].
Auxiliary Space: O(N)
Efficient Approach: The above approach can also be optimized by using Hashing for finding the unique elements in the array. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findGCD( int a, int b)
{
if (b == 0)
return a;
return findGCD(b, a % b);
}
int findLCM( int a, int b)
{
return (a * b) / findGCD(a, b);
}
int uniqueElementsLCM( int arr[], int N)
{
unordered_map< int , int > freq;
for ( int i = 0; i < N; i++) {
freq[arr[i]]++;
}
int lcm = 1;
for ( auto i : freq) {
if (i.second == 1) {
lcm = findLCM(lcm, i.first);
}
}
if (lcm == 1)
lcm = -1;
cout << lcm;
}
int main()
{
int arr[] = { 1, 2, 1, 3, 3, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
uniqueElementsLCM(arr, N);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map.Entry;
class GFG{
static int findGCD( int a, int b)
{
if (b == 0 )
return a;
return findGCD(b, a % b);
}
static int findLCM( int a, int b)
{
return (a * b) / findGCD(a, b);
}
static void uniqueElementsLCM( int arr[], int N)
{
HashMap<Integer,
Integer> freq = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++)
{
freq.put(arr[i],
freq.getOrDefault(arr[i], 0 ) + 1 );
}
int lcm = 1 ;
for (Entry<Integer, Integer> i : freq.entrySet())
{
if (i.getValue() == 1 )
{
lcm = findLCM(lcm, i.getKey());
}
}
if (lcm == 1 )
lcm = - 1 ;
System.out.print(lcm);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 1 , 3 , 3 , 4 };
int N = arr.length;
uniqueElementsLCM(arr, N);
}
}
|
Python3
from collections import defaultdict
def findGCD(a, b):
if (b = = 0 ):
return a
return findGCD(b, a % b)
def findLCM(a, b):
return (a * b) / / findGCD(a, b)
def uniqueElementsLCM(arr, N):
freq = defaultdict( int )
for i in range (N):
freq[arr[i]] + = 1
lcm = 1
for i in freq:
if (freq[i] = = 1 ):
lcm = findLCM(lcm, i)
if (lcm = = 1 ):
lcm = - 1
print (lcm)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 1 , 3 , 3 , 4 ]
N = len (arr)
uniqueElementsLCM(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int findGCD( int a, int b)
{
if (b == 0)
return a;
return findGCD(b, a % b);
}
static int findLCM( int a, int b)
{
return (a * b) / findGCD(a, b);
}
static void uniqueElementsLCM( int []arr, int N)
{
Dictionary< int , int > freq = new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (freq.ContainsKey(arr[i]))
freq[arr[i]]++;
else
freq.Add(arr[i],1);
}
int lcm = 1;
foreach (KeyValuePair< int , int > kvp in freq) {
if (kvp.Value == 1) {
lcm = findLCM(lcm, kvp.Key);
}
}
if (lcm == 1)
lcm = -1;
Console.Write(lcm);
}
public static void Main()
{
int []arr = {1, 2, 1, 3, 3, 4 };
int N = arr.Length;
uniqueElementsLCM(arr, N);
}
}
|
Javascript
<script>
function findGCD(a, b)
{
if (b == 0)
return a;
return findGCD(b, a % b);
}
function findLCM(a, b)
{
return (a * b) / findGCD(a, b);
}
function uniqueElementsLCM(arr, N)
{
var freq = new Map();
for ( var i = 0; i < N; i++) {
if (freq.has(arr[i]))
{
freq.set(arr[i], freq.get(arr[i])+1);
}
else
{
freq.set(arr[i], 1);
}
}
var lcm = 1;
freq.forEach((value, key) => {
if (value == 1) {
lcm = findLCM(lcm, key);
}
});
if (lcm == 1)
lcm = -1;
document.write( lcm);
}
var arr = [1, 2, 1, 3, 3, 4];
var N = arr.length;
uniqueElementsLCM(arr, N);
</script>
|
Time Complexity: O(N * log(M)), where M is the largest element of the array arr[]
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...