Open In App

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

Last Updated : 19 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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[1] = 4.
For i = 2, arr[2] = arr[2] + arr[2 + arr[2]] = arr[2] + arr[2 + 1] = arr[2] + arr[3] = 1 + 4 = 5.
For i = 3, arr[3] = 4.

Input: arr[] = {1, 2, 7, 1, 8}
Output: 12 11 7 9 8
Explanation:
For i = 1, arr[1] = arr[1] + arr[1 + 1] + arr[1 + 1 + 2] + arr[1 + 1 + 2 + 1] = arr[1] + arr[2] + arr[4] + arr[5] = 1 + 2 + 1 + 8 = 12.
For i = 2, arr[2] = arr[2] + arr[2 + 2] + arr[2 + 2 + 1] = 2 + 1 + 8 = 11.
For i = 3, arr[3] = 7.
For i = 4,arr[4] = arr[4] + arr[4 + 1] = 1 + 8 = 9.
For i = 5, the sum will be arr[5] = 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 <bits/stdc++.h>
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]];
 
    # Print 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




<script>
 
// JavaScript program for
// the above approach
  
// Function to maximize value
// at every array index by
// performing given operations
function maxSum(arr, N)
{
    let ans = 0;
 
    // Traverse the array in reverse
    for (let i = N - 1; i >= 0; i--) {
 
        let 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 (let i = 0; i < N; i++) {
         document.write(arr[i] + ' ');
    }
}
  
// Driver Code
  
    // Given array
    let arr = [ 1, 2, 7, 1, 8 ];
 
    // Size of the array
    let N = arr.length;
 
    maxSum(arr, N);
  
</script>


Output: 

12 11 7 9 8

 

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads