Given an array arr[] with N distinct numbers and another array arr1[] with N-1 operators (either < or >), the task is to organize the numbers to form a valid sequence which obeys relational operator rules with respect to provided operators.
Examples:
Input: arr[] = {3, 12, 7, 8, 5}; arr1= {‘<‘, ‘>’, ‘>’, ‘<‘}
Output: {3, 12, 8, 5, 7}
Explanation:
3 < 12 > 8 > 5 < 7
There can be more such combinations. The task is to return one of the combinations.Input: arr[] = {8, 2, 7, 1, 5, 9}; arr1[] = {‘>’, ‘>’, ‘<‘, ‘>’, ‘<‘}
Output:{9, 8, 1, 7, 2, 5}
Explanation:
9 > 8 > 1 < 7 > 2 < 5
Naive Approach:
A naive approach is to try all different possible arrangement of numbers and check if the sequence is valid.
Time Complexity: O(2N).
Efficient Approach: The idea is to first sort the given array of numbers in ascending order. Then solve the problem using two pointers technique: one pointing at the front and other pointing at the end.
- Take one resultant array of size same as given array.
- If the current operator is ‘<‘ then include the element which the top pointer is pointing to in resultant array and increment it by 1.
- If the current operator is ‘>’ then include the element which the last pointer is pointing to in resultant array and decrement it by 1.
Below is the implementation of the above approach.
C++
// C++ implemenattion of the above approach#include <bits/stdc++.h>using namespace std; // Function to organize the given numbers// to form a valid sequence.vector<int> orgazineInOrder(vector<int> vec, vector<int> op, int n){ vector<int> result(n); // Sorting the array sort(vec.begin(), vec.end()); int i = 0, j = n - 1, k = 0; while (i <= j && k <= n - 2) { // Two pointer technique // to organize the numbers if (op[k] == '<') { result[k] = vec[i++]; } else { result[k] = vec[j--]; } k++; } result[n - 1] = vec[i]; return result;} // Driver codeint main(){ vector<int> vec({ 8, 2, 7, 1, 5, 9 }); vector<int> op({ '>', '>', '<', '>', '<' }); vector<int> result = orgazineInOrder(vec, op, vec.size()); for (int i = 0; i < result.size(); i++) { cout << result[i] << " "; } return 0;} |
Java
// Java implemenattion of the above approachimport java.util.*; class GFG{ // Function to organize the given numbers// to form a valid sequence.static int[] orgazineInOrder(int []vec,int[] op, int n){ int []result = new int[n]; // Sorting the array Arrays.sort(vec); int i = 0, j = n - 1, k = 0; while (i <= j && k <= n - 2) { // Two pointer technique // to organize the numbers if (op[k] == '<') { result[k] = vec[i++]; } else { result[k] = vec[j--]; } k++; } result[n - 1] = vec[i]; return result;} // Driver codepublic static void main(String[] args){ int []vec ={ 8, 2, 7, 1, 5, 9 }; int[] op ={ '>', '>', '<', '>', '<' }; int []result = orgazineInOrder(vec, op, vec.length); for (int i = 0; i < result.length; i++) { System.out.print(result[i]+ " "); }}} // This code is contributed by 29AjayKumar |
Python3
# Python3 implemenattion of the above approach # Function to organize the given numbers # to form a valid sequence. def orgazineInOrder(vec, op, n) : result = [0] * n; # Sorting the array vec.sort(); i = 0; j = n - 1; k = 0; while (i <= j and k <= n - 2) : # Two pointer technique # to organize the numbers if (op[k] == '<') : result[k] = vec[i]; i += 1; else : result[k] = vec[j]; j -= 1; k += 1; result[n - 1] = vec[i]; return result; # Driver code if __name__ == "__main__" : vec = [ 8, 2, 7, 1, 5, 9 ]; op = [ '>', '>', '<', '>', '<' ]; result = orgazineInOrder(vec, op, len(vec)); for i in range(len(result)) : print(result[i], end = " "); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Function to organize the given numbers // to form a valid sequence. static int[] orgazineInOrder(int []vec,int[] op, int n) { int []result = new int[n]; // Sorting the array Array.Sort(vec); int i = 0, j = n - 1, k = 0; while (i <= j && k <= n - 2) { // Two pointer technique // to organize the numbers if (op[k] == '<') { result[k] = vec[i++]; } else { result[k] = vec[j--]; } k++; } result[n - 1] = vec[i]; return result; } // Driver code public static void Main() { int []vec ={ 8, 2, 7, 1, 5, 9 }; int[] op ={ '>', '>', '<', '>', '<' }; int []result = orgazineInOrder(vec, op, vec.Length); for (int i = 0; i < result.Length; i++) { Console.Write(result[i] + " "); } } } // This code is contributed by AnkitRai01 |
9 8 1 7 2 5
Time Complexity: O(NlogN)
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


