📜  LCM数组中存在的唯一元素

📅  最后修改于: 2021-05-14 00:10:54             🧑  作者: Mango

给定由N个正整数组成的数组arr [] ,任务是查找给定数组的所有唯一元素的LCM。如果数组不包含任何唯一元素,则打印“ -1 ”。

例子:

天真的方法:解决给定问题的最简单方法是遍历每个数组元素arr [i]的给定数组arr [ ]并检查arr [i]是否唯一。如果发现为真,则将其存储在另一个数组中。遍历所有元素后,打印新创建的数组中存在的元素的LCM。

时间复杂度: O(N 2 + N * log(M)),其中M是数组arr []最大元素
辅助空间: O(N)

高效方法:还可以通过使用哈希在数组中查找唯一元素来优化上述方法。请按照以下步骤解决问题:

  • 初始化一个变量,将ans表示1,以存储数组唯一元素的LCM。
  • 遍历数组arr []并将每个元素的频率存储在Map中。
  • 现在,遍历地图,并且如果任何元素的计数等于1,ANS的值更新为ANS的LCM和当前元素。
  • 完成上述步骤后,如果ans的值为1,则打印“ -1” 。否则,输出ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find GCD of two numbers
int findGCD(int a, int b)
{
    // Base Case
    if (b == 0)
        return a;
 
    // Recursively find the GCD
    return findGCD(b, a % b);
}
 
// Function to find LCM of two numbers
int findLCM(int a, int b)
{
    return (a * b) / findGCD(a, b);
}
 
// Function to find LCM of unique elements
// present in the array
int uniqueElementsLCM(int arr[], int N)
{
    // Stores the frequency of each
    // number of the array
    unordered_map freq;
 
    // Store the frequency of each
    // element of the array
    for (int i = 0; i < N; i++) {
        freq[arr[i]]++;
    }
 
    // Store the required result
    int lcm = 1;
 
    // Traverse the map freq
    for (auto i : freq) {
 
        // If the frequency of the
        // current element is 1, then
        // update ans
        if (i.second == 1) {
            lcm = findLCM(lcm, i.first);
        }
    }
 
    // If there is no unique element,
    // set lcm to -1
    if (lcm == 1)
        lcm = -1;
 
    // Print the result
    cout << lcm;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 1, 3, 3, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    uniqueElementsLCM(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.HashMap;
import java.util.Map.Entry;
 
class GFG{
     
// Function to find GCD of two numbers
static int findGCD(int a, int b)
{
     
    // Base Case
    if (b == 0)
        return a;
 
    // Recursively find the GCD
    return findGCD(b, a % b);
}
 
// Function to find LCM of two numbers
static int findLCM(int a, int b)
{
    return (a * b) / findGCD(a, b);
}
 
// Function to find LCM of unique elements
// present in the array
static void uniqueElementsLCM(int arr[], int N)
{
     
    // Stores the frequency of each
    // number of the array
    HashMap freq = new HashMap();
 
    // Store the frequency of each
    // element of the array
    for(int i = 0; i < N; i++)
    {
        freq.put(arr[i],
                 freq.getOrDefault(arr[i], 0) + 1);
    }
 
    // Store the required result
    int lcm = 1;
 
    // Traverse the map freq
    for(Entry i : freq.entrySet())
    {
         
        // If the frequency of the
        // current element is 1, then
        // update ans
        if (i.getValue() == 1)
        {
            lcm = findLCM(lcm, i.getKey());
        }
    }
 
    // If there is no unique element,
    // set lcm to -1
    if (lcm == 1)
        lcm = -1;
 
    // Print the result
    System.out.print(lcm);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 1, 3, 3, 4 };
    int N = arr.length;
 
    // Function Call
    uniqueElementsLCM(arr, N);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
from collections import defaultdict
 
# Function to find GCD of two numbers
def findGCD(a, b):
     
    # Base Case
    if (b == 0):
        return a
 
    # Recursively find the GCD
    return findGCD(b, a % b)
 
# Function to find LCM of two numbers
def findLCM(a, b):
 
    return (a * b) // findGCD(a, b)
 
# Function to find LCM of unique elements
# present in the array
def uniqueElementsLCM(arr, N):
 
    # Stores the frequency of each
    # number of the array
    freq = defaultdict(int)
 
    # Store the frequency of each
    # element of the array
    for i in range(N):
        freq[arr[i]] += 1
 
    # Store the required result
    lcm = 1
 
    # Traverse the map freq
    for i in freq:
 
        # If the frequency of the
        # current element is 1, then
        # update ans
        if (freq[i] == 1):
            lcm = findLCM(lcm, i)
 
    # If there is no unique element,
    # set lcm to -1
    if (lcm == 1):
        lcm = -1
 
    # Print the result
    print(lcm)
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 1, 2, 1, 3, 3, 4 ]
    N = len(arr)
     
    # Function Call
    uniqueElementsLCM(arr, N)
 
# This code is contributed by ukasp


C#
// C# program for the above approach
 
using System;
using System.Collections.Generic;
 
class GFG{
 
  // Function to find GCD of two numbers
static int findGCD(int a, int b)
{
    // Base Case
    if (b == 0)
        return a;
 
    // Recursively find the GCD
    return findGCD(b, a % b);
}
 
// Function to find LCM of two numbers
static int findLCM(int a, int b)
{
    return (a * b) / findGCD(a, b);
}
 
// Function to find LCM of unique elements
// present in the array
static void uniqueElementsLCM(int []arr, int N)
{
    // Stores the frequency of each
    // number of the array
    Dictionary freq = new Dictionary();
 
    // Store the frequency of each
    // element of the array
    for (int i = 0; i < N; i++) {
        if(freq.ContainsKey(arr[i]))
          freq[arr[i]]++;
        else
            freq.Add(arr[i],1);
    }
 
    // Store the required result
    int lcm = 1;
 
    // Traverse the map freq
    foreach(KeyValuePair kvp in freq) {
 
        // If the frequency of the
        // current element is 1, then
        // update ans
        if (kvp.Value == 1) {
            lcm = findLCM(lcm, kvp.Key);
        }
    }
 
    // If there is no unique element,
    // set lcm to -1
    if (lcm == 1)
        lcm = -1;
 
    // Print the result
    Console.Write(lcm);
}
 
// Driver Code
public static void Main()
{
    int []arr = {1, 2, 1, 3, 3, 4 };
    int N = arr.Length;
 
    // Function Call
    uniqueElementsLCM(arr, N);
}
}
 
// This code is contributed by ipg2016107.


输出:
4

时间复杂度: O(N * log(M)),其中M是数组arr []最大元素
辅助空间: O(N)