# Replace all elements by difference of sums of positive and negative numbers after that element

Given an array of positive and negative elements. The task is to replace every i-th element of the array by the absolute difference of absolute sums of positive and negative elements in the range i+1 to N. That is, find the absolute sum of all positive elements and the absolute sum of all negative elements in the range i+1 to N. Now find the absolute difference of these two sums and replace with the i-th element.

Note: The last element of the updated array will be zero.

Examples:

```Input : N = 5,  arr[] = {1, -1, 2, 3, -2}
Output : arr[] = {2, 3, 1, 2, 0}

Input : N = 6,  arr[] = {-3, -4, -2, 5, 1, -2}
Output : arr[] = {2, 2, 4, 1, 2, 0}.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: The naive approach is to run two for loops and for all i-th elements, calculate abs value of the sum of all positive and negative elements with index in the range i+1 to N. Now find the absolute difference of both sums and replace with the i-th element.

Time complexity of this approach will be O(N2) where N is the number of elements in the array.

Below is the implementation of the above approach:

## C++

 `// C++ program to implement above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to print the array elements ` `void` `printArray(``int` `N, ``int` `arr[]) ` `{ ` `    ``for` `(``int` `i = 0; i < N; i++) ` `        ``cout << arr[i] << ``" "``; ` ` `  `    ``cout << endl; ` `} ` ` `  `// Function to replace all elements with absolute ` `// difference of absolute sums of positive ` `// and negatve elements ` `void` `replacedArray(``int` `N, ``int` `arr[]) ` `{ ` `    ``int` `pos_sum, neg_sum, i, j, diff; ` ` `  `    ``for` `(i = 0; i < N; i++) { ` `        ``pos_sum = 0; ` `        ``neg_sum = 0; ` ` `  `        ``// Calculate absolute sums of possitive ` `        ``// and negative elements in range i+1 to N ` `        ``for` `(j = i + 1; j < N; j++) { ` `            ``if` `(arr[j] > 0) ` `                ``pos_sum += arr[j]; ` `            ``else` `                ``neg_sum += arr[j]; ` `        ``} ` ` `  `        ``// calculate difference of both sums ` `        ``diff = ``abs``(pos_sum) - ``abs``(neg_sum); ` ` `  `        ``// replace i-th elements with absolute ` `        ``// difference ` `        ``arr[i] = ``abs``(diff); ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `N = 5; ` `    ``int` `arr[] = { 1, -1, 2, 3, -2 }; ` `    ``replacedArray(N, arr); ` `    ``printArray(N, arr); ` ` `  `    ``N = 6; ` `    ``int` `arr1[] = { -3, -4, -2, 5, 1, -2 }; ` `    ``replacedArray(N, arr1); ` `    ``printArray(N, arr1); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to implement above approach ` `class` `GFG ` `{ ` `     `  `// Function to print the array elements ` `static` `void` `printArray(``int` `N, ``int` `[]arr) ` `{ ` `    ``for` `(``int` `i = ``0``; i < N; i++) ` `        ``System.out.print(arr[i] + ``" "``); ` ` `  `    ``System.out.println(); ` `} ` ` `  `// Function to replace all elements with  ` `// absolute difference of absolute sums  ` `// of positive and negatve elements ` `static` `void` `replacedArray(``int` `N, ``int` `[]arr) ` `{ ` `    ``int` `pos_sum, neg_sum, i, j, diff; ` ` `  `    ``for` `(i = ``0``; i < N; i++)  ` `    ``{ ` `        ``pos_sum = ``0``; ` `        ``neg_sum = ``0``; ` ` `  `        ``// Calculate absolute sums of possitive ` `        ``// and negative elements in range i+1 to N ` `        ``for` `(j = i + ``1``; j < N; j++) ` `        ``{ ` `            ``if` `(arr[j] > ``0``) ` `                ``pos_sum += arr[j]; ` `            ``else` `                ``neg_sum += arr[j]; ` `        ``} ` ` `  `        ``// calculate difference of both sums ` `        ``diff = Math.abs(pos_sum) - Math.abs(neg_sum); ` ` `  `        ``// replace i-th elements with absolute ` `        ``// difference ` `        ``arr[i] = Math.abs(diff); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `N = ``5``; ` `    ``int` `[]arr = { ``1``, -``1``, ``2``, ``3``, -``2` `}; ` `    ``replacedArray(N, arr); ` `    ``printArray(N, arr); ` ` `  `    ``N = ``6``; ` `    ``int` `[]arr1 = { -``3``, -``4``, -``2``, ``5``, ``1``, -``2` `}; ` `    ``replacedArray(N, arr1); ` `    ``printArray(N, arr1); ` `} ` `} ` ` `  `// This code is contributed by Akanksha Rai `

## Python3

 `# Python 3 program to implement  ` `# above approach ` ` `  `# Function to print the array elements ` `def` `printArray(N, arr): ` `    ``for` `i ``in` `range``(N): ` `        ``print``(arr[i], end ``=` `" "``) ` ` `  `    ``print``(``"\n"``, end ``=` `"") ` ` `  `# Function to replace all elements with  ` `# absolute difference of absolute sums  ` `# of positive and negatve elements ` `def` `replacedArray(N, arr): ` `    ``for` `i ``in` `range``(N): ` `        ``pos_sum ``=` `0` `        ``neg_sum ``=` `0` ` `  `        ``# Calculate absolute sums of possitive ` `        ``# and negative elements in range i+1 to N ` `        ``for` `j ``in` `range``(i ``+` `1``, N, ``1``): ` `            ``if` `(arr[j] > ``0``): ` `                ``pos_sum ``+``=` `arr[j] ` `            ``else``: ` `                ``neg_sum ``+``=` `arr[j] ` ` `  `        ``# calculate difference of both sums ` `        ``diff ``=` `abs``(pos_sum) ``-` `abs``(neg_sum) ` ` `  `        ``# replace i-th elements with absolute ` `        ``# difference ` `        ``arr[i] ``=` `abs``(diff) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``N ``=` `5` `    ``arr ``=` `[``1``, ``-``1``, ``2``, ``3``, ``-``2``] ` `    ``replacedArray(N, arr) ` `    ``printArray(N, arr) ` ` `  `    ``N ``=` `6` `    ``arr1 ``=` `[``-``3``, ``-``4``, ``-``2``, ``5``, ``1``, ``-``2``] ` `    ``replacedArray(N, arr1) ` `    ``printArray(N, arr1) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# program to implement above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to print the array elements ` `static` `void` `printArray(``int` `N, ``int` `[]arr) ` `{ ` `    ``for` `(``int` `i = 0; i < N; i++) ` `        ``Console.Write(arr[i] + ``" "``); ` ` `  `    ``Console.WriteLine(); ` `} ` ` `  `// Function to replace all elements with  ` `// absolute difference of absolute sums  ` `// of positive and negatve elements ` `static` `void` `replacedArray(``int` `N, ``int` `[]arr) ` `{ ` `    ``int` `pos_sum, neg_sum, i, j, diff; ` ` `  `    ``for` `(i = 0; i < N; i++)  ` `    ``{ ` `        ``pos_sum = 0; ` `        ``neg_sum = 0; ` ` `  `        ``// Calculate absolute sums of possitive ` `        ``// and negative elements in range i+1 to N ` `        ``for` `(j = i + 1; j < N; j++) ` `        ``{ ` `            ``if` `(arr[j] > 0) ` `                ``pos_sum += arr[j]; ` `            ``else` `                ``neg_sum += arr[j]; ` `        ``} ` ` `  `        ``// calculate difference of both sums ` `        ``diff = Math.Abs(pos_sum) - Math.Abs(neg_sum); ` ` `  `        ``// replace i-th elements with absolute ` `        ``// difference ` `        ``arr[i] = Math.Abs(diff); ` `    ``} ` `} ` ` `  `// Driver code ` `static` `void` `Main() ` `{ ` `    ``int` `N = 5; ` `    ``int` `[]arr = { 1, -1, 2, 3, -2 }; ` `    ``replacedArray(N, arr); ` `    ``printArray(N, arr); ` ` `  `    ``N = 6; ` `    ``int` `[]arr1 = { -3, -4, -2, 5, 1, -2 }; ` `    ``replacedArray(N, arr1); ` `    ``printArray(N, arr1); ` `} ` `} ` ` `  `// This code is contributed by mits `

Output:

```2 3 1 2 0
2 2 4 1 2 0
```

Efficient Approach: Initialize positive and negative sums as 0. Now run a single for loop from the last element to the first element and calculate diff = abs(pos_sum) – abs(neg_sum).
Now if the i-th element is positive, add it to pos_sum otherwise add it to neg_sum. After all, replace the i-th element with absolute difference i.e. abs(diff).

Below is the implementation of the above approach:

## CPP

 `// C++ program to implement above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to print the array elements ` `void` `printArray(``int` `N, ``int` `arr[]) ` `{ ` `    ``for` `(``int` `i = 0; i < N; i++) ` `        ``cout << arr[i] << ``" "``; ` ` `  `    ``cout << endl; ` `} ` ` `  `// Function to replace all elements with absolute ` `// difference of absolute sums of positive ` `// and negatve elements ` `void` `replacedArray(``int` `N, ``int` `arr[]) ` `{ ` `    ``int` `pos_sum, neg_sum, i, j, diff; ` ` `  `    ``pos_sum = 0; ` `    ``neg_sum = 0; ` ` `  `    ``for` `(i = N - 1; i >= 0; i--) { ` ` `  `        ``// calculate differenbce of both sums ` `        ``diff = ``abs``(pos_sum) - ``abs``(neg_sum); ` ` `  `        ``// if i-th element is positive, ` `        ``// add it to positive sum ` `        ``if` `(arr[i] > 0) ` `            ``pos_sum += arr[i]; ` ` `  `        ``// if i-th element is negative, ` `        ``// add it to negative sum ` `        ``else` `            ``neg_sum += arr[i]; ` ` `  `        ``// replace i-th elements with ` `        ``// absolute difference ` `        ``arr[i] = ``abs``(diff); ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `N = 5; ` `    ``int` `arr[] = { 1, -1, 2, 3, -2 }; ` `    ``replacedArray(N, arr); ` `    ``printArray(N, arr); ` ` `  `    ``N = 6; ` `    ``int` `arr1[] = { -3, -4, -2, 5, 1, -2 }; ` `    ``replacedArray(N, arr1); ` `    ``printArray(N, arr1); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to implement above approach ` `class` `GFG ` `{ ` `     `  `    ``// Function to print the array elements ` `    ``static` `void` `printArray(``int` `N, ``int` `arr[]) ` `    ``{ ` `        ``for` `(``int` `i = ``0``; i < N; i++) ` `            ``System.out.print(arr[i] + ``" "``); ` `     `  `        ``System.out.println(); ` `    ``} ` `     `  `    ``// Function to replace all elements with absolute ` `    ``// difference of absolute sums of positive ` `    ``// and negatve elements ` `    ``static` `void` `replacedArray(``int` `N, ``int` `arr[]) ` `    ``{ ` `        ``int` `pos_sum, neg_sum, i, j, diff; ` `     `  `        ``pos_sum = ``0``; ` `        ``neg_sum = ``0``; ` `     `  `        ``for` `(i = N - ``1``; i >= ``0``; i--) ` `        ``{ ` `     `  `            ``// calculate differenbce of both sums ` `            ``diff = Math.abs(pos_sum) - Math.abs(neg_sum); ` `     `  `            ``// if i-th element is positive, ` `            ``// add it to positive sum ` `            ``if` `(arr[i] > ``0``) ` `                ``pos_sum += arr[i]; ` `     `  `            ``// if i-th element is negative, ` `            ``// add it to negative sum ` `            ``else` `                ``neg_sum += arr[i]; ` `     `  `            ``// replace i-th elements with ` `            ``// absolute difference ` `            ``arr[i] = Math.abs(diff); ` `        ``} ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``int` `N = ``5``; ` `        ``int` `arr[] = { ``1``, -``1``, ``2``, ``3``, -``2` `}; ` `        ``replacedArray(N, arr); ` `        ``printArray(N, arr); ` `     `  `        ``N = ``6``; ` `        ``int` `arr1[] = { -``3``, -``4``, -``2``, ``5``, ``1``, -``2` `}; ` `        ``replacedArray(N, arr1); ` `        ``printArray(N, arr1); ` `    ``} ` `} ` ` `  `// This code is contributed by ihritik `

## Python

 `# Python program to implement above approach ` ` `  `# Function to print the array elements ` `def` `printArray(N, arr) : ` ` `  `    ``for` `i ``in` `range` `(``0``, N) : ` `        ``print``(arr[i], end``=``" "``) ` ` `  `    ``print``() ` ` `  ` `  `# Function to replace all elements with absolute ` `# difference of absolute sums of positive ` `# and negatve elements ` `def` `replacedArray(N, arr) : ` ` `  `     `  `    ``pos_sum ``=` `0` `    ``neg_sum ``=` `0` ` `  `    ``for` `i ``in` `range` `(N ``-` `1``,``-``1``, ``-``1``) :  ` ` `  `        ``# calculate differenbce of both sums ` `        ``diff ``=` `abs``(pos_sum) ``-` `abs``(neg_sum) ` ` `  `        ``# if i-th element is positive, ` `        ``# add it to positive sum ` `        ``if` `(arr[i] > ``0``) : ` `            ``pos_sum ``=` `pos_sum ``+` `arr[i] ` ` `  `        ``# if i-th element is negative, ` `        ``# add it to negative sum ` `        ``else` `: ` `            ``neg_sum ``=` `neg_sum ``+` `arr[i] ` ` `  `        ``# replace i-th elements with ` `        ``# absolute difference ` `        ``arr[i] ``=` `abs``(diff) ` ` `  `# Driver Code ` ` `  `N ``=` `5` `arr ``=` `[ ``1``, ``-``1``, ``2``, ``3``, ``-``2` `] ` `replacedArray(N, arr) ` `printArray(N, arr) ` ` `  `N ``=` `6` `arr1 ``=` `[ ``-``3``, ``-``4``, ``-``2``, ``5``, ``1``, ``-``2` `] ` `replacedArray(N, arr1) ` `printArray(N, arr1) ` ` `  `# This code is contributed by ihritik `

## C#

 `// C# program to implement above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Function to print the array elements ` `    ``static` `void` `printArray(``int` `N, ``int` `[] arr) ` `    ``{ ` `        ``for` `(``int` `i = 0; i < N; i++) ` `            ``Console.Write(arr[i] + ``" "``); ` `     `  `        ``Console.WriteLine(); ` `    ``} ` `     `  `    ``// Function to replace all elements with absolute ` `    ``// difference of absolute sums of positive ` `    ``// and negatve elements ` `    ``static` `void` `replacedArray(``int` `N, ``int` `[] arr) ` `    ``{ ` `        ``int` `pos_sum, neg_sum, i, diff; ` `     `  `        ``pos_sum = 0; ` `        ``neg_sum = 0; ` `     `  `        ``for` `(i = N - 1; i >= 0; i--) ` `        ``{ ` `     `  `            ``// calculate differenbce of both sums ` `            ``diff = Math.Abs(pos_sum) - Math.Abs(neg_sum); ` `     `  `            ``// if i-th element is positive, ` `            ``// add it to positive sum ` `            ``if` `(arr[i] > 0) ` `                ``pos_sum += arr[i]; ` `     `  `            ``// if i-th element is negative, ` `            ``// add it to negative sum ` `            ``else` `                ``neg_sum += arr[i]; ` `     `  `            ``// replace i-th elements with ` `            ``// absolute difference ` `            ``arr[i] = Math.Abs(diff); ` `        ``} ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `N = 5; ` `        ``int` `[] arr = { 1, -1, 2, 3, -2 }; ` `        ``replacedArray(N, arr); ` `        ``printArray(N, arr); ` `     `  `        ``N = 6; ` `        ``int` `[] arr1 = { -3, -4, -2, 5, 1, -2 }; ` `        ``replacedArray(N, arr1); ` `        ``printArray(N, arr1); ` `    ``} ` `} ` ` `  `// This code is contributed by ihritik `

Output:

```2 3 1 2 0
2 2 4 1 2 0
```

Time complexity: O(N), where N is the number of elements. My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.