给定整数数组arr [] ,其大小为N ,任务是在每个有效索引处打印所有可能的总和,这可以通过将第i个和第i个索引(基于1的索引)相加,直到i≤来获得。 N.
例子:
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.
天真的方法: 最简单的方法是遍历阵列和用于每第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)