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 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.
C++
// THIS CODE IS CONTRIBUTED BY KIRTI AGARWAL(KIRTIAGARWAL23121999) // 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){ for ( int i = 0; i<n; i++){ int sum = 0; for ( int j = i+1; j<n; j++){ sum += arr[j]; } arr[i] = sum; } for ( int i = 0; i<n; i++){ cout<<arr[i]<< " " ; } } // Driver code to test above function int main(){ int arr[] = { 1, 2, 5, 2, 2, 5 }; int n = sizeof (arr) / sizeof (arr[0]); replaceElement(arr, n); } |
Java
public class GFG { public static void main(String[] args) { int [] arr = { 1 , 2 , 5 , 2 , 2 , 5 }; int n = arr.length; replaceElement(arr, n); } /** * Function to replace every element of the array to the * sum of elements on the right side of the array * @param arr the input array of integers * @param n the size of the array */ public static void replaceElement( int [] arr, int n) { for ( int i = 0 ; i < n; i++) { int sum = 0 ; for ( int j = i + 1 ; j < n; j++) { sum += arr[j]; } arr[i] = sum; } for ( int i = 0 ; i < n; i++) { System.out.print(arr[i] + " " ); } } } |
Python3
# Function to replace every element # of the array to the sum of elements # on the right side of the array def replaceElement(arr, n): for i in range (n): sum = 0 for j in range (i + 1 , n): sum + = arr[j] arr[i] = sum for i in range (n): print (arr[i], end = " " ) # Driver code to test above function arr = [ 1 , 2 , 5 , 2 , 2 , 5 ] n = len (arr) replaceElement(arr, n) |
C#
using System; namespace GFG { class Program { static void Main( string [] args) { int [] arr = { 1, 2, 5, 2, 2, 5 }; int n = arr.Length; ReplaceElement(arr, n); } /** * Function to replace every element of the array to the * sum of elements on the right side of the array * @param arr the input array of integers * @param n the size of the array */ static void ReplaceElement( int [] arr, int n) { for ( int i = 0; i < n; i++) { int sum = 0; for ( int j = i + 1; j < n; j++) { sum += arr[j]; } arr[i] = sum; } for ( int i = 0; i < n; i++) { Console.Write(arr[i] + " " ); } } } } // This code is contributed by Prajwal Kandekar |
Javascript
// Function to replace every element of the array to the sum of elements on the right side of the array function replaceElement(arr) { for (let i = 0; i < arr.length; i++) { let sum = 0; for (let j = i + 1; j < arr.length; j++) { sum += arr[j]; } arr[i] = sum; } console.log(arr.join( " " )); } // Driver code to test above function let arr = [1, 2, 5, 2, 2, 5]; replaceElement(arr); |
Output
16 14 9 7 5 0
Time Complexity: O(N^2)
Auxiliary Space : O(1)
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 |
Javascript
<script> // JavaScript 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 function replaceElement(arr, n) { let sum = 0; // Calculate sum of all // elements of array for (let i = 0; i < n; i++) sum += arr[i]; // Traverse the array for (let 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 (let i = 0; i < n; i++) document.write(arr[i] + " " ); } // Driver code let arr = [ 1, 2, 5, 2, 2, 5 ]; let n = arr.length; replaceElement(arr, n); //This code is contributed by Mayank Tyagi </script> |
Output:
16 14 9 7 5 0
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...