Arrange the numbers in the Array as per given inequalities | Java program for the above approach ; Function to place the integers in between the inequality signs ; Sort the integers array and set the index of smallest and largest element ; Iterate over the inequalities ; Append the necessary integers per symbol ; Add the final integer ; Return the answer ; Driver Code ; Given List of Integers ; Given list of inequalities ; Function Call ; Print the output
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 ) ; } }