检查给定的数组是否可以重新排列为递增、递减或山序列
给定一个大小为N的数组arr[] 。任务是找出满足以下任一条件的序列的数量:
- The given sequence can be re-arranged into strictly increasing order, or
- The given sequence can be re-arranged into strictly decreasing order, or
- The given sequence can be re-arranged into Hill sequence order.
任务是检查有利的希尔序列是否可能,然后打印可能的序列。
例子:
Input: arr[] = {5, 7, 2, 1, 2}
Output: 1 2 5 7 2
Explanation: Here one such sequences is as follows
– s1 = {1, 2, 5, 7, 2}
Input: arr[] = {2, 4, 1, 2, 5, 6, 3}
Output: 1, 2, 3, 4, 5, 6, 2
Explanation: Here two such sequences are as follows
– s1 ={1, 2, 3, 4, 5, 6, 2} or,
– s2 ={1, 2, 3, 6, 5, 4, 2}
方法:这个想法是使用散列和排序来解决问题。检查是否存在频率大于 2 的元素,那么这是不可能的。请按照以下步骤解决问题:
- 将变量标志初始化为0 。
- 初始化 map
freq[]。 - 初始化向量
a[]。 - 使用变量i遍历范围[0, n)并执行以下任务:
- 将arr[i]的值压入数组a[]。
- 将freq[arr[i]]的计数增加1。
- 将变量max初始化为数组a[] 中的最大元素。
- 将变量freqsum初始化为0 。
- 使用变量x遍历映射freq[]并执行以下任务:
- 如果x.second大于等于3 ,则将flag设置为-1。
- 使用变量x遍历映射freq[]并执行以下任务:
- 计算变量freqsum中的所有不同元素。
- 如果freq[max]等于2 ,则将flag设置为-1 ,否则将flag设置为1。
- 如果flag等于1 ,则执行以下任务:
- 使用变量x遍历映射freq[]并执行以下任务:
- 如果x.second等于1则推入向量descending[]否则也将其推入ascending[] 。
- 对向量descending[]按升序排序, ascending[]按降序排序。
- 使用变量x遍历映射freq[]并执行以下任务:
- 执行上述步骤后,打印结果。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
void find(int arr[], int n)
{
// Flag will indicate whether
// sequence is possible or not
int flag = 0;
map freq;
vector a;
for (int i = 0; i < n; i++) {
a.push_back(arr[i]);
freq[arr[i]]++;
}
// Max element in
int max = *max_element(a.begin(), a.end());
// Store only unique elements count
int freqsum = 0;
// If any element having freq more than 2
for (auto& x : freq) {
// Hill sequence isn't possible
if (x.second >= 3)
flag = -1;
}
vector ascending, descending;
// Counting all distinct elements only
for (auto& x : freq) {
// Having freq more than 1 should
// be count only 1'nce
if (x.second > 1)
freqsum += 1;
else
freqsum += x.second;
}
// All elements are distinct
// Hill sequence is possible
if (a.size() == freqsum)
flag = 1;
else {
// Max element appeared morethan 1nce
// Hill sequence isn't possible
if (freq[max] >= 2)
flag = -1;
// Hill sequence is possible
else
flag = 1;
}
// Print favourable sequence if flag==1
// Hill sequence is possible
if (flag == 1) {
for (auto& x : freq) {
// If an element's freq==1
if (x.second == 1)
descending.push_back(x.first);
else {
// If an element's freq==2
descending.push_back(x.first);
ascending.push_back(x.first);
}
}
sort(descending.begin(), descending.end());
sort(ascending.begin(), ascending.end(),
greater());
for (auto& x : descending)
cout << x << " ";
for (auto& x : ascending)
cout << x << " ";
}
else {
cout << "Not Possible!\n";
}
}
// Driver Code
int main()
{
int n = 5;
int arr[n] = { 5, 7, 2, 1, 2 };
find(arr, n);
return 0;
}
Java
// JAVA program for the above approach
import java.util.*;
class GFG {
public static void find(int arr[], int n)
{
// Flag will indicate whether
// sequence is possible or not
int flag = 0;
HashMap freq = new HashMap<>();
ArrayList a = new ArrayList();
for (int i = 0; i < n; i++) {
a.add(arr[i]);
if (freq.containsKey(arr[i])) {
freq.put(arr[i], freq.get(arr[i]) + 1);
}
else {
freq.put(arr[i], 1);
}
}
// Max element in
int max = Collections.max(a);
// Store only unique elements count
int freqsum = 0;
// If any element having freq more than 2
for (Map.Entry i :
freq.entrySet()) {
// Hill sequence isn't possible
if (i.getValue() >= 3)
flag = -1;
}
ArrayList ascending
= new ArrayList();
ArrayList descending
= new ArrayList();
// Counting all distinct elements only
for (Map.Entry i :
freq.entrySet()) {
// Having freq more than 1 should
// be count only 1'nce
if (i.getValue() > 1)
freqsum += 1;
else
freqsum += i.getValue();
}
// All elements are distinct
// Hill sequence is possible
if (a.size() == freqsum)
flag = 1;
else {
// Max element appeared morethan 1nce
// Hill sequence isn't possible
if (freq.get(max) >= 2)
flag = -1;
// Hill sequence is possible
else
flag = 1;
}
// Print favourable sequence if flag==1
// Hill sequence is possible
if (flag == 1) {
for (Map.Entry i :
freq.entrySet()) {
// If an element's freq==1
if (i.getValue() == 1)
descending.add(i.getKey());
else {
// If an element's freq==2
descending.add(i.getKey());
ascending.add(i.getKey());
}
}
Collections.sort(descending);
Collections.sort(ascending,
Collections.reverseOrder());
for (int i = 0; i < descending.size(); i++)
System.out.print(descending.get(i) + " ");
for (int i = 0; i < ascending.size(); i++)
System.out.print(ascending.get(i) + " ");
}
else {
System.out.println("Not Possible!");
}
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
int[] arr = new int[n];
arr[0] = 5;
arr[1] = 7;
arr[2] = 2;
arr[3] = 1;
arr[4] = 2;
find(arr, n);
}
}
// This code is contributed by Taranpreet
Python3
# Python code for the above approach
def find(arr, n):
# Flag will indicate whether
# sequence is possible or not
flag = 0
freq = {}
a = []
for i in range(n):
a.append(arr[i])
if (arr[i] in freq):
freq[arr[i]] += 1
else:
freq[arr[i]] = 1
# Max element in
_max = max(a)
# Store only unique elements count
freqsum = 0
# If any element having freq more than 2
for k in freq.keys():
# Hill sequence isn't possible
if (freq[k] >= 3):
flag = -1
ascending = []
descending = []
# Counting all distinct elements only
for k in freq:
# Having freq more than 1 should
# be count only 1'nce
if (freq[k] > 1):
freqsum += 1
else:
freqsum += freq[k]
# All elements are distinct
# Hill sequence is possible
if (len(a) == freqsum):
flag = 1
else:
# Max element appeared morethan 1nce
# Hill sequence isn't possible
if (freq[_max] >= 2):
flag = -1
# Hill sequence is possible
else:
flag = 1
# Print favourable sequence if flag==1
# Hill sequence is possible
if (flag == 1):
for k in freq.keys():
# If an element's freq==1
if (freq[k] == 1):
descending.append(k)
else:
# If an element's freq==2
descending.append(k)
ascending.append(k)
descending.sort()
ascending.sort()
for x in descending:
print(x, end=" ")
for x in ascending:
print(x, end=" ")
else:
print("Not Possible!" + '
')
# Driver Code
n = 5
arr = [5, 7, 2, 1, 2]
find(arr, n)
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
public static void find(int []arr, int n)
{
// Flag will indicate whether
// sequence is possible or not
int flag = 0;
Dictionary freq = new Dictionary();
List a = new List();
for (int i = 0; i < n; i++) {
a.Add(arr[i]);
if (freq.ContainsKey(arr[i])) {
freq[arr[i]] = freq[arr[i]] + 1;
}
else {
freq.Add(arr[i], 1);
}
}
// Max element in
int max = a.Max();
// Store only unique elements count
int freqsum = 0;
// If any element having freq more than 2
foreach (KeyValuePair i in
freq) {
// Hill sequence isn't possible
if (i.Value >= 3)
flag = -1;
}
List ascending
= new List();
List descending
= new List();
// Counting all distinct elements only
foreach (KeyValuePair i in
freq) {
// Having freq more than 1 should
// be count only 1'nce
if (i.Value > 1)
freqsum += 1;
else
freqsum += i.Value;
}
// All elements are distinct
// Hill sequence is possible
if (a.Count == freqsum)
flag = 1;
else {
// Max element appeared morethan 1nce
// Hill sequence isn't possible
if (freq[max] >= 2)
flag = -1;
// Hill sequence is possible
else
flag = 1;
}
// Print favourable sequence if flag==1
// Hill sequence is possible
if (flag == 1) {
foreach (KeyValuePair i in
freq) {
// If an element's freq==1
if (i.Value == 1)
descending.Add(i.Key);
else {
// If an element's freq==2
descending.Add(i.Key);
ascending.Add(i.Key);
}
}
descending.Sort();
ascending.Sort();
ascending.Reverse();
for (int i = 0; i < descending.Count; i++)
Console.Write(descending[i] + " ");
for (int i = 0; i < ascending.Count; i++)
Console.Write(ascending[i] + " ");
}
else {
Console.WriteLine("Not Possible!");
}
}
// Driver Code
public static void Main(String[] args)
{
int n = 5;
int[] arr = new int[n];
arr[0] = 5;
arr[1] = 7;
arr[2] = 2;
arr[3] = 1;
arr[4] = 2;
find(arr, n);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
1 2 5 7 2
时间复杂度: O(N*log(N))
辅助空间: O(N)