Arrange numbers to form a valid sequence
Given an array arr[] with N distinct numbers and another array arr1[] with N-1 operators (either < or >), the task is to organize the numbers to form a valid sequence which obeys relational operator rules with respect to provided operators.
Examples:
Input: arr[] = {3, 12, 7, 8, 5}; arr1= {‘<‘, ‘>’, ‘>’, ‘<‘}
Output: {3, 12, 8, 5, 7}
Explanation:
3 < 12 > 8 > 5 < 7
There can be more such combinations. The task is to return one of the combinations.
Input: arr[] = {8, 2, 7, 1, 5, 9}; arr1[] = {‘>’, ‘>’, ‘<‘, ‘>’, ‘<‘}
Output:{9, 8, 1, 7, 2, 5}
Explanation:
9 > 8 > 1 < 7 > 2 < 5
Naive Approach:
A naive approach is to try all different possible arrangement of numbers and check if the sequence is valid.
Time Complexity: O(2N).
Efficient Approach: The idea is to first sort the given array of numbers in ascending order. Then solve the problem using two pointers technique: one pointing at the front and other pointing at the end.
- Take one resultant array of size same as given array.
- If the current operator is ‘<‘ then include the element which the top pointer is pointing to in resultant array and increment it by 1.
- If the current operator is ‘>’ then include the element which the last pointer is pointing to in resultant array and decrement it by 1.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > orgazineInOrder(vector< int > vec,
vector< int > op, int n)
{
vector< int > result(n);
sort(vec.begin(), vec.end());
int i = 0, j = n - 1, k = 0;
while (i <= j && k <= n - 2) {
if (op[k] == '<' ) {
result[k] = vec[i++];
}
else {
result[k] = vec[j--];
}
k++;
}
result[n - 1] = vec[i];
return result;
}
int main()
{
vector< int > vec({ 8, 2, 7, 1, 5, 9 });
vector< int > op({ '>' , '>' , '<' ,
'>' , '<' });
vector< int > result
= orgazineInOrder(vec,
op, vec.size());
for ( int i = 0; i < result.size(); i++) {
cout << result[i] << " " ;
}
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int [] orgazineInOrder( int []vec, int [] op, int n)
{
int []result = new int [n];
Arrays.sort(vec);
int i = 0 , j = n - 1 , k = 0 ;
while (i <= j && k <= n - 2 )
{
if (op[k] == '<' )
{
result[k] = vec[i++];
}
else
{
result[k] = vec[j--];
}
k++;
}
result[n - 1 ] = vec[i];
return result;
}
public static void main(String[] args)
{
int []vec ={ 8 , 2 , 7 , 1 , 5 , 9 };
int [] op ={ '>' , '>' , '<' ,
'>' , '<' };
int []result = orgazineInOrder(vec,
op, vec.length);
for ( int i = 0 ; i < result.length; i++)
{
System.out.print(result[i]+ " " );
}
}
}
|
Python3
def orgazineInOrder(vec, op, n) :
result = [ 0 ] * n;
vec.sort();
i = 0 ;
j = n - 1 ;
k = 0 ;
while (i < = j and k < = n - 2 ) :
if (op[k] = = '<' ) :
result[k] = vec[i];
i + = 1 ;
else :
result[k] = vec[j];
j - = 1 ;
k + = 1 ;
result[n - 1 ] = vec[i];
return result;
if __name__ = = "__main__" :
vec = [ 8 , 2 , 7 , 1 , 5 , 9 ];
op = [ '>' , '>' , '<' , '>' , '<' ];
result = orgazineInOrder(vec, op, len (vec));
for i in range ( len (result)) :
print (result[i], end = " " );
|
C#
using System;
class GFG
{
static int [] orgazineInOrder( int []vec, int [] op, int n)
{
int []result = new int [n];
Array.Sort(vec);
int i = 0, j = n - 1, k = 0;
while (i <= j && k <= n - 2)
{
if (op[k] == '<' )
{
result[k] = vec[i++];
}
else
{
result[k] = vec[j--];
}
k++;
}
result[n - 1] = vec[i];
return result;
}
public static void Main()
{
int []vec ={ 8, 2, 7, 1, 5, 9 };
int [] op ={ '>' , '>' , '<' ,
'>' , '<' };
int []result = orgazineInOrder(vec,
op, vec.Length);
for ( int i = 0; i < result.Length; i++)
{
Console.Write(result[i] + " " );
}
}
}
|
Javascript
<script>
function orgazineInOrder(vec, op, n)
{
let result = [n];
vec.sort();
let i = 0, j = n - 1, k = 0;
while (i <= j && k <= n - 2)
{
if (op[k] == '<' )
{
result[k] = vec[i++];
}
else
{
result[k] = vec[j--];
}
k++;
}
result[n - 1] = vec[i];
return result;
}
let vec = [ 8, 2, 7, 1, 5, 9 ];
let op = [ '>' , '>' , '<' , '>' , '<' ];
let result = orgazineInOrder(vec,
op, vec.length);
for (let i = 0; i < result.length; i++)
{
document.write(result[i] + " " );
}
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Last Updated :
01 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...