📌  相关文章
📜  找到大小为 2*N 的非递减数组 brr[],使得每个 arr[i] 等于 brr[i] 和 brr[2*n – i +1] 的总和

📅  最后修改于: 2022-05-13 01:56:10.679000             🧑  作者: Mango

找到大小为 2*N 的非递减数组 brr[],使得每个 arr[i] 等于 brr[i] 和 brr[2*n – i +1] 的总和

给定一个大小为N的数组arr[] ,任务是找到另一个大小为2*N的数组brr[] ,使得它不递减,并且对于从1N的每个第 iarr[i] = brr[i] + brr[2*n – i +1]。

例子:

方法:数组brr[]的个数会成对恢复(brr[1], brr[2*n]), (brr[2], brr[2*n-1])等等。因此,满足上述条件brr[i]+brr[2*Ni-1]==arr[i]的这些值可能存在一定的限制。l尽可能小, r是答案中可能的最大值。最初, l=0, r=10^18并且它们被更新为l=brr[i] , r=brr[2*ni-1] 。请按照以下步骤解决问题:

  • 将变量l初始化为0 ,将r初始化为INF64。
  • N的值乘以2以保留第二个数组大小的计数。
  • 定义一个函数brute(ind, l, r)其中ind是要为其填充值的数组的索引, lr是值的范围。递归调用此函数以计算第二个数组brr[] 中每一对的值。
  • 在函数brute(ind, l, r)
    • 定义基本情况,即当ind的值等于第一个数组arr[] 的大小时。
    • 如果是,则打印第二个数组brr[]的元素并退出函数。
    • 否则,在[l, arr[ind]/2]范围内迭代并执行以下步骤。
      • 如果arr[ind]-i的值小于r,则将brr[ind]的值设置为i ,并将brr[n-ind-1]的值设置为arr[ind]-i。
      • l的值设置为i并将r设置为arr[ind]-i作为lr 的更新值。
      • 为下一个索引递归地调用相同的函数brute(ind+1, l, r)

下面是上述方法的实现。

C++
// C++ program for the above approach.
#include 
using namespace std;
 
const long long INF64 = 1000000000000000000ll;
const int N = 200 * 1000 + 13;
 
int n;
long long arr[N], brr[N];
 
// Function to find the possible
// output array
void brute(int ind, long long l, long long r)
{
    // Base case for the recursion
    if (ind == n / 2) {
        // If ind becomes half of the size
        // then print the array.
        for (int i = 0; i < int(n); i++)
            printf("%lld ", brr[i]);
        puts("");
        // Exit the function.
        exit(0);
    }
 
    // Iterate in the range.
    for (long long i = l; i <= arr[ind] / 2; ++i)
        if (arr[ind] - i <= r) {
            // Put the values in the respective
            // indices.
            brr[ind] = i;
            brr[n - ind - 1] = arr[ind] - i;
 
            // Call the function to find values for
            // other indices.
            brute(ind + 1, i, arr[ind] - i);
        }
}
 
// Driver Code
int main()
{
    n = 2;
    n *= 2;
 
    arr[0] = 5;
    arr[1] = 6;
 
    brute(0, 0, INF64);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
   
static int INF64 = (int)1e10;
static int N = 200 * 1000 + 13;
 
static int n;
static int arr[] = new int[N];
static int brr[] = new int[N];
 
// Function to find the possible
// output array
static void brute(int ind, int l, int r)
{
     
    // Base case for the recursion
    if (ind == n / 2)
    {
         
        // If ind becomes half of the size
        // then print the array.
        for(int i = 0; i < (int)n; i++)
            System.out.print(brr[i] + " ");
             
        // Exit the function. 
        System.exit(0);
    }
 
    // Iterate in the range.
    for(int i = l; i <= arr[ind] / 2; ++i)
        if (arr[ind] - i <= r)
        {
             
            // Put the values in the respective
            // indices.
            brr[ind] = i;
            brr[n - ind - 1] = arr[ind] - i;
 
            // Call the function to find values for
            // other indices.
            brute(ind + 1, i, arr[ind] - i);
        }
}
 
// Driver code
public static void main(String[] args)
{
    n = 2;
    n *= 2;
 
    arr[0] = 5;
    arr[1] = 6;
 
    brute(0, 0, INF64);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python 3 program for the above approach.
 
N = 200 * 1000 + 13
 
n = 0
arr = [0 for i in range(N)]
brr = [0 for i in range(N)]
 
import sys
 
# Function to find the possible
# output array
def brute(ind, l, r):
   
    # Base case for the recursion
    if (ind == n / 2):
       
        # If ind becomes half of the size
        # then print the array.
        for i in range(n):
            print(brr[i],end = " ")
             
        # Exit the function.
        sys.exit()
 
    # Iterate in the range.
    for i in range(l,arr[ind] // 2 +1,1):
        if (arr[ind] - i <= r):
           
            # Put the values in the respective
            # indices.
            brr[ind] = i
            brr[n - ind - 1] = arr[ind] - i
 
            # Call the function to find values for
            # other indices.
            brute(ind + 1, i, arr[ind] - i)
 
# Driver Code
if __name__ == '__main__':
    n = 2
    n *= 2
 
    arr[0] = 5
    arr[1] = 6
    INF64 = 1000000000000000000
 
    brute(0, 0, INF64)
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
static int INF64 = (int)1e8;
static int N = 200 * 1000 + 13;
 
static int n;
static int[] arr = new int[N];
static int[] brr = new int[N];
 
// Function to find the possible
// output array
static void brute(int ind, int l, int r)
{
     
    // Base case for the recursion
    if (ind == n / 2)
    {
         
        // If ind becomes half of the size
        // then print the array.
        for(int i = 0; i < (int)n; i++)
            Console.Write(brr[i] + " ");
             
        // Exit the function. 
        System.Environment.Exit(0);
    }
 
    // Iterate in the range.
    for(int i = l; i <= arr[ind] / 2; ++i)
        if (arr[ind] - i <= r)
        {
             
            // Put the values in the respective
            // indices.
            brr[ind] = i;
            brr[n - ind - 1] = arr[ind] - i;
 
            // Call the function to find values for
            // other indices.
            brute(ind + 1, i, arr[ind] - i);
        }
}
 
// Driver Code
public static void Main()
{
    n = 2;
    n *= 2;
 
    arr[0] = 5;
    arr[1] = 6;
 
    brute(0, 0, INF64);
}
}
 
// This code is contributed by target_2.


Javascript


输出
0 1 5 5 

时间复杂度: O(N)
辅助空间: O(N)