Replace every element of array with sum of elements on its right side

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 0

Input: 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output:
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.





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.



Article Tags :
Practice Tags :