📌  相关文章
📜  将数组拆分为仅由相等元素组成的等长子序列

📅  最后修改于: 2021-10-26 06:21:56             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是检查是否可以将数组arr[]拆分为大小相同的不同子序列,使得子序列的每个元素都相等。如果发现是真的,则打印“YES” 。否则,打印“NO”

例子:

方法:这个想法基于以下观察:让arr[i]的频率为C i ,那么这些元素必须分解为X 的子序列,使得C i % X = 0 。对于每个索引i ,这必须是 YES 。为了满足这一点, X的值应该等于所有C i (1≤i≤N) 的最大公约数 (GCD ) 。如果X大于 1,则打印 YES,否则打印 NO。

请按照以下步骤解决问题:

  • 创建一个哈希图mp来存储数组arr[]中所有元素的频率。
  • mp中所有频率的最大公约数存储在变量X 中
  • 如果X大于 1,则答案为 YES。
  • 否则,答案是否定的。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the GCD
// of two numbers a and b
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to check if it is possible to
// split the array into equal length subsequences
// such that all elements in the subsequence are equal
void splitArray(int arr[], int N)
{
 
    // Store frequencies of
    // array elements
    map mp;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update frequency of arr[i]
        mp[arr[i]]++;
    }
 
    // Store the GCD of frequencies
    // of all array elements
    int G = 0;
 
    // Traverse the map
    for (auto i : mp) {
 
        // Update GCD
        G = gcd(G, i.second);
    }
 
    // If the GCD is greater than 1,
    // print YES otherwise print NO
    if (G > 1)
        cout << "YES";
    else
        cout << "NO";
}
 
// Driver Code
int main()
{
 
    // Given array
    int arr[] = { 1, 2, 3, 4, 4, 3, 2, 1 };
 
    // Store the size of the array
    int n = sizeof(arr) / sizeof(arr[0]);
 
    splitArray(arr, n);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG
{
 
    // Function to find the GCD
    // of two numbers a and b
    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
 
    // Function to check if it is possible to
    // split the array into equal length subsequences
    // such that all elements in the subsequence are equal
    void splitArray(int arr[], int N)
    {
 
        // Store frequencies of
        // array elements
        TreeMap mp
            = new TreeMap();
 
        // Traverse the array
        for (int i = 0; i < N; i++)
        {
 
            // Update frequency of arr[i]
            if (mp.containsKey(arr[i]))
            {
                mp.put(arr[i], mp.get(arr[i]) + 1);
            }
            else
            {
                mp.put(arr[i], 1);
            }
        }
 
        // Store the GCD of frequencies
        // of all array elements
        int G = 0;
 
        // Traverse the map
        for (Map.Entry m :
             mp.entrySet())
        {
           
            // update gcd
            Integer i = m.getValue();
            G = gcd(G, i.intValue());
        }
 
        // If the GCD is greater than 1,
        // print YES otherwise print NO
        if (G > 1)
            System.out.print("YES");
        else
            System.out.print("NO");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given array
        int[] arr = new int[] { 1, 2, 3, 4, 4, 3, 2, 1 };
 
        // Store the size of the array
        int n = arr.length;
        new GFG().splitArray(arr, n);
    }
}
 
// This code is contributed by abhishekgiri1


Python3
# Python3 program for the above approach
from collections import defaultdict
 
# Function to find the GCD
# of two numbers a and b
def gcd(a, b):
     
    if (b == 0):
        return a
         
    return gcd(b, a % b)
 
# Function to check if it is possible
# to split the array into equal length
# subsequences such that all elements
# in the subsequence are equal
def splitArray(arr, N):
     
    # Store frequencies of
    # array elements
    mp = defaultdict(int)
     
    # Traverse the array
    for i in range(N):
         
        # Update frequency of arr[i]
        mp[arr[i]] += 1
 
    # Store the GCD of frequencies
    # of all array elements
    G = 0
 
    # Traverse the map
    for i in mp:
 
        # Update GCD
        G = gcd(G, mp[i])
 
    # If the GCD is greater than 1,
    # print YES otherwise print NO
    if (G > 1):
        print("YES")
    else:
        print("NO")
 
# Driver Code
if __name__ == "__main__":
     
    # Given array
    arr = [ 1, 2, 3, 4, 4, 3, 2, 1 ]
 
    # Store the size of the array
    n = len(arr)
 
    splitArray(arr, n)
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
class GFG{
     
// Function to find the GCD
// of two numbers a and b
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to check if it is possible to
// split the array into equal length subsequences
// such that all elements in the subsequence are equal
static void splitArray(int[] arr, int n)
{
 
    // Store frequencies of
    // array elements
    Dictionary mp = new Dictionary();
 
    // Traverse the array
    for(int i = 0; i < n; ++i)
    {
           
        // Update frequency of
        // each array element
        if (mp.ContainsKey(arr[i]) == true)
        mp[arr[i]] += 1;
      else
        mp[arr[i]] = 1;
    }
 
    // Store the GCD of frequencies
    // of all array elements
    int G = 0;
 
    // Traverse the map
    foreach (KeyValuePair i in mp)
    {
 
        // Update GCD
        G = gcd(G, i.Value);
    }
 
    // If the GCD is greater than 1,
    // print YES otherwise print NO
    if (G > 1)
        Console.Write("YES");
    else
        Console.Write("NO");
}
 
// Driver Code
public static void Main()
{
   
    // Given array
    int[] arr = { 1, 2, 3, 4, 4, 3, 2, 1 };
 
    // Store the size of the array
    int n = arr.Length;
    splitArray(arr, n);
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
YES

时间复杂度: O(N * log(N))
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程