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

📅  最后修改于: 2021-09-03 13:55:30             🧑  作者: Mango

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

例子:

天真的方法: 最简单的方法是遍历数组,对于每个i索引,当i ≤ N 时,不断将arr[i]更新为arr[i + arr[i]]并打印该索引处的总和。

时间复杂度: 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)

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