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 arrayvoid 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 operationsvoid 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 codeint 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 approachclass GFG{ // Utility function to print the// elements of an arraystatic 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 operationsstatic 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 codepublic 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 print the# elements of an arraydef 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 operationsdef 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 codearr = [1, 5, 2, 4, 3]n = len(arr)generateArr(arr, n)# This code is contributed by Mohit Kumar |
C#
// C# implementation of the approachusing System;class GFG{ // Utility function to print the// elements of an arraystatic 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 operationsstatic 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 codestatic public void Main (){ int []arr = { 1, 5, 2, 4, 3 }; int n = arr.Length; generateArr(arr, n);}}// This code is contributed by ajit. |
Javascript
<script> // JavaScript implementation of the approach // Utility function to print the // elements of an array function printArray(arr, n) { for (let i = 0; i < n; i++) { document.write(arr[i] + " "); } } // Function to return the minimum element // in the subarray arr[i...j] function getMin(arr, i, j) { // To store the minimum element let 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] function getMax(arr, i, j) { // To store the maximum element let 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 function generateArr(arr, n) { // Base cases if (n == 0) return; if (n == 1) { document.write(arr[0] + "</br>"); return; } // To store the new array elements let tmpArr = new Array(n); tmpArr.fill(0); // 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 (let 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); } let arr = [ 1, 5, 2, 4, 3 ]; let n = arr.length; generateArr(arr, n);// This code is contributed by suresh07.</script> |
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 arrayvoid printArray(int arr[], int n){ for (int i = 0; i < n; i++) { cout << arr[i] << " "; }}// Function to generate the array// with the given operationsvoid 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 codeint 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 approachclass GFG{// Utility function to print the// elements of an arraystatic void printArray(int arr[], int n){ for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); }}// Function to generate the array// with the given operationsstatic 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 codepublic 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 |
Python3
# Python 3 implementation of the approach # Utility function to print the# elements of an arraydef printArray(arr, n): for i in range( n): print(arr[i],end= " ") # Function to generate the array# with the given operationsdef generateArr(arr, n): # Base cases if (n == 0): return if (n == 1): print( arr[0]) return # To suffixMax[i] will store the maximum # element in the subarray arr[i...n-1] suffixMax=[0]*n suffixMax[n - 1] = arr[n - 1] for i in range(n - 2, -1 ,-1): suffixMax[i] = max(arr[i], suffixMax[i + 1]) # To store the minimum element on the left 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 i in range( 1,n - 1): # Store a copy of the currene element 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 codeif __name__ == "__main__": arr = [ 1, 5, 2, 4, 3 ] n = len(arr) generateArr(arr, n)# This code is contributed by chitranayal |
C#
// C# implementation for above approachusing System;class GFG{// Utility function to print the// elements of an arraystatic void printArray(int []arr, int n){ for (int i = 0; i < n; i++) { Console.Write(arr[i] + " "); }}// Function to generate the array// with the given operationsstatic 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 codepublic 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 |
Javascript
<script> // JavaScript implementation for above approach // Utility function to print the // elements of an array function printArray(arr, n) { for (let i = 0; i < n; i++) { document.write(arr[i] + " "); } } // Function to generate the array // with the given operations function generateArr(arr, n) { // Base cases if (n == 0) return; if (n == 1) { document.write(arr[0]); return; } // To suffixMax[i] will store the maximum // element in the subarray arr[i...n-1] let suffixMax = new Array(n); suffixMax.fill(0); suffixMax[n - 1] = arr[n - 1]; for (let i = n - 2; i >= 0; i--) suffixMax[i] = Math.max(arr[i], suffixMax[i + 1]); // To store the minimum element on the left let 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 (let i = 1; i < n - 1; i++) { // Store a copy of the currene element let 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); } let arr = [ 1, 5, 2, 4, 3 ]; let n = arr.length; generateArr(arr, n); </script> |
5 3 3 2 1
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.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


