Replace every element of array with sum of elements on its right side
Last Updated :
28 Mar, 2023
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.
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);
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 sum - arr[i] 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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Replace every element of the array by sum of all other elements Given an array of size N, the task is to find the encrypted array. The encrypted array is obtained by replacing each element of the original array with the sum of the remaining array elements i.e. For every i, arr[i] = sumOfArrayElements - arr[i] Examples: Input: arr[] = {5, 1, 3, 2, 4} Output: 10 1
5 min read
Maximum sum of Array formed by replacing each element with sum of adjacent elements Given an array arr[] of size N, the task is to find the maximum sum of the Array formed by replacing each element of the original array with the sum of adjacent elements.Examples: Input: arr = [4, 2, 1, 3] Output: 23 Explanation: Replacing each element of the original array with the sum of adjacent
9 min read
Replace every element in a circular array by sum of next K elements Given a circular array arr[] of N integers and an integer K, the task is to print the array after the following operations: If K is non-negative, then replace A[i] with the sum of the next K elements.If K is negative, then replace it with the sum of previous K elements. A cyclic array means the next
15+ min read
Construct sum-array with sum of elements in given range You are given an array of n-elements and an odd-integer m. You have to construct a new sum_array from given array such that sum_array[i] = ?arr[j] for (i-(m/2)) < j (i+(m/2)). note : for 0 > j or j >= n take arr[j] = 0. Examples: Input : arr[] = {1, 2, 3, 4, 5}, m = 3 Output : sum_array = {
7 min read
Replace every array element by sum of previous and next Given an array of integers, update every element with sum of previous and next elements with following exceptions. First element is replaced by sum of first and second. Last element is replaced by sum of last and second last. Examples: Input : arr[] = { 2, 3, 4, 5, 6} Output : 5 6 8 10 11 Explanatio
7 min read
Rearrange given Array by replacing every element with the element located at mean of adjacent elements Given an array arr[]. The array contains numbers from 0 to N-1 only, where N is the size of arr[]. The task is to modify the array in such that for each i from 0â¤i<N, arr[i] = arr[ (arr[i-1] + arr[i+1]) / 2 ] There are a few exceptions: For first element of the array, arr[i-1] = 0; because previo
9 min read