给定N个元素的数组。我们的任务是将元素拆分为两个数组,即a1 []和a2 [],以便其中一个包含严格增加的元素,另一个包含严格减少的元素,并且a1.size()+ a2.size()= a.size() 。如果无法这样做,则打印-1 ,否则打印两个数组。
注意:可以有多个答案,子数组中元素的顺序不必相同。
例子:
Input: a[] = {7, 2, 7, 3, 3, 1, 4}
Output: a1[] = {1, 2, 3, 4, 7} , a2[] = {7, 3}
Input: a[] = {1, 2, 2, 1, 1}
Output: -1
It is not possile
方法:按照以下步骤解决上述问题:
- 初始化两个向量v1和v2,它们存储递增和递减的数字。
- 使用哈希可以知道数组中数字的出现。
- 如果该数字是第一次出现,则将其存储在v1中。
- 如果该数字似乎第二次出现,则将其存储在v2中。
- 如果该数字出现两次以上,则无法存储以创建严格增加和严格减少的数组。
- 最后,将第一个向量按升序排序,将第二个向量按降序排序并打印。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print both the arrays
void PrintBothArrays(int a[], int n)
{
// Store both arrays
vector v1, v2;
// Used for hashing
unordered_map mpp;
// Iterate for every element
for (int i = 0; i < n; i++) {
// Increase the count
mpp[a[i]]++;
// If first occurrence
if (mpp[a[i]] == 1)
v1.push_back(a[i]);
// If second occurrence
else if (mpp[a[i]] == 2)
v2.push_back(a[i]);
// If occurs more than 2 times
else {
cout << "Not possible";
return;
}
}
// Sort in increasing order
sort(v1.begin(), v1.end());
// Print the increasing array
cout << "Strictly increasing array is:\n";
for (auto it : v1)
cout << it << " ";
// Sort in reverse order
sort(v2.begin(), v2.end(), greater());
// Print the decreasing array
cout << "\nStrictly decreasing array is:\n";
for (auto it : v2)
cout << it << " ";
}
// Driver code
int main()
{
int a[] = { 7, 2, 7, 3, 3, 1, 4 };
int n = sizeof(a) / sizeof(a[0]);
PrintBothArrays(a, n);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to print both the arrays
static void PrintBothArrays(int a[], int n)
{
// Store both arrays
Vector v1 = new Vector(),
v2 = new Vector();
// Used for hashing
HashMap mpp = new HashMap<>();
// Iterate for every element
for (int i = 0; i < n; i++)
{
// Increase the count
mpp.put(a[i],(mpp.get(a[i]) == null?0:mpp.get(a[i]))+1);
// If first occurrence
if (mpp.get(a[i]) == 1)
v1.add(a[i]);
// If second occurrence
else if (mpp.get(a[i]) == 2)
v2.add(a[i]);
// If occurs more than 2 times
else
{
System.out.println( "Not possible");
return;
}
}
// Sort in increasing order
Collections.sort(v1);
// Print the increasing array
System.out.println("Strictly increasing array is:");
for (int i = 0; i < v1.size(); i++)
System.out.print(v1.get(i) + " ");
// Sort
Collections.sort(v2);
Collections.reverse(v2);
// Print the decreasing array
System.out.println("\nStrictly decreasing array is:");
for (int i = 0; i < v2.size(); i++)
System.out.print(v2.get(i) + " ");
}
// Driver code
public static void main(String args[])
{
int a[] = { 7, 2, 7, 3, 3, 1, 4 };
int n = a.length;
PrintBothArrays(a, n);
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 program to implement
# the above approach
# Function to print both the arrays
def PrintBothArrays(a, n) :
# Store both arrays
v1, v2 = [], [];
# Used for hashing
mpp = dict.fromkeys(a, 0);
# Iterate for every element
for i in range(n) :
# Increase the count
mpp[a[i]] += 1;
# If first occurrence
if (mpp[a[i]] == 1) :
v1.append(a[i]);
# If second occurrence
elif (mpp[a[i]] == 2) :
v2.append(a[i]);
# If occurs more than 2 times
else :
print("Not possible");
return;
# Sort in increasing order
v1.sort();
# Print the increasing array
print("Strictly increasing array is:");
for it in v1:
print(it, end = " ");
# Sort in reverse order
v2.sort(reverse = True);
# Print the decreasing array
print("\nStrictly decreasing array is:");
for it in v2 :
print(it, end = " ")
# Driver code
if __name__ == "__main__" :
a = [ 7, 2, 7, 3, 3, 1, 4 ];
n = len(a);
PrintBothArrays(a, n);
# This code is contributed by Ryuga
C#
// C# program to implement
// the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// Function to print both the arrays
static void PrintBothArrays(int [] a, int n)
{
// Store both arrays
List v1 = new List();
List v2 = new List();
// Used for hashing
Dictionary mpp = new Dictionary();
// Iterate for every element
for (int i = 0; i < n; i++)
{
// Increase the Count
if(mpp.ContainsKey(a[i]))
mpp[a[i]] = mpp[a[i]] + 1;
else
mpp[a[i]] = 1;
// If first occurrence
if (mpp[a[i]] == 1)
v1.Add(a[i]);
// If second occurrence
else if (mpp[a[i]] == 2)
v2.Add(a[i]);
// If occurs more than 2 times
else
{
Console.WriteLine( "Not possible");
return;
}
}
// Sort in increasing order
v1.Sort();
// Print the increasing array
Console.WriteLine("Strictly increasing array is:");
for (int i = 0; i < v1.Count; i++)
Console.Write(v1[i] + " ");
// Sort
v2.Sort();
v2.Reverse();
// Print the decreasing array
Console.WriteLine("\nStrictly decreasing array is:");
for (int i = 0; i < v2.Count; i++)
Console.Write(v2[i] + " ");
}
// Driver code
public static void Main()
{
int [] a = { 7, 2, 7, 3, 3, 1, 4 };
int n = a.Length;
PrintBothArrays(a, n);
}
}
// This code is contributed by ihritik
输出:
Strictly increasing array is:
1 2 3 4 7
Strictly decreasing array is:
7 3