Arrange the numbers in the Array as per given inequalities
Given a list of N distinct integers and a list of N-1 inequality signs, the task is to insert the integers between the inequality signs, such that the final inequality formed always holds true.
Note: The order of the inequality signs should not be changed.
Examples:
Input: Integers: [ 2, 5, 1, 0 ], Signs: [ <, >, < ]
Output: 0 < 5 > 1 < 2
Explanation:
The inequality formed is consistent and valid.
Input: Integers: [ 8, 34, 25, 1, -5, 10], Signs: [ >, >, <, <, > ]
Output: 34 > 25 > -5 < 1 < 10 > 8
Explanation:
The inequality formed is consistent and valid.
Approach: The list of inequality symbols can contain symbols in any order. So to obtain a consistent inequality, put the smallest integer left in the array before each < symbol and the largest integer left before each > symbol. Based on this idea, below are the steps:
- Sort the list of integers in ascending order.
- Maintain two variables, say low and high, pointing at the first and last index of the list of integers.
- Iterate over the list of inequality symbols. If the current symbol is less, then add the integer pointed by low before <, and increment low to point to the next index. If the current symbol is greater, then add the integer pointed by high before >, and decrement high to point to the previous index.
- Finally, add the remaining element to the last position.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string formAnInequality( int integers[], char inequalities[],
int n1, int n2)
{
sort(integers, integers + n1);
int lowerIndex = 0;
int higherIndex = n1 - 1;
string sb = "" ;
for ( int i = 0; i < n2; i++) {
char ch = inequalities[i];
if (ch == '<' ) {
sb += " " ;
sb += to_string(integers[lowerIndex++]);
sb += " " ;
sb += ch;
}
else {
sb += " " ;
sb += to_string(integers[higherIndex--]);
sb += " " ;
sb += ch;
}
}
sb += " " + to_string(integers[lowerIndex]);
return sb;
}
int main()
{
int integers[] = { 2, 5, 1, 0 };
char inequalities[] = { '<' , '>' , '<' };
int n1 = 4;
int n2 = 3;
string output
= formAnInequality(integers, inequalities, n1, n2);
cout << output << endl;
}
|
Java
import java.util.Arrays;
public class PlacingNumbers {
static String
formAnInequality( int [] integers,
char [] inequalities)
{
Arrays.sort(integers);
int lowerIndex = 0 ;
int higherIndex = integers.length - 1 ;
StringBuilder sb = new StringBuilder();
for ( char ch : inequalities) {
if (ch == '<' ) {
sb.append( " "
+ integers[lowerIndex++]
+ " "
+ ch);
}
else {
sb.append( " "
+ integers[higherIndex--]
+ " "
+ ch);
}
}
sb.append( " " + integers[lowerIndex]);
return sb.toString();
}
public static void main(String[] args)
{
int [] integers = { 2 , 5 , 1 , 0 };
char [] inequalities = { '<' , '>' , '<' };
String output
= formAnInequality(integers,
inequalities);
System.out.println(output);
}
}
|
Python3
def formAnInequality(integers,
inequalities):
integers.sort()
lowerIndex = 0
higherIndex = len (integers) - 1
sb = ""
for ch in inequalities:
if (ch = = '<' ):
sb + = ( " " + chr (integers[lowerIndex]) +
" " + ch)
lowerIndex + = 1
else :
sb + = ( " " + chr (integers[higherIndex]) +
" " + ch)
higherIndex - = 1
sb + = ( " " + chr (integers[lowerIndex]))
return sb
if __name__ = = "__main__" :
integers = [ 2 , 5 , 1 , 0 ]
inequalities = [ '<' , '>' , '<' ]
output = formAnInequality(integers,
inequalities)
print (output)
|
C#
using System;
using System.Text;
class GFG{
static String
formAnInequality( int [] integers,
char [] inequalities)
{
Array.Sort(integers);
int lowerIndex = 0;
int higherIndex = integers.Length - 1;
StringBuilder sb = new StringBuilder();
foreach ( char ch in inequalities)
{
if (ch == '<' )
{
sb.Append( " " + integers[lowerIndex++] +
" " + ch);
}
else
{
sb.Append( " " + integers[higherIndex--] +
" " + ch);
}
}
sb.Append( " " + integers[lowerIndex]);
return sb.ToString();
}
public static void Main(String[] args)
{
int [] integers = { 2, 5, 1, 0 };
char [] inequalities = { '<' , '>' , '<' };
String output = formAnInequality(integers,
inequalities);
Console.WriteLine(output);
}
}
|
Javascript
<script>
function formAnInequality(integers, inequalities)
{
(integers).sort( function (a, b){ return a - b;});
let lowerIndex = 0;
let higherIndex = integers.length - 1;
let sb = "" ;
for (let ch = 0; ch < inequalities.length; ch++)
{
if (inequalities[ch] == '<' )
{
sb += " " + (integers[lowerIndex++]) +
" " + inequalities[ch];
}
else
{
sb += " " + (integers[higherIndex--]) +
" " + inequalities[ch];
}
}
sb += " " + (integers[lowerIndex]);
return sb;
}
let integers = [ 2, 5, 1, 0 ];
let inequalities = [ '<' , '>' , '<' ];
let output = formAnInequality(
integers, inequalities);
document.write(output);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Last Updated :
05 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...