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> |
0 < 5 > 1 < 2
Â
Time Complexity: O(N*log N)Â
Auxiliary Space: O(1)Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!