给定大小为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
输出:
0 1 5 5
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。