Given an array arr[], the task is to replace every element of the array with the sum of elements on its right side.
Examples:
Input: arr[] = {1, 2, 5, 2, 2, 5}
Output: 16 14 9 7 5 0Input: arr[] = {5, 1, 3, 2, 4}
Output: 10 9 6 4 0
Naive Approach: A simple approach is to run two loops, Outer loop to fix each element one by one and inner loop to calculate sum of elements on right side of fixed element.
Time Complexity: ![]()
Efficient Approach: The idea is to compute the total sum of the array and then update the current element as the
and in each step update the sum to the current element.
for i in arr: arr[i] = sum - arr[i] sum = arr[i]
Below is the implementation of the above approach:
C++
// C++ program to Replace every // element of array with sum of // elements on its right side #include<bits/stdc++.h> using namespace std; // Function to replace every element // of the array to the sum of elements // on the right side of the array void replaceElement(int arr[], int n) { int sum = 0; // Calculate sum of all // elements of array for(int i = 0; i < n; i++) sum += arr[i]; // Traverse the array for(int i = 0; i < n; i++) { // Replace current element // of array with sum-arr[i] arr[i] = sum - arr[i]; // Update sum with arr[i] sum = arr[i]; } // Print modified array for(int i = 0; i < n; i++) cout << arr[i] << " "; } // Driver code int main() { int arr[] = { 1, 2, 5, 2, 2, 5 }; int n = sizeof(arr) / sizeof(arr[0]); replaceElement(arr, n); } // This code is contributed by Surendra_Gangwar |
Java
// Java program to Replace every // element of array with sum of // elements on its right side import java.util.*; class GFG { // Function to replace every element // of the array to the sum of elements // on the right side of the array static void replaceElement(int[] arr, int n) { int sum = 0; // Calculate sum of all // elements of array for (int i = 0; i < n; i++) sum += arr[i]; // Traverse the array for (int i = 0; i < n; i++) { // Replace current element // of array with sum-arr[i] arr[i] = sum - arr[i]; // Update sum with arr[i] sum = arr[i]; } // Print modified array for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Driver code public static void main(String[] args) { int[] arr = { 1, 2, 5, 2, 2, 5 }; int n = arr.length; replaceElement(arr, n); } } |
Python3
# Python3 program to replace every # element of array with sum of # elements on its right side # Function to replace every element # of the array to the sum of elements # on the right side of the array def replaceElement(arr, n): sum = 0; # Calculate sum of all # elements of array for i in range(0, n): sum += arr[i]; # Traverse the array for i in range(0, n): # Replace current element # of array with sum-arr[i] arr[i] = sum - arr[i]; # Update sum with arr[i] sum = arr[i]; # Print modified array for i in range(0, n): print(arr[i], end = " "); # Driver Code if __name__ == "__main__": arr = [ 1, 2, 5, 2, 2, 5 ] n = len(arr) replaceElement(arr, n) # This code is contributed by Ritik Bansal |
C#
// C# program to Replace every // element of array with sum of // elements on its right side using System; class GFG{ // Function to replace every element // of the array to the sum of elements // on the right side of the array static void replaceElement(int[] arr, int n) { int sum = 0; // Calculate sum of all // elements of array for (int i = 0; i < n; i++) sum += arr[i]; // Traverse the array for (int i = 0; i < n; i++) { // Replace current element // of array with sum-arr[i] arr[i] = sum - arr[i]; // Update sum with arr[i] sum = arr[i]; } // Print modified array for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); } // Driver code public static void Main() { int[] arr = { 1, 2, 5, 2, 2, 5 }; int n = arr.Length; replaceElement(arr, n); } } // This code is contributed by Nidhi_biet |
16 14 9 7 5 0
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:
- 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 with the least greater element on its right
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- 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 at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Ceiling in right side for every element in an array
- Replace every elements in the array by its frequency in the array
- Queries to replace every array element by its XOR with a given value with updates
- Replace elements with absolute difference of smallest element on left and largest element on right
- Replace every element of the array by sum of all other elements
- Replace every element in a circular array by sum of next K elements
- Count the number of elements which are greater than any of element on right side of an array
- Count of smaller elements on right side of each element in an Array using Merge sort
- Steps to make array empty by removing maximum and its right side
- Replace every array element by Bitwise Xor of previous and next element
- Replace every element with the smallest of all other array elements
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.

