📌  相关文章
📜  通过重复添加所有有效的第i + a [i]个数组元素来最大化每个数组元素

📅  最后修改于: 2021-05-04 16:06:40             🧑  作者: Mango

给定大小为N的整数数组arr [] ,任务是在每个有效索引处打印所有可能的和,可以通过将i + a [i] (基于1的索引)的后续元素相加直到i≤来获得N.

例子:

天真的方法: 最简单的方法是遍历阵列和用于每i指数,不断更新ARR [I]Arr第[i + ARR [I]]虽然我≤N和该索引处打印的总和。

时间复杂度: O(N 2 )
辅助空间: O(N)

高效方法:可以通过反向遍历数组并将每个访问索引的和存储到当前索引来优化上述方法。最后,打印数组。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to maximize value
// at every array index by
// performing given operations
int maxSum(int arr[], int N)
{
    int ans = 0;
 
    // Traverse the array in reverse
    for (int i = N - 1; i >= 0; i--) {
 
        int t = i;
 
        // If the current index
        // is a valid index
        if (t + arr[i] < N) {
            arr[i] += arr[t + arr[i]];
        }
    }
 
    // Print the array
    for (int i = 0; i < N; i++) {
        cout << arr[i] << ' ';
    }
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 2, 7, 1, 8 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    maxSum(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to maximize value
// at every array index by
// performing given operations
static void maxSum(int[] arr, int N)
{
    int ans = 0;
 
    // Traverse the array in reverse
    for(int i = N - 1; i >= 0; i--)
    {
        int t = i;
 
        // If the current index
        // is a valid index
        if (t + arr[i] < N)
        {
            arr[i] += arr[t + arr[i]];
        }
    }
 
    // Print the array
    for(int i = 0; i < N; i++)
    {
        System.out.print(arr[i] + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given array
    int[] arr = { 1, 2, 7, 1, 8 };
 
    // Size of the array
    int N = arr.length;
 
    maxSum(arr, N);
}
}
 
// This code is contributed by Dharanendra L V


Python3
# Python program for the above approach
 
# Function to maximize value
# at every array index by
# performing given operations
def maxSum(arr, N):
    ans = 0;
 
    # Traverse the array in reverse
    for i in range(N - 1, -1, -1):
        t = i;
 
        # If the current index
        # is a valid index
        if (t + arr[i] < N):
            arr[i] += arr[t + arr[i]];
 
    # Prthe array
    for i in range(N):
        print(arr[i], end = " ");
 
# Driver Code
if __name__ == '__main__':
   
    # Given array
    arr = [1, 2, 7, 1, 8];
 
    # Size of the array
    N = len(arr);
    maxSum(arr, N);
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
 
class GFG {
 
// Function to maximize value
// at every array index by
// performing given operations
static void maxSum(int[] arr, int N)
{
     
    // Traverse the array in reverse
    for(int i = N - 1; i >= 0; i--)
    {
        int t = i;
         
        // If the current index
        // is a valid index
        if (t + arr[i] < N)
        {
            arr[i] += arr[t + arr[i]];
        }
    }
 
    // Print the array
    for(int i = 0; i < N; i++)
    {
        Console.Write(arr[i] + " ");
    }
}
 
// Driver Code
static public void Main()
{
     
    // Given array
    int[] arr = { 1, 2, 7, 1, 8 };
 
    // Size of the array
    int N = arr.Length;
 
    maxSum(arr, N);
}
}
 
// This code is contributed by Dharanendra L V


Javascript


输出:
12 11 7 9 8

时间复杂度: O(N)
辅助空间: O(1)