给定一个大小为N的数组arr[]包含从 1 到 N 的任意顺序的不同数字,任务是根据以下规则在该数组中执行修改后的范围和。
对于数组arr 中的每个索引“ i ”:
- 范围 ‘ L ‘ 的起始索引选择为i + 1
- 范围 ‘ R ‘ 的结束索引选择为:
- 分钟(arr[i],N-1) ;如果 arr[i] > i
- max(i+1, arr[i]) ;如果 arr[i] < i+1
- 对于更新,范围 arr[L] 到 arr[R] 中的值增加 1。
- 使用输入数组而不是更新的数组找到范围
例子:
Input: arr[] = {4, 1, 3, 2}
Output: 4 2 5 4
Explanation:
For i = 0 -> Element in input array = 4. Therefore L = 1, and R = min(4, N-1) = 3. Hence, all the elements from arr[1] to arr[3] are incremented by 1. The elements after update operation are {4, 2, 4, 3}.
For i = 1 -> Element in input array = 1. Therefore L = 2, and R = max(1, i+1) = 2. Hence, all the elements from arr[2] to arr[2] are incremented by 1. The elements after update operation are {4, 2, 5, 3}.
For i = 2 -> Element in input array = 3. Therefore L = 3, and R = min(3, N-1) = 3. Hence, all the elements from arr[3] to arr[3] are incremented by 1. The elements after update operation are {4, 2, 5, 4}.
For i = 3 -> The array is unaffected. Therefore the elements after update operation are {4, 2, 5, 4}.
The resulting array is {4, 2, 5, 4}.
Input: arr[] = {2, 1}
Output: {2, 2}
Explanation:
The first element is 2. So arr[1] gets incremented by 1. Hence, the resulting array is {2, 2}.
朴素的方法:朴素的方法是为每个元素运行一个循环,并将从 arr[i+1] 到 arr[min(i+arr[i], N-1)] 的所有值增加 1。这种方法是O(N 2 ) 。
高效的方法:这个问题可以通过使用 O(N) 的额外空间在 O(N) 中解决。这个想法是使用前缀和数组的概念。按照以下步骤计算答案:
- 声明了一个大小为N + 1的数组b[]并且所有元素都初始化为 0。
- 对于给定数组中的每个元素 arr[i] ,将 1 添加到b[i+1]并从b[min(i + arr[i], N – 1)+ 1] 中减去。
- 然后,计算数组b[] 的前缀和。
- 最后, arr 更新为arr[i] = arr[i] + b[i] 。
下面是上述方法的实现:
C++
// C++ program to find elements in an array
// after performing range updates.
#include
using namespace std;
// Function to perform the update on the given
// input array arr[].
void update(vector& arr,
vector& d, int n)
{
// A new array of size N+1 is defined
// and 1's are added in that array
for (int i = 0; i < n - 1; i++) {
d[i + 1] += 1;
d[min(i + arr[i], n - 1) + 1] -= 1;
}
// Loop to perform the prefix sum
// on the array d[].
for (int i = 1; i < n; i++) {
d[i] = d[i] + d[i - 1];
}
}
// Function to print the final
// array after updation
void print(vector& arr,
vector& d, int n)
{
// Loop to add the values of d[i]
// to arr[i]
for (int i = 0; i < n; i++)
cout << arr[i] + d[i] << " ";
}
// Function to perform modified range sum
void modifiedRangeSum(vector& arr, int n)
{
vector d;
// Loop to add N+1 0's in array d[]
for (int i = 0; i <= n; i++)
d.push_back(0);
update(arr, d, n);
print(arr, d, n);
}
// Driver code
int main()
{
vector arr = { 5, 4, 1, 3, 2 };
int n = 5;
modifiedRangeSum(arr, n);
return 0;
}
Java
// Java program to find elements in an array
// after performing range updates.
import java.util.*;
class GFG{
static ArrayList arr = new
ArrayList(Arrays.asList(5, 4, 1, 3, 2));
static int n = 5;
// Function to perform the update on the given
// input array arr[].
static void update(ArrayList d)
{
// A new array of size N+1 is defined
// and 1's are added in that array
for (int i = 0; i < n - 1; i++) {
d.set(i + 1,d.get(i+1)+1);
int x = Math.min(i + arr.get(i), n - 1)+ 1;
d.set(x,d.get(x)-1);
}
// Loop to perform the prefix sum
// on the array d[].
for (int i = 1; i < n; i++) {
d.set(i,d.get(i)+d.get(i - 1));
}
}
// Function to print the final
// array after updation
static void print(ArrayList d)
{
// Loop to add the values of d[i]
// to arr[i]
for (int i = 0; i < n; i++)
System.out.print(arr.get(i) + d.get(i)+ " ");
}
// Function to perform modified range sum
static void modifiedRangeSum()
{
ArrayList d = new ArrayList();
// Loop to add N+1 0's in array d[]
for (int i = 0; i <= n; i++)
d.add(0);
update(d);
print(d);
}
// Driver code
public static void main(String args[])
{
modifiedRangeSum();
}
}
// This code is contributed by Surendra_Gangwar
Python3
# Python3 program to find elements in an array
# after performing range updates.
arr = []
d = []
# Function to perform the update on the given
# input array arr[].
def update( n):
global d
global arr
# A new array of size N+1 is defined
# and 1's are added in that array
for i in range(n - 1):
d[i + 1] += 1
d[min(i + arr[i], n - 1) + 1] -= 1
# Loop to perform the prefix sum
# on the array d[].
for i in range(n):
d[i + 1] = d[i + 1] + d[i ]
# Function to print the final
# array after updation
def print_( n):
global d
global arr
# Loop to add the values of d[i]
# to arr[i]
for i in range(n):
x = (arr[i] + d[i] )
print(x, end = " ")
# Function to perform modified range sum
def modifiedRangeSum( n):
global d
global arr
d = []
# Loop to add N+1 0's in array d[]
for i in range(n + 1):
d.append(0)
update( n)
print_(n)
# Driver code
arr = [5, 4, 1, 3, 2]
n = 5
modifiedRangeSum( n)
# This code is contributed by Arnab Kundu
C#
// C# program to find elements in an array
// after performing range updates.
using System;
class GFG {
// Function to perform the update on the given
// input array arr[].
static void update(int []arr,int [] d, int n){
// A new array of size N+1 is defined
// and 1's are added in that array
for (int i = 0; i < n - 1; i++) {
d[i + 1] += 1;
d[Math.Min(i + arr[i], n - 1) + 1] -= 1;
}
// Loop to perform the prefix sum
// on the array d[].
for (int i = 1; i < n; i++) {
d[i] = d[i] + d[i - 1];
}
}
// Function to print the final
// array after updation
static void print(int []arr,int []d, int n)
{
// Loop to add the values of d[i]
// to arr[i]
for (int i = 0; i < n; i++)
Console.Write((arr[i] + d[i])+" ");
}
// Function to perform modified range sum
static void modifiedRangeSum(int []arr, int n)
{
int []d= new int[n+1];
// Loop to add N+1 0's in array d[]
for (int i = 0; i <= n; i++)
d[i]=0;
update(arr, d, n);
print(arr, d, n);
}
// Driver code
public static void Main()
{
int [] arr = { 5, 4, 1, 3, 2 };
int n = 5;
modifiedRangeSum(arr, n);
}
}
// This code is contributed by mohit kumar 29
Javascript
5 5 3 6 5
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live