给定数组arr [] ,任务是将数组的每个元素替换为其右侧的元素总和。
例子:
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
天真的方法:一种简单的方法是运行两个循环,外循环一个接一个地固定每个元素,内循环计算固定元素右侧的元素总和。
时间复杂度:
高效的方法:想法是计算数组的总和,然后将当前元素更新为并在每个步骤中将总和更新为当前元素。
for i in arr:
arr[i] = sum - arr[i]
sum = arr[i]
下面是上述方法的实现:
C++
// C++ program to Replace every
// element of array with sum of
// elements on its right side
#include
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
时间复杂度: O(N)
辅助空间: O(1)