给定一个由N个整数组成的数组arr [] ,任务是使用以下类型的最小操作数来修改数组,使arr [index] = index 。
- 选择任何索引i和任何整数X ,并将X添加到[0,i]范围内的所有元素。
- 选择任何索引i和任何整数X ,并将arr [j]更改为arr [j]%X ,其中0≤j≤i 。
对于执行的每个操作,打印以下内容:
- 对于第一次操作:打印1 i X
- 对于第二次操作:打印2 i X
注意:最多可以应用N +1个操作。
例子:
Input: arr[] = {7, 6, 3}, N = 3
Output:
1 2 5
1 1 2
1 0 1
2 2 3
Explanation:
1st operation: Adding 5 to all the elements till index 2 modifies array to {12, 11, 8}.
2nd operation: Adding 2 to all the elements till index 1 modifies array to {14, 13, 8}.
3rd operation: Adding 1 to all the elements till index 0 modifies array to {15, 13, 8}.
4th operation: Adding 3 to all the elements till index 2 modifies array to {0, 1, 2}.
So after 4 operations, the required array is obtained.
Input: arr[] = {3, 4, 5, 6}, N = 4
Output:
1 3 5
1 2 4
1 1 4
1 0 4
2 3 4
方法:此问题可以使用贪婪方法解决。步骤如下:
- 应用类型1的N个操作,其中第i个操作是通过以相反顺序遍历数组将X = (N + i –(arr [i]%N))加到索引i 。对于每第i次操作,打印“ 1 i X” 。
- 在上述N个运算之后,对于0≤i
数组将为arr [i]%N = i的形式。 - 必须执行另一操作,该操作是使用N对每个数组元素进行模运算,即运算“ 2(N-1)N” 。
- 在执行上述操作之后,对于每个索引i , arr [i] = i 。
下面是上述方法的实现:
C++
// CPP program for the above approach
#include
using namespace std;
// Function which makes the given
// array increasing using given
// operations
void makeIncreasing(int arr[], int N)
{
// The ith operation will be
// 1 i N + i - arr[i] % N
for (int x = N - 1; x >= 0; x--)
{
int val = arr[x];
// Find the value to be added
// in each operation
int add = N - val % N + x;
// Print the operation
cout << "1 " << x << " " << add << endl;
// Performing the operation
for (int y = x; y >= 0; y--) {
arr[y] += add;
}
}
// Last modulo with N operation
int mod = N;
cout << "2 " << N - 1 << " " << mod << endl;
for (int x = N - 1; x >= 0; x--) {
arr[x] %= mod;
}
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 7, 6, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
makeIncreasing(arr, N);
}
Java
// Java Program for the above approach
import java.util.*;
class GFG
{
// Function which makes the given
// array increasing using given
// operations
static void makeIncreasing(int arr[], int N)
{
// The ith operation will be
// 1 i N + i - arr[i] % N
for (int x = N - 1; x >= 0; x--)
{
int val = arr[x];
// Find the value to be added
// in each operation
int add = N - val % N + x;
// Print the operation
System.out.println("1"
+ " " + x + " " + add);
// Performing the operation
for (int y = x; y >= 0; y--)
{
arr[y] += add;
}
}
// Last modulo with N operation
int mod = N;
System.out.println("2"
+ " " + (N - 1) + " " + mod);
for (int x = N - 1; x >= 0; x--)
{
arr[x] %= mod;
}
}
// Driver code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 7, 6, 3 };
int N = arr.length;
// Function Call
makeIncreasing(arr, N);
}
}
Python3
# python Program for the above problem
# Function which makes the given
# array increasing using given
# operations
def makeIncreasing(arr, N):
# The ith operation will be
# 1 i N + i - arr[i] % N
for x in range( N - 1 , -1, -1):
val = arr[x]
# Find the value to be added
# in each operation
add = N - val % N + x
# Print the operation
print("1" + " " + str(x) + " " + str(add))
# Performing the operation
for y in range(x, -1, -1):
arr[y] += add
# Last modulo with N operation
mod = N;
print("2" + " " + str(N - 1) + " " + str(mod))
for i in range( N - 1, -1, -1):
arr[i] = arr[i] % mod
# Driver code
# Given array arr
arr = [ 7, 6, 3 ]
N = len(arr)
# Function Call
makeIncreasing(arr, N)
C#
// C# Program for the above approach
using System;
class GFG
{
// Function which makes the given
// array increasing using given
// operations
static void makeIncreasing(int[] arr, int N)
{
// The ith operation will be
// 1 i N + i - arr[i] % N
for (int x = N - 1; x >= 0; x--)
{
int val = arr[x];
// Find the value to be added
// in each operation
int add = N - val % N + x;
// Print the operation
Console.WriteLine("1"
+ " " + x + " " + add);
// Performing the operation
for (int y = x; y >= 0; y--)
{
arr[y] += add;
}
}
// Last modulo with N operation
int mod = N;
Console.WriteLine("2"
+ " " + (N - 1) + " " + mod);
for (int x = N - 1; x >= 0; x--) {
arr[x] %= mod;
}
}
// Driver code
public static void Main()
{
// Given array arr[]
int[] arr = new int[] { 7, 6, 3 };
int N = arr.Length;
// Function Call
makeIncreasing(arr, N);
}
}
1 2 5
1 1 2
1 0 1
2 2 3
时间复杂度: O(N 2 )
辅助空间: O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。