给定一个大小为N / 2的数组A[] ,任务是构造大小为 N 的数组B[]使得:
- B[] 按非递减顺序排序。
- A[i] = B[i] + B[n – i + 1]。
注意:数组A[]以这样一种方式给出,即答案总是可能的。
例子:
Input: A[] = {3, 4}
Output: 0 1 3 3
Input: A[] = {4, 1}
Output: 0 0 1 4
方法:让我们介绍以下贪心方法。数字将成对恢复(B[0], B[n – 1]) , (B[1], B[n – 2])等等。因此,我们可以对当前对的值进行一些限制(满足有关排序结果的标准)。
最初, l = 0和r = 10 9 ,它们被更新为l = a[i]和r = a[n – i + 1] 。让我在答案中尽可能小。取a[i] = max(l, b[i] – r)和r = b[i] – l ,这样l 的选择方式使得l和r都在限制范围内,而且l也是最小的可能的。
如果l大于我们将向上移动l限制和向下移动r限制,从而为以后的选择留下更少的自由。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Utility function to print
// the contents of the array
void printArr(int b[], int n)
{
for (int i = 0; i < n; i++)
cout << b[i] << " ";
}
// Function to build array B[]
void ModifiedArray(int a[], int n)
{
// Lower and upper limits
int l = 0, r = INT_MAX;
// To store the required array
int b[n] = { 0 };
// Apply greedy approach
for (int i = 0; i < n / 2; i++) {
b[i] = max(l, a[i] - r);
b[n - i - 1] = a[i] - b[i];
l = b[i];
r = b[n - i - 1];
}
// Print the built array b[]
printArr(b, n);
}
// Driver code
int main()
{
int a[] = { 5, 6 };
int n = sizeof(a) / sizeof(a[0]);
ModifiedArray(a, 2 * n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class solution
{
// Utility function to print
// the contents of the array
void printArr(int b[], int n)
{
for (int i = 0; i < n; i++)
{
System.out.print(" " + b[i] + " ");
}
}
// Function to build array B[]
void ModifiedArray(int a[], int n)
{
// Lower and upper limits
int l = 0, r = Integer.MAX_VALUE;
// To store the required array
int[] b = new int[n];
// Apply greedy approach
for (int i = 0; i < n / 2; i++) {
b[i] = Math.max(l, a[i] - r);
b[n - i - 1] = a[i] - b[i];
l = b[i];
r = b[n - i - 1];
}
// Print the built array b[]
printArr(b, n);
}
// Driver code
public static void main(String args[])
{
int a[] = { 5, 6 };
int n = a.length ;
solution s=new solution();
s.ModifiedArray(a, 2 * n);
}
}
//This code is contributed by Shivi_Aggarwal
Python3
# Python 3 implementation of the approach
import sys
# Utility function to print the
# contents of the array
def printArr(b, n):
for i in range(0, n, 1):
print(b[i], end = " ")
# Function to build array B[]
def ModifiedArray(a, n):
# Lower and upper limits
l = 0
r = sys.maxsize
# To store the required array
b = [0 for i in range(n)]
# Apply greedy approach
for i in range(0, int(n / 2), 1):
b[i] = max(l, a[i] - r)
b[n - i - 1] = a[i] - b[i]
l = b[i]
r = b[n - i - 1]
# Print the built array b[]
printArr(b, n)
# Driver code
if __name__ == '__main__':
a = [5, 6]
n = len(a)
ModifiedArray(a, 2 * n)
# This code is contributed by
# Shashank_Sharma
C#
// C# implementation of the approach
using System;
public class GFG{
// Utility function to print
// the contents of the array
static void printArr(int []b, int n)
{
for (int i = 0; i < n; i++)
{
Console.Write(" " + b[i] + " ");
}
}
// Function to build array B[]
static void ModifiedArray(int []a, int n)
{
// Lower and upper limits
int l = 0, r = int.MaxValue;
// To store the required array
int[] b = new int[n];
// Apply greedy approach
for (int i = 0; i < n / 2; i++) {
b[i] = Math.Max(l, a[i] - r);
b[n - i - 1] = a[i] - b[i];
l = b[i];
r = b[n - i - 1];
}
// Print the built array b[]
printArr(b, n);
}
// Driver code
static public void Main (){
int []a = { 5, 6 };
int n = a.Length;
ModifiedArray(a, 2 * n);
}
}
// This code is contributed
// by Sach_Code
PHP
Javascript
输出:
0 1 5 5