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:
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); } } |
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 |
0 < 5 > 1 < 2
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Arrange given numbers to form the biggest number | Set 1
- Arrange given numbers to form the biggest number | Set 2
- Arrange given numbers to form the smallest number
- Minimum value by which each Array element must be added as per given conditions
- Maximize the last Array element as per the given conditions
- Maximum modified Array sum possible by choosing elements as per the given conditions
- Arrange the array such that upon performing given operations an increasing order is obtained
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Count number of ways to arrange first N numbers
- Arrange numbers to form a valid sequence
- Arrange numbers 1 to N^2 in a Zig-Zag Matrix in ascending order
- Maximum number of objects that can be created as per given conditions
- Maximize Profit by trading stocks based on given rate per day
- Minimum cost to reduce the integer N to 1 as per given conditions
- Is it possible to reach N and M from 1 and 0 respectively as per given conditio
- Nth angle of a Polygon whose initial angle and per angle increment is given
- Perform range sum queries on string as per given condition
- Count of Binary Strings possible as per given conditions
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Time taken per hour for stoppage of Car
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : 29AjayKumar

