📌  相关文章
📜  用其右侧的元素总和替换数组的每个元素

📅  最后修改于: 2021-09-06 06:45:03             🧑  作者: Mango

给定一个数组arr[] ,任务是将数组的每个元素替换为其右侧元素的总和。
例子:

朴素的方法:一个简单的方法是运行两个循环,外循环一个一个固定每个元素,内循环计算固定元素右侧的元素总和。
时间复杂度: O(N^{2})
高效方法:思想是计算数组的总和,然后将当前元素更新为sum - arr[i]    并在每一步中更新当前元素的总和。

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


Javascript


输出:
16 14 9 7 5 0

时间复杂度: O(N)

辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live