Given an array of integers, replace every element with the least greater element on its right side in the array. If there are no greater element on the right side, replace it with -1.
Examples:
Input: [8, 58, 71, 18, 31, 32, 63, 92,
43, 3, 91, 93, 25, 80, 28]
Output: [18, 63, 80, 25, 32, 43, 80, 93,
80, 25, 93, -1, 28, -1, -1]
A naive method is to run two loops. The outer loop will one by one pick array elements from left to right. The inner loop will find the smallest element greater than the picked element on its right side. Finally, the outer loop will replace the picked element with the element found by inner loop. The time complexity of this method will be O(n2).
A tricky solution would be to use Binary Search Trees. We start scanning the array from right to left and insert each element into the BST. For each inserted element, we replace it in the array by its inorder successor in BST. If the element inserted is the maximum so far (i.e. its inorder successor doesn’t exist), we replace it by -1.
Below is the implementation of the above idea –
C++
// C++ program to replace every element with the // least greater element on its right #include <bits/stdc++.h> using namespace std; // A binary Tree node struct Node { int data; Node *left, *right; }; // A utility function to create a new BST node Node* newNode(int item) { Node* temp = new Node; temp->data = item; temp->left = temp->right = NULL; return temp; } /* A utility function to insert a new node with given data in BST and find its successor */void insert(Node*& node, int data, Node*& succ) { /* If the tree is empty, return a new node */ if (node == NULL) node = newNode(data); // If key is smaller than root's key, go to left // subtree and set successor as current node if (data < node->data) { succ = node; insert(node->left, data, succ); } // go to right subtree else if (data > node->data) insert(node->right, data, succ); } // Function to replace every element with the // least greater element on its right void replace(int arr[], int n) { Node* root = NULL; // start from right to left for (int i = n - 1; i >= 0; i--) { Node* succ = NULL; // insert current element into BST and // find its inorder successor insert(root, arr[i], succ); // replace element by its inorder // successor in BST if (succ) arr[i] = succ->data; else // No inorder successor arr[i] = -1; } } // Driver Program to test above functions int main() { int arr[] = { 8, 58, 71, 18, 31, 32, 63, 92, 43, 3, 91, 93, 25, 80, 28 }; int n = sizeof(arr)/ sizeof(arr[0]); replace(arr, n); for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; } |
Java
// Java program to replace every element with // the least greater element on its right import java.io.*; class BinarySearchTree{ // A binary Tree node class Node { int data; Node left, right; Node(int d) { data = d; left = right = null; } } // Root of BST static Node root; static Node succ; // Constructor BinarySearchTree() { root = null; succ = null; } // A utility function to insert a new node with // given data in BST and find its successor Node insert(Node node, int data) { // If the tree is empty, return a new node if (node == null) { node = new Node(data); } // If key is smaller than root's key, // go to left subtree and set successor // as current node if (data < node.data) { succ = node; node.left = insert(node.left, data); } // Go to right subtree else if (data > node.data) node.right = insert(node.right, data); return node; } // Function to replace every element with the // least greater element on its right static void replace(int arr[], int n) { BinarySearchTree tree = new BinarySearchTree(); // start from right to left for(int i = n - 1; i >= 0; i--) { succ = null; // Insert current element into BST and // find its inorder successor root = tree.insert(root, arr[i]); // Replace element by its inorder // successor in BST if (succ != null) arr[i] = succ.data; // No inorder successor else arr[i] = -1; } } // Driver code public static void main(String[] args) { int arr[] = new int[] { 8, 58, 71, 18, 31, 32, 63, 92, 43, 3, 91, 93, 25, 80, 28 }; int n = arr.length; replace(arr, n); for(int i = 0; i < n; i++) System.out.print(arr[i] + " "); } } // The code is contributed by Tushar Bansal |
Output:
18 63 80 25 32 43 80 93 80 25 93 -1 28 -1 -1
The worst-case time complexity of the above solution is also O(n2) as it uses BST. The worst-case will happen when array is sorted in ascending or descending order. The complexity can easily be reduced to O(nlogn) by using balanced trees like red-black trees.
This article is contributed by Aditya Goel. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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:
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Replace every element of array with sum of elements on its right side
- Replace every element with the greatest element on right side
- Replace every element with the greatest element on its left side
- Replace every element with the smallest element on its left side
- Replace every element of the array by its previous element
- Replace every element of the array by its next element
- Count of Array elements greater than all elements on its left and next K elements on its right
- Queries to replace every array element by its XOR with a given value with updates
- Replace every elements in the array by its frequency in the array
- Replace elements with absolute difference of smallest element on left and largest element on right
- Replace every array element by Bitwise Xor of previous and next element
- Closest greater element for every array element from another array
- Maximize the number of indices such that element is greater than element to its left
- Replace every array element by multiplication of previous and next
- Replace every element of the array with BitWise XOR of all other
- Replace every element of the array by sum of all other elements
- Replace every element of the array by product of all other elements
- Replace every array element by sum of previous and next
- Replace every element with the smallest of the others
Improved By : mysticpeaks

