Arrange the numbers in the Array as per given inequalities
Last Updated :
05 Oct, 2022
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to place the integers
// in between the inequality signs
string formAnInequality(int integers[], char inequalities[],
int n1, int n2)
{
// Sort the integers array and
// set the index of smallest
// and largest element
sort(integers, integers + n1);
int lowerIndex = 0;
int higherIndex = n1 - 1;
string sb = "";
// Iterate over the inequalities
for (int i = 0; i < n2; i++) {
char ch = inequalities[i];
// Append the necessary
// integers per symbol
if (ch == '<') {
sb += " ";
sb += to_string(integers[lowerIndex++]);
sb += " ";
sb += ch;
}
else {
sb += " ";
sb += to_string(integers[higherIndex--]);
sb += " ";
sb += ch;
}
}
// Add the final integer
sb += " " + to_string(integers[lowerIndex]);
// Return the answer
return sb;
}
// Driver Code
int main()
{
// Given List of Integers
int integers[] = { 2, 5, 1, 0 };
// Given list of inequalities
char inequalities[] = { '<', '>', '<' };
int n1 = 4;
int n2 = 3;
// Function Call
string output
= formAnInequality(integers, inequalities, n1, n2);
// Print the output
cout << output << endl;
}
// This code is contributed by phasing17
Java
// Java program for the above approach
import java.util.Arrays;
public class PlacingNumbers {
// Function to place the integers
// in between the inequality signs
static String
formAnInequality(int[] integers,
char[] inequalities)
{
// Sort the integers array and
// set the index of smallest
// and largest element
Arrays.sort(integers);
int lowerIndex = 0;
int higherIndex = integers.length - 1;
StringBuilder sb = new StringBuilder();
// Iterate over the inequalities
for (char ch : inequalities) {
// Append the necessary
// integers per symbol
if (ch == '<') {
sb.append(" "
+ integers[lowerIndex++]
+ " "
+ ch);
}
else {
sb.append(" "
+ integers[higherIndex--]
+ " "
+ ch);
}
}
// Add the final integer
sb.append(" " + integers[lowerIndex]);
// Return the answer
return sb.toString();
}
// Driver Code
public static void main(String[] args)
{
// Given List of Integers
int[] integers = { 2, 5, 1, 0 };
// Given list of inequalities
char[] inequalities = { '<', '>', '<' };
// Function Call
String output
= formAnInequality(integers,
inequalities);
// Print the output
System.out.println(output);
}
}
Python3
# Python3 program for
# the above approach
# Function to place the integers
# in between the inequality signs
def formAnInequality(integers,
inequalities):
# Sort the integers array and
# set the index of smallest
# and largest element
integers.sort()
lowerIndex = 0
higherIndex = len(integers) - 1
sb = ""
# Iterate over the inequalities
for ch in inequalities:
# Append the necessary
# integers per symbol
if (ch == '<'):
sb += (" " + chr(integers[lowerIndex]) +
" " + ch)
lowerIndex += 1
else:
sb += (" " + chr(integers[higherIndex]) +
" " + ch)
higherIndex -= 1
# Add the final integer
sb += (" " + chr(integers[lowerIndex]))
# Return the answer
return sb
# Driver Code
if __name__ == "__main__":
# Given List of Integers
integers = [2, 5, 1, 0]
# Given list of inequalities
inequalities = ['<', '>', '<']
# Function Call
output = formAnInequality(integers,
inequalities)
# Print the output
print(output)
# This code is contributed by Chitranayal
C#
// C# program for the above approach
using System;
using System.Text;
class GFG{
// Function to place the integers
// in between the inequality signs
static String
formAnInequality(int[] integers,
char[] inequalities)
{
// Sort the integers array and
// set the index of smallest
// and largest element
Array.Sort(integers);
int lowerIndex = 0;
int higherIndex = integers.Length - 1;
StringBuilder sb = new StringBuilder();
// Iterate over the inequalities
foreach(char ch in inequalities)
{
// Append the necessary
// integers per symbol
if (ch == '<')
{
sb.Append(" " + integers[lowerIndex++] +
" " + ch);
}
else
{
sb.Append(" " + integers[higherIndex--] +
" " + ch);
}
}
// Add the readonly integer
sb.Append(" " + integers[lowerIndex]);
// Return the answer
return sb.ToString();
}
// Driver Code
public static void Main(String[] args)
{
// Given List of ints
int[] integers = { 2, 5, 1, 0 };
// Given list of inequalities
char[] inequalities = { '<', '>', '<' };
// Function call
String output = formAnInequality(integers,
inequalities);
// Print the output
Console.WriteLine(output);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program for the above approach
// Function to place the integers
// in between the inequality signs
function formAnInequality(integers, inequalities)
{
// Sort the integers array and
// set the index of smallest
// and largest element
(integers).sort(function(a, b){return a - b;});
let lowerIndex = 0;
let higherIndex = integers.length - 1;
let sb = "";
// Iterate over the inequalities
for(let ch = 0; ch < inequalities.length; ch++)
{
// Append the necessary
// integers per symbol
if (inequalities[ch] == '<')
{
sb += " " + (integers[lowerIndex++]) +
" " + inequalities[ch];
}
else
{
sb += " " + (integers[higherIndex--]) +
" " + inequalities[ch];
}
}
// Add the final integer
sb += " " + (integers[lowerIndex]);
// Return the answer
return sb;
}
// Driver Code
// Given List of Integers
let integers = [ 2, 5, 1, 0 ];
// Given list of inequalities
let inequalities = [ '<', '>', '<' ];
// Function Call
let output = formAnInequality(
integers, inequalities);
// Print the output
document.write(output);
// This code is contributed by avanitrachhadiya2155
</script>
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Similar Reads
Arrange given numbers to form the smallest number Given an array arr[] of integer elements, the task is to arrange them in such a way that these numbers form the smallest possible number. For example, if the given array is {5, 6, 2, 9, 21, 1} then the arrangement will be 1212569. Examples: Input: arr[] = {5, 6, 2, 9, 21, 1} Output: 1212569 Input: a
14 min read
Arrange given numbers to form the biggest number Given an array of non-negative integers arr[], the task is to arrange them in a manner such that after concatenating them in order, it results in the largest possible number. Since the result may be very large, return it as a string.Examples:Input: arr[] = [3, 30, 34, 5, 9]Output: "9534330"Explanati
6 min read
Rearrange the given array to minimize the indices with prefix sum at least arr[i] Given an array arr[] consisting of N positive integers, rearrange the array to minimize the number of indices i such that the prefix sum from index 1 to index i-1 is greater than or equal to the current element arr[i] i.e. arr[1]+arr[2]+...+arr[i-1] >= arr[i]. Examples: Input: arr[] = [4, 2, 1]Ou
6 min read
Find 'N' number of solutions with the given inequality equations Find the value of a1, a2, a3, ....an such that the following two conditions are satisfied. a_1^2 + a_2^2 + a_3^2 + ....+ a_n^2 \geq X a_1 + a_2 + a_3 + ....+ a_n \leq Y Print the value of a1, a2, ..., an and "No solution" otherwise. Note: There maybe a several solutions, print any of them. Examples:
5 min read
Count number of pairs of arrays (a, b) such that a[i] <= b[i] Given two integers n and m, the task is to count the number of pairs of arrays (a, b) where both arrays have a length of m, contain integers between 1 and n (inclusive), and satisfy the conditions that each element in array a is less than or equal to the corresponding element in array b for all indi
9 min read
Check whether the given String is good for any ascending Array or not Given a string S of length N. Consider any ascending array A[] such that each A[i] > 0 for all (1 <= i <= N) and some conditions, the task is to output YES/NO, by checking that the above-given conditions are true for all possible ascending A[]'s or not with given S. Sum up all the elements
7 min read