Given an array of N elements. The task is to split the elements into two arrays say a1[] and a2[] such that one contains strictly increasing elements and the other contains strictly decreasing elements and a1.size() + a2.size() = a.size(). If it is not possible to do so, print -1 or else print both the arrays.
Note: There can be multiple answers and the order of elements needs not to be the same in the child arrays.
Examples:
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 possible
Approach: The following steps are followed to solve the above problem:
- Initialize two vectors v1 and v2 which stores increasing and decreasing numbers.
- Use hashing to know the occurrence of the number in the array.
- If the number appears to come for the first time, then store it in v1.
- If the number appears to come for the second time, then store it in v2.
- If the number appears for more than 2 times, then it is not possible to store to create a strictly increasing and strictly decreasing array.
- At last, sort the first vector in increasing order and the second vector in decreasing order and print them.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void PrintBothArrays( int a[], int n)
{
vector< int > v1, v2;
unordered_map< int , int > mpp;
for ( int i = 0; i < n; i++) {
mpp[a[i]]++;
if (mpp[a[i]] == 1)
v1.push_back(a[i]);
else if (mpp[a[i]] == 2)
v2.push_back(a[i]);
else {
cout << "Not possible" ;
return ;
}
}
sort(v1.begin(), v1.end());
cout << "Strictly increasing array is:\n" ;
for ( auto it : v1)
cout << it << " " ;
sort(v2.begin(), v2.end(), greater< int >());
cout << "\nStrictly decreasing array is:\n" ;
for ( auto it : v2)
cout << it << " " ;
}
int main()
{
int a[] = { 7, 2, 7, 3, 3, 1, 4 };
int n = sizeof (a) / sizeof (a[0]);
PrintBothArrays(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void PrintBothArrays( int a[], int n)
{
Vector<Integer> v1 = new Vector<Integer>(),
v2 = new Vector<Integer>();
HashMap<Integer, Integer> mpp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
mpp.put(a[i],(mpp.get(a[i]) == null ? 0 :mpp.get(a[i]))+ 1 );
if (mpp.get(a[i]) == 1 )
v1.add(a[i]);
else if (mpp.get(a[i]) == 2 )
v2.add(a[i]);
else
{
System.out.println( "Not possible" );
return ;
}
}
Collections.sort(v1);
System.out.println( "Strictly increasing array is:" );
for ( int i = 0 ; i < v1.size(); i++)
System.out.print(v1.get(i) + " " );
Collections.sort(v2);
Collections.reverse(v2);
System.out.println( "\nStrictly decreasing array is:" );
for ( int i = 0 ; i < v2.size(); i++)
System.out.print(v2.get(i) + " " );
}
public static void main(String args[])
{
int a[] = { 7 , 2 , 7 , 3 , 3 , 1 , 4 };
int n = a.length;
PrintBothArrays(a, n);
}
}
|
Python3
def PrintBothArrays(a, n) :
v1, v2 = [], [];
mpp = dict .fromkeys(a, 0 );
for i in range (n) :
mpp[a[i]] + = 1 ;
if (mpp[a[i]] = = 1 ) :
v1.append(a[i]);
elif (mpp[a[i]] = = 2 ) :
v2.append(a[i]);
else :
print ( "Not possible" );
return ;
v1.sort();
print ( "Strictly increasing array is:" );
for it in v1:
print (it, end = " " );
v2.sort(reverse = True );
print ( "\nStrictly decreasing array is:" );
for it in v2 :
print (it, end = " " )
if __name__ = = "__main__" :
a = [ 7 , 2 , 7 , 3 , 3 , 1 , 4 ];
n = len (a);
PrintBothArrays(a, n);
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static void PrintBothArrays( int [] a, int n)
{
List< int > v1 = new List< int >();
List< int > v2 = new List< int >();
Dictionary< int , int > mpp = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (mpp.ContainsKey(a[i]))
mpp[a[i]] = mpp[a[i]] + 1;
else
mpp[a[i]] = 1;
if (mpp[a[i]] == 1)
v1.Add(a[i]);
else if (mpp[a[i]] == 2)
v2.Add(a[i]);
else
{
Console.WriteLine( "Not possible" );
return ;
}
}
v1.Sort();
Console.WriteLine( "Strictly increasing array is:" );
for ( int i = 0; i < v1.Count; i++)
Console.Write(v1[i] + " " );
v2.Sort();
v2.Reverse();
Console.WriteLine( "\nStrictly decreasing array is:" );
for ( int i = 0; i < v2.Count; i++)
Console.Write(v2[i] + " " );
}
public static void Main()
{
int [] a = { 7, 2, 7, 3, 3, 1, 4 };
int n = a.Length;
PrintBothArrays(a, n);
}
}
|
Javascript
<script>
function PrletBothArrays(a, n)
{
let v1 = [], v2 = [];
let mpp = new Map();
for (let i = 0; i < n; i++)
{
mpp.set(a[i],(mpp.get(a[i]) == null ?0:mpp.get(a[i]))+1);
if (mpp.get(a[i]) == 1)
v1.push(a[i]);
else if (mpp.get(a[i]) == 2)
v2.push(a[i]);
else
{
document.write( "Not possible" );
return ;
}
}
v1.sort();
document.write( "Strictly increasing array is:" + "<br/>" );
for (let i = 0; i < v1.length; i++)
document.write(v1[i] + " " );
v2.sort();
v2.reverse();
document.write( "<br/>" + "\nStrictly decreasing array is:" + "<br/>" );
for (let i = 0; i < v2.length; i++)
document.write(v2[i] + " " );
}
let a = [ 7, 2, 7, 3, 3, 1, 4 ];
let n = a.length;
PrletBothArrays(a, n);
</script>
|
Output: Strictly increasing array is:
1 2 3 4 7
Strictly decreasing array is:
7 3
Time Complexity: O(N*logN), as in the worst case we will be using an inbuilt sort function to sort an array of size N. Where N is the number of elements in the array.
Auxiliary Space: O(N), as we are using extra space for the map and array v1 and v2. Where N is the length of the string.