给定大小为N的数组arr [] ,任务是检查是否有可能将数组arr []拆分为大小相等的不同子序列,以使子序列的每个元素都相等。如果发现是真的,则打印“是” 。否则,打印“否” 。
例子:
Input: arr[] = {1, 2, 3, 4, 4, 3, 2, 1}
Output: YES
Explanation: Possible partition: {1, 1}, {2, 2}, {3, 3}, {4, 4}.
Input: arr[] = {1, 1, 1, 2, 2, 2, 3, 3}
Output: NO
方法:该思想基于以下观察:假设arr [i]的频率为C i ,则必须将这些元素分解为X的子序列,以使C i %X = 0 。每个索引i都必须为YES。为了满足这一要求, X的值应等于所有C i的最大公约数(GCD) (1≤i≤N) 。如果X大于1,则打印YES,否则打印NO。
请按照以下步骤解决问题:
- 创建一个哈希表mp ,以存储数组arr []的所有元素的频率。
- 将所有频率的最大公约数存储在变量X中的mp中。
- 如果X大于1,则答案为是。
- 否则,答案是否定的。
下面是上述方法的实现:
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.
输出:
YES
时间复杂度: O(N * log(N))
辅助空间: O(N)