VOOZH about

URL: https://www.geeksforgeeks.org/dsa/finding-absolute-difference-of-sums-for-each-index-in-an-array/

⇱ Finding absolute difference of sums for each index in an Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Finding absolute difference of sums for each index in an Array

Last Updated : 13 Sep, 2023

Given an array arr[] of size N, find a new array ans[] where each index i represents the absolute difference between the sum of elements to the left and right of index i in the array arr. Specifically,  
ans[i] = |leftSum[i] - rightSum[i]|, where leftSum[i] is the sum of all elements to the left of index i and rightSum[i] is the sum of all elements to the right of index i.

Examples:

Input: arr[] = [10, 4, 8, 3], N = 4
Output: [15, 1, 11, 22]
Explanation: The array leftSum is [0, 10, 14, 22] and the array rightSum is [15, 11, 3, 0]. The array ans is [|0 - 15|, |10 - 11|, |14 - 3|, |22 - 0|] = [15, 1, 11, 22].

Input: arr[] = [5], N =1
Output: [0]
Explanation: The array leftSum is [0] and the array rightSum is [0]. The array answer is [|0 - 0|] = [0].

Approach 1:Bruteforce approach

The brute force approach calculates the absolute difference between the left and right subarrays of each element in the input array by iterating through the array and summing the elements to the left and right of each element separately. This approach has a time complexity of O(N^2), where N is the size of the input array.

Below are the steps involved in the implementation of the code:

1. Create an empty list res to store the absolute difference for each element of the input array.
2. Iterate through the input array arr, and for each element arr[i], do the following:
    2.1 Create two variables leftSum and rightSum, both initialized to 0. These variables will store the sum of the elements to the left and to the right          of the current element, respectively.
    2.2 Calculate the left sum by iterating from the first element of the array up to arr[i]-1 and adding each element to leftSum.
    2.3 Calculate the right sum by iterating from arr[i]+1 up to the last element of the array and adding each element to rightSum.
    2.4 Calculate the absolute difference between leftSum and rightSum using Math.abs(leftSum - rightSum).
    2.5  Add the absolute difference to the result list res.
3. Return the result list res.

Below is the implementation for the above approach:


Output
15 1 11 22 

Time complexity: O(N^2)
Auxiliary Space: O(1) 

Approach 2 : This can be solved with the following idea:

Calculate the sum of all array arr[] in a variable. Iterate in the array and find the absolute difference between the left and right variables.

Below are the steps involved in the implementation of the code:

  • Store the sum of the array by iterating.
  • While iterating keep on adding the sum in the left variable.
  • Find the absolute difference between sum and left and update in the array.
  • Return the updated array arr.

Below is the implementation for the above approach:


Output
15 1 11 22 

Time complexity: O(N)
Auxiliary Space: O(1) 

Comment