Related Articles

# Maximize every array element by repeatedly adding all valid i+a[i]th array element

• Last Updated : 15 Mar, 2021

Given an array of integers, arr[] of size N, the task is to print all possible sum at each valid index, that can be obtained by adding i + a[i]th (1-based indexing) subsequent elements till i ≤ N.

Examples:

Input: arr[] = {4, 1, 4}
Output: 4 5 4
Explanation:
For i = 1, arr = 4.
For i = 2, arr = arr + arr[2 + arr] = arr + arr[2 + 1] = arr + arr = 1 + 4 = 5.
For i = 3, arr = 4.

Input: arr[] = {1, 2, 7, 1, 8}
Output: 12 11 7 9 8
Explanation:
For i = 1, arr = arr + arr[1 + 1] + arr[1 + 1 + 2] + arr[1 + 1 + 2 + 1] = arr + arr + arr + arr = 1 + 2 + 1 + 8 = 12.
For i = 2, arr = arr + arr[2 + 2] + arr[2 + 2 + 1] = 2 + 1 + 8 = 11.
For i = 3, arr = 7.
For i = 4,arr = arr + arr[4 + 1] = 1 + 8 = 9.
For i = 5, the sum will be arr = 8.

Naive Approach: The simplest approach is to traverse the array and for every ith index, keep updating arr[i] to arr[i + arr[i]] while i ≤ N and print the sum at that index.

Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Approach: The above approach can be optimized by traversing the array in reverse and store the sum for every visited index to the current index. Finally, print the array.

Below is the implementation of the above approach:

## 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);` `    ``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

 ``
Output:
`12 11 7 9 8`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up