给定一个大小为N的数组arr[] ,任务是通过恰好 3 次应用以下操作将每个数组元素转换为0 :
- 选择一个子数组。
- 将子数组的每个元素增加其长度的整数倍。
最后,打印所涉及的子数组的第一个和最后一个索引,以及在每个步骤中添加到子数组的每个索引的元素。
例子:
Input: arr[] = {1, 3, 2, 4}
Output:
Operation 1: 1 1
Added elements: 1
Operation 2: 3 4
Added elements: 4 2
Operation 3: 2 4
Added elements: -3 -6 -6
Explanation:
Step 1: Select subarray {arr[1]} and add -1 to the only element in the subarray. Hence, the array arr[] modifies to {0, 3, 2, 4}.
Step 2: Select subarray {arr[3], arr[4]} and add {4, 2} to the subarray elements respectively. Hence, the array arr[] modifies to {0, 3, 6, 6}.
Step 3: Select subarray {arr[2], arr[4]} and add {-3, -6, -6} to the subarray elements respectively. Hence, the array arr[] modifies to {0, 0, 0, 0}
Input: arr[] = { 5 }
Output:
Operation 1 : 1 1
Added elements: -5
Operation 2 : 1 1
Added elements: 5
Operation 3 : 1 1
Added elements: -5
方法:根据以下观察可以解决给定的问题:
Operation 1: Select subarray {arr[1], .., arr[N]}. Set A[i] = A[i] – N * A[i] = (N – 1) * (-A[i]).
After operation 1, each element is a multiple of (N – 1).
Operation 2: Select subarray {arr[1], .. arr[N – 1]}. Add / Subtract a multiple of (N – 1) to all values of the subarray till they reduce to 0.
Operation 3: Select subarray {arr[N]}, of size 1. Add / Subtract a multiple of 1 to make A[N] = 0.
请按照以下步骤解决问题:
- 如果N等于1 ,则分三步分别打印-arr[0], +arr[0], -arr[0] 。
- 否则,执行以下操作:
- 打印1 N 。从数组的每个元素中减去N * arr[i]打印减去的元素。
- 打印1 N – 1 。小号ubtract(N – 1)* ARR [I]从子阵列的每个元素,并打印所述经减去值。
- 最后,打印N 。小号ubtract ARR [I – 1]从元件。打印arr[i – 1] 。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to reduce all
// array elements to zero
void ConvertArray(int arr[], int N)
{
// If size of array is 1
if (N == 1) {
// First operation
cout << "Operation 1 : " << 1
<< " " << 1 << endl;
cout << "Added elements: "
<< -1 * arr[0] << endl;
cout << endl;
// 2nd Operation
cout << "Operation 2 : "
<< 1 << " " << 1 << endl;
cout << "Added elements: "
<< 1 * arr[0] << endl;
cout << endl;
// 3rd Operation
cout << "Operation 3 : "
<< 1 << " " << 1 << endl;
cout << "Added elements: "
<< -1 * arr[0] << endl;
}
// Otherwise
else {
// 1st Operation
cout << "Operation 1 : "
<< 1 << " " << N << endl;
cout << "Added elements: ";
for (int i = 0; i < N; i++) {
cout << -1 * arr[i] * N << " ";
}
cout << endl;
cout << endl;
// 2nd Operation
cout << "Operation 2 : "
<< 1 << " " << N - 1 << endl;
cout << "Added elements: ";
for (int i = 0; i < N - 1; i++) {
cout << arr[i] * (N - 1) << " ";
}
cout << endl;
cout << endl;
// 3rd Operation
cout << "Operation 3 : " << N
<< " " << N << endl;
cout << "Added elements: ";
cout << arr[N - 1] * (N - 1) << endl;
}
}
// Driver Code
int main()
{
// Input
int arr[] = { 1, 3, 2, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call to make all
// array elements equal to 0
ConvertArray(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to reduce all
// array elements to zero
static void ConvertArray(int arr[], int N)
{
// If size of array is 1
if (N == 1) {
// First operation
System.out.println("Operation 1 : " + 1
+ " " + 1 );
System.out.println("Added elements: "
+ -1 * arr[0] );
System.out.println();
// 2nd Operation
System.out.println("Operation 2 : "
+ 1 + " " + 1 );
System.out.println("Added elements: "
+ 1 * arr[0] );
System.out.println();
// 3rd Operation
System.out.println("Operation 3 : "
+ 1 + " " + 1 );
System.out.println("Added elements: "
+ -1 * arr[0] );
}
// Otherwise
else {
// 1st Operation
System.out.println("Operation 1 : "
+ 1 + " " + N );
System.out.print("Added elements: ");
for (int i = 0; i < N; i++) {
System.out.print(-1 * arr[i] * N + " ");
}
System.out.println();
System.out.println();
// 2nd Operation
System.out.println("Operation 2 : "
+ 1 + " " + (N - 1) );
System.out.print("Added elements: ");
for (int i = 0; i < N - 1; i++) {
System.out.print(arr[i] * (N - 1) + " ");
}
System.out.println();
System.out.println();
// 3rd Operation
System.out.println("Operation 3 : " + N
+ " " + N );
System.out.print("Added elements: ");
System.out.println(arr[N - 1] * (N - 1) );
}
}
// Driver code
public static void main(String[] args)
{
// Input
int arr[] = { 1, 3, 2, 4 };
int N = arr.length;
// Function call to make all
// array elements equal to 0
ConvertArray(arr, N);
}
}
// This code is contributed by souravghosh0416.
Python3
# Python 3 program of the above approach
# Function to reduce all
# array elements to zero
def ConvertArray(arr, N):
# If size of array is 1
if (N == 1):
# First operation
print("Operation 1 :",1,1)
print("Added elements:",-1 * arr[0])
print("\n",end = "")
# 2nd Operation
print("Operation 2 :",1,1)
print("Added elements:",1 * arr[0])
print("\n",end = "")
# 3rd Operation
print("Operation 3 :",1,1)
print("Added elements:",-1 * arr[0])
print("\n",end = "")
# Otherwise
else:
# 1st Operation
print("Operation 1 :",1,N)
print("Added elements:",end = " ")
for i in range(N):
print(-1 * arr[i] * N,end = " ")
print("\n")
# 2nd Operation
print("Operation 2 :",1,N - 1)
print("Added elements:",end = " ")
for i in range(N - 1):
print(arr[i] * (N - 1),end = " ")
print("\n")
# 3rd Operation
print("Operation 3 : ",N,N)
print("Added elements:",end = " ")
print(arr[N - 1] * (N - 1))
# Driver Code
if __name__ == '__main__':
# Input
arr = [1, 3, 2, 4]
N = len(arr)
# Function call to make all
# array elements equal to 0
ConvertArray(arr, N)
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG{
// Function to reduce all
// array elements to zero
static void ConvertArray(int[] arr, int N)
{
// If size of array is 1
if (N == 1) {
// First operation
Console.WriteLine("Operation 1 : " + 1
+ " " + 1 );
Console.WriteLine("Added elements: "
+ -1 * arr[0] );
Console.WriteLine();
// 2nd Operation
Console.WriteLine("Operation 2 : "
+ 1 + " " + 1 );
Console.WriteLine("Added elements: "
+ 1 * arr[0] );
Console.WriteLine();
// 3rd Operation
Console.WriteLine("Operation 3 : "
+ 1 + " " + 1 );
Console.WriteLine("Added elements: "
+ -1 * arr[0] );
}
// Otherwise
else {
// 1st Operation
Console.WriteLine("Operation 1 : "
+ 1 + " " + N );
Console.Write("Added elements: ");
for (int i = 0; i < N; i++) {
Console.Write(-1 * arr[i] * N + " ");
}
Console.WriteLine();
Console.WriteLine();
// 2nd Operation
Console.WriteLine("Operation 2 : "
+ 1 + " " + (N - 1) );
Console.Write("Added elements: ");
for (int i = 0; i < N - 1; i++) {
Console.Write(arr[i] * (N - 1) + " ");
}
Console.WriteLine();
Console.WriteLine();
// 3rd Operation
Console.WriteLine("Operation 3 : " + N
+ " " + N );
Console.Write("Added elements: ");
Console.WriteLine(arr[N - 1] * (N - 1) );
}
}
// Driver Code
public static void Main(string[] args)
{
// Input
int[] arr = { 1, 3, 2, 4 };
int N = arr.Length;
// Function call to make all
// array elements equal to 0
ConvertArray(arr, N);
}
}
// This code is contributed by code_hunt.
Javascript
Operation 1 : 1 4
Added elements: -4 -12 -8 -16
Operation 2 : 1 3
Added elements: 3 9 6
Operation 3 : 4 4
Added elements: 12
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。