Replace elements with absolute difference of smallest element on left and largest element on right
Given an array arr[] of N integers. The task is to replace all the elements of the array by the absolute difference of the smallest element on its left and the largest element on its right.
Examples:
Input: arr[] = {1, 5, 2, 4, 3}
Output: 5 3 3 2 1
Element Smallest on its left Largest on its right Absolute difference 1 NULL 5 5 5 1 4 3 2 1 4 3 4 1 3 2 3 1 NULL 1 Input: arr[] = {4, 3, 6, 2, 1, 20, 9, 10, 15, 6}
Output: 20 16 17 17 18 14 14 14 5 1
Naive approach: For every element arr[i] of the array, find the minimum element in the subarray arr[0…i-1] then the maximum element in the subarray arr[i+1…n-1] and print the absolute difference between the two. The time complexity of this approach will be O(N2).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Utility function to print the // elements of an array void printArray( int arr[], int n) { for ( int i = 0; i < n; i++) { cout << arr[i] << " " ; } } // Function to return the minimum element // in the subarray arr[i...j] int getMin( int arr[], int i, int j) { // To store the minimum element int minVal = arr[i++]; while (i <= j) { // Update the minimum element so far minVal = min(minVal, arr[i]); i++; } // Return the minimum element found return minVal; } // Function to return the maximum element // in the subarray arr[i...j] int getMax( int arr[], int i, int j) { // To store the maximum element int maxVal = arr[i++]; while (i <= j) { // Update the maximum element so far maxVal = max(maxVal, arr[i]); i++; } // Return the maximum element found return maxVal; } // Function to generate the array // with the given operations void generateArr( int arr[], int n) { // Base cases if (n == 0) return ; if (n == 1) { cout << arr[0]; return ; } // To store the new array elements int tmpArr[n]; // The first element has no // element on its left tmpArr[0] = getMax(arr, 1, n - 1); // From the second element to the // second last element for ( int i = 1; i < n - 1; i++) { // Absolute difference of the maximum // element to the right and the // minimum element to the left tmpArr[i] = abs (getMax(arr, i + 1, n - 1) - getMin(arr, 0, i - 1)); } // The last element has no // element on its right tmpArr[n - 1] = getMin(arr, 0, n - 2); // Print the generated array printArray(tmpArr, n); } // Driver code int main() { int arr[] = { 1, 5, 2, 4, 3 }; int n = sizeof (arr) / sizeof (arr[0]); generateArr(arr, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Utility function to print the // elements of an array static void printArray( int arr[], int n) { for ( int i = 0 ; i < n; i++) { System.out.print(arr[i] + " " ); } } // Function to return the minimum element // in the subarray arr[i...j] static int getMin( int arr[], int i, int j) { // To store the minimum element int minVal = arr[i++]; while (i <= j) { // Update the minimum element so far minVal = Math.min(minVal, arr[i]); i++; } // Return the minimum element found return minVal; } // Function to return the maximum element // in the subarray arr[i...j] static int getMax( int arr[], int i, int j) { // To store the maximum element int maxVal = arr[i++]; while (i <= j) { // Update the maximum element so far maxVal = Math.max(maxVal, arr[i]); i++; } // Return the maximum element found return maxVal; } // Function to generate the array // with the given operations static void generateArr( int arr[], int n) { // Base cases if (n == 0 ) return ; if (n == 1 ) { System.out.println(arr[ 0 ]); return ; } // To store the new array elements int tmpArr[] = new int [n]; // The first element has no // element on its left tmpArr[ 0 ] = getMax(arr, 1 , n - 1 ); // From the second element to the // second last element for ( int i = 1 ; i < n - 1 ; i++) { // Absolute difference of the maximum // element to the right and the // minimum element to the left tmpArr[i] = Math.abs(getMax(arr, i + 1 , n - 1 ) - getMin(arr, 0 , i - 1 )); } // The last element has no // element on its right tmpArr[n - 1 ] = getMin(arr, 0 , n - 2 ); // Print the generated array printArray(tmpArr, n); } // Driver code public static void main (String[] args) { int arr[] = { 1 , 5 , 2 , 4 , 3 }; int n = arr.length; generateArr(arr, n); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Utility function to prthe # elements of an array def printArray(arr, n): for i in range (n): print (arr[i], end = " " ) # Function to return the minimum element # in the subarray arr[i...j] def getMin(arr, i, j): # To store the minimum element minVal = arr[i] i + = 1 while (i < = j): # Update the minimum element so far minVal = min (minVal, arr[i]) i + = 1 # Return the minimum element found return minVal # Function to return the maximum element # in the subarray arr[i...j] def getMax(arr, i, j): # To store the maximum element maxVal = arr[i] i + = 1 while (i < = j): # Update the maximum element so far maxVal = max (maxVal, arr[i]) i + = 1 # Return the maximum element found return maxVal # Function to generate the array # With the given operations def generateArr(arr, n): # Base cases if (n = = 0 ): return if (n = = 1 ): print (arr[ 0 ], end = "") return # To store the new array elements tmpArr = [ 0 for i in range (n)] # The first element has no # element on its left tmpArr[ 0 ] = getMax(arr, 1 , n - 1 ) # From the second element to the # second last element for i in range ( 1 , n - 1 ): # Absolute difference of the maximum # element to the right and the # minimum element to the left tmpArr[i] = abs (getMax(arr, i + 1 , n - 1 ) - getMin(arr, 0 , i - 1 )) # The last element has no # element on its right tmpArr[n - 1 ] = getMin(arr, 0 , n - 2 ) # Print the generated array printArray(tmpArr, n) # Driver code arr = [ 1 , 5 , 2 , 4 , 3 ] n = len (arr) generateArr(arr, n) # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { // Utility function to print the // elements of an array static void printArray( int []arr, int n) { for ( int i = 0; i < n; i++) { Console.Write(arr[i] + " " ); } } // Function to return the minimum element // in the subarray arr[i...j] static int getMin( int []arr, int i, int j) { // To store the minimum element int minVal = arr[i++]; while (i <= j) { // Update the minimum element so far minVal = Math.Min(minVal, arr[i]); i++; } // Return the minimum element found return minVal; } // Function to return the maximum element // in the subarray arr[i...j] static int getMax( int []arr, int i, int j) { // To store the maximum element int maxVal = arr[i++]; while (i <= j) { // Update the maximum element so far maxVal = Math.Max(maxVal, arr[i]); i++; } // Return the maximum element found return maxVal; } // Function to generate the array // with the given operations static void generateArr( int []arr, int n) { // Base cases if (n == 0) return ; if (n == 1) { Console.WriteLine(arr[0]); return ; } // To store the new array elements int []tmpArr = new int [n]; // The first element has no // element on its left tmpArr[0] = getMax(arr, 1, n - 1); // From the second element to the // second last element for ( int i = 1; i < n - 1; i++) { // Absolute difference of the maximum // element to the right and the // minimum element to the left tmpArr[i] = Math.Abs(getMax(arr, i + 1, n - 1) - getMin(arr, 0, i - 1)); } // The last element has no // element on its right tmpArr[n - 1] = getMin(arr, 0, n - 2); // Print the generated array printArray(tmpArr, n); } // Driver code static public void Main () { int []arr = { 1, 5, 2, 4, 3 }; int n = arr.Length; generateArr(arr, n); } } // This code is contributed by ajit. |
5 3 3 2 1
Efficient approach: Create an array suffixMax[] where suffixMax[i] will store the maximum element in the subarray arr[i…N-1]. Also take a variable minSoFar which will store the minimum element so far on traversing the array from left to right. Now, for every element arr[i] the updated value will be abs(suffixMax[i + 1] – minSoFar).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Utility function to print the // elements of an array void printArray( int arr[], int n) { for ( int i = 0; i < n; i++) { cout << arr[i] << " " ; } } // Function to return the minimum element // in the subarray arr[i...j] int getMin( int arr[], int i, int j) { // To store the minimum element int minVal = arr[i++]; while (i <= j) { // Update the minimum element so far minVal = min(minVal, arr[i]); i++; } // Return the minimum element found return minVal; } // Function to return the maximum element // in the subarray arr[i...j] int getMax( int arr[], int i, int j) { // To store the maximum element int maxVal = arr[i++]; while (i <= j) { // Update the maximum element so far maxVal = max(maxVal, arr[i]); i++; } // Return the maximum element found return maxVal; } // Function to generate the array // with the given operations void generateArr( int arr[], int n) { // Base cases if (n == 0) return ; if (n == 1) { cout << arr[0]; return ; } // To suffixMax[i] will store the maximum // element in the subarray arr[i...n-1] int suffixMax[n]; suffixMax[n - 1] = arr[n - 1]; for ( int i = n - 2; i >= 0; i--) suffixMax[i] = max(arr[i], suffixMax[i + 1]); // To store the minimum element on the left int minSoFar = arr[0]; // The first element has no // element on its left arr[0] = suffixMax[1]; // From the second element to the // second last element for ( int i = 1; i < n - 1; i++) { // Store a copy of the currene element int temp = arr[i]; // Absolute difference of the maximum // element to the right and the // minimum element to the left arr[i] = abs (suffixMax[i + 1] - minSoFar); // Update the minimum element so far minSoFar = min(minSoFar, temp); } // The last element has no // element on its right arr[n - 1] = minSoFar; // Print the updated array printArray(arr, n); } // Driver code int main() { int arr[] = { 1, 5, 2, 4, 3 }; int n = sizeof (arr) / sizeof (arr[0]); generateArr(arr, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Utility function to print the // elements of an array static void printArray( int arr[], int n) { for ( int i = 0 ; i < n; i++) { System.out.print(arr[i] + " " ); } } // Function to return the minimum element // in the subarray arr[i...j] static int getMin( int arr[], int i, int j) { // To store the minimum element int minVal = arr[i++]; while (i <= j) { // Update the minimum element so far minVal = Math.min(minVal, arr[i]); i++; } // Return the minimum element found return minVal; } // Function to return the maximum element // in the subarray arr[i...j] static int getMax( int arr[], int i, int j) { // To store the maximum element int maxVal = arr[i++]; while (i <= j) { // Update the maximum element so far maxVal = Math.max(maxVal, arr[i]); i++; } // Return the maximum element found return maxVal; } // Function to generate the array // with the given operations static void generateArr( int arr[], int n) { // Base cases if (n == 0 ) return ; if (n == 1 ) { System.out.print(arr[ 0 ]); return ; } // To suffixMax[i] will store the maximum // element in the subarray arr[i...n-1] int []suffixMax = new int [n]; suffixMax[n - 1 ] = arr[n - 1 ]; for ( int i = n - 2 ; i >= 0 ; i--) suffixMax[i] = Math.max(arr[i], suffixMax[i + 1 ]); // To store the minimum element on the left int minSoFar = arr[ 0 ]; // The first element has no // element on its left arr[ 0 ] = suffixMax[ 1 ]; // From the second element to the // second last element for ( int i = 1 ; i < n - 1 ; i++) { // Store a copy of the currene element int temp = arr[i]; // Absolute difference of the maximum // element to the right and the // minimum element to the left arr[i] = Math.abs(suffixMax[i + 1 ] - minSoFar); // Update the minimum element so far minSoFar = Math.min(minSoFar, temp); } // The last element has no // element on its right arr[n - 1 ] = minSoFar; // Print the updated array printArray(arr, n); } // Driver code public static void main (String[] args) { int arr[] = { 1 , 5 , 2 , 4 , 3 }; int n = arr.length; generateArr(arr, n); } } // This code is contributed by PrinciRaj1992 |
C#
// C# implementation for above approach using System; class GFG { // Utility function to print the // elements of an array static void printArray( int []arr, int n) { for ( int i = 0; i < n; i++) { Console.Write(arr[i] + " " ); } } // Function to return the minimum element // in the subarray arr[i...j] static int getMin( int []arr, int i, int j) { // To store the minimum element int minVal = arr[i++]; while (i <= j) { // Update the minimum element so far minVal = Math.Min(minVal, arr[i]); i++; } // Return the minimum element found return minVal; } // Function to return the maximum element // in the subarray arr[i...j] static int getMax( int []arr, int i, int j) { // To store the maximum element int maxVal = arr[i++]; while (i <= j) { // Update the maximum element so far maxVal = Math.Max(maxVal, arr[i]); i++; } // Return the maximum element found return maxVal; } // Function to generate the array // with the given operations static void generateArr( int []arr, int n) { // Base cases if (n == 0) return ; if (n == 1) { Console.Write(arr[0]); return ; } // To suffixMax[i] will store the maximum // element in the subarray arr[i...n-1] int []suffixMax = new int [n]; suffixMax[n - 1] = arr[n - 1]; for ( int i = n - 2; i >= 0; i--) suffixMax[i] = Math.Max(arr[i], suffixMax[i + 1]); // To store the minimum element on the left int minSoFar = arr[0]; // The first element has no // element on its left arr[0] = suffixMax[1]; // From the second element to the // second last element for ( int i = 1; i < n - 1; i++) { // Store a copy of the currene element int temp = arr[i]; // Absolute difference of the maximum // element to the right and the // minimum element to the left arr[i] = Math.Abs(suffixMax[i + 1] - minSoFar); // Update the minimum element so far minSoFar = Math.Min(minSoFar, temp); } // The last element has no // element on its right arr[n - 1] = minSoFar; // Print the updated array printArray(arr, n); } // Driver code public static void Main (String[] args) { int []arr = { 1, 5, 2, 4, 3 }; int n = arr.Length; generateArr(arr, n); } } // This code is contributed by 29AjayKumar |
5 3 3 2 1
Recommended Posts:
- Replace every element with the smallest element on its left side
- Replace every element with the greatest element on its left side
- Find the difference of count of equal elements on the right and the left for each element
- Replace each element by the difference of the total size of the array and frequency of that element
- Replace all elements by difference of sums of positive and negative numbers after that element
- k-th smallest absolute difference of two elements in an array
- Sum of minimum absolute difference of each array element
- K’th Smallest/Largest Element using STL
- Replace every element of the array by sum of all other elements
- K'th Smallest/Largest Element in Unsorted Array | Set 1
- Replace every element of the array by product of all other elements
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Count of elements on the left which are divisible by current element
- Maximum count of elements divisible on the left for any element
- Replace every array element by Bitwise Xor of previous and next element
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.