📌  相关文章
📜  从相邻元素之间的差异数组生成前 N 个自然数的排列

📅  最后修改于: 2021-10-27 08:50:26             🧑  作者: Mango

给定一个由(N – 1)组成的数组arr[] ,任务是构造一个由前N 个自然数组成的置换数组P[]使得arr[i] = (P[i +1] – P[i ]) 。如果不存在这样的排列,则打印“-1”

例子:

方法:给定的问题可以通过将排列的第一个元素视为0 ,然后使用给定的数组arr[]构造一个新的排列数组来解决在此之后,将新数组的最小元素添加到每个元素,使数组元素在[1, N]范围内。请按照以下步骤解决问题:

  • 初始化一个数组,比如大小为N 的perm[]来存储结果排列。
  • perm[0]初始化为0 ,并初始化一个变量,比如lastEle0
  • 使用变量i在范围[1, N] 上迭代并将arr[i – 1]的值添加到元素lastEle并将perm[i]的值更新为lastEle
  • 初始化一个变量,比如minimumElement到数组perm[]的最小元素。
  • 初始化整数st的 HashSet ,以存储排列的所有元素。此外,将变量mx初始化为0以存储perm[]数组中的最大元素。
  • 遍历perm[]数组并将(-sm) + 1的值添加到值perm[i] ,将mx的值更新为max(mx, perm[i])并将perm[i]添加到st
  • 完成上述步骤后,如果mx的值和 HashSet st的大小为N ,则打印数组perm[]作为结果数组。否则,打印-1

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the permutation of
// N integers from the given difference
// array A[]
void findPermutation(int A[], int N)
{
    int lasEle = 0;
 
    // Stores the resultant permutation
    int perm[N];
    perm[0] = 0;
 
    for (int i = 1; i < N; i++) {
 
        // Update the value of lastEle
        lasEle += A[i - 1];
 
        // Initialize the value of
        // perm[i]
        perm[i] = lasEle;
    }
 
    // Stores the minimum element of
    // the array perm[]
    int sm = *min_element(perm, perm + N);
 
    // Stores the elements of the
    // permutation array perm[]
    unordered_set st;
    int mx = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update the value of perm[i]
        perm[i] += (-sm) + 1;
 
        // Update the value of mx
        mx = max(mx, perm[i]);
 
        // Insert the current element
        // in the hashset
        st.insert(perm[i]);
    }
 
    // Check if the maximum element and
    // the size of hashset is N or not
    if (mx == N and st.size() == N) {
 
        // Print the permutation
        for (int i = 0; i < N; i++) {
            cout << perm[i] << " ";
        }
    }
 
    // Otherwise print -1
    else {
        cout << -1 << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { -1, 2, -3, -1 };
    int N = sizeof(arr) / sizeof(arr[0]);
    findPermutation(arr, N + 1);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the permutation of
// N integers from the given difference
// array A[]
static void findPermutation(int []A, int N)
{
    int lasEle = 0;
 
    // Stores the resultant permutation
    int []perm = new int[N];
    perm[0] = 0;
 
    for (int i = 1; i < N; i++) {
 
        // Update the value of lastEle
        lasEle += A[i - 1];
 
        // Initialize the value of
        // perm[i]
        perm[i] = lasEle;
    }
 
    // Stores the minimum element of
    // the array perm[]
     int sm = perm[0]; 
        //Loop through the array 
        for (int i = 0; i < perm.length; i++) { 
            //Compare elements of array with min 
           if(perm[i]  st = new HashSet();
    int mx = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update the value of perm[i]
        perm[i] += (-sm) + 1;
 
        // Update the value of mx
        mx = Math.max(mx, perm[i]);
 
        // Insert the current element
        // in the hashset
        st.add(perm[i]);
    }
 
    // Check if the maximum element and
    // the size of hashset is N or not
    if (mx == N && st.size() == N) {
 
        // Print the permutation
        for (int i = 0; i < N; i++) {
            System.out.print(perm[i]+" ");
        }
    }
 
    // Otherwise print -1
    else {
        System.out.print(-1);
    }
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { -1, 2, -3, -1 };
    int N = arr.length;
    findPermutation(arr, N + 1);
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Python3
# Python program for the above approach
 
# Function to find the permutation of
# N integers from the given difference
# array A[]
def findPermutation(A, N):
    lasEle = 0
 
    # Stores the resultant permutation
    perm = [0]*N
    perm[0] = 0
 
    for i in range(1,N):
        # Update the value of lastEle
        lasEle += A[i - 1]
 
        # Initialize the value of
        # perm[i]
        perm[i] = lasEle
 
    # Stores the minimum element of
    # the array perm[]
    sm = min(perm)
 
    # Stores the elements of the
    # permutation array perm[]
    st = {}
    mx = 0
 
    # Traverse the array
    for i in range(N):
        # Update the value of perm[i]
        perm[i] += (-sm) + 1
 
        # Update the value of mx
        mx = max(mx, perm[i])
 
        # Insert the current element
        # in the hashset
        st[perm[i]] = 1
 
    # Check if the maximum element and
    # the size of hashset is N or not
    if (mx == N and len(st) == N):
 
        # Prthe permutation
        for i in range(N):
            print(perm[i],end=" ")
    # Otherwise pr-1
    else:
        print(-1,end=" ")
 
# Driver Code
if __name__ == '__main__':
    arr = [-1, 2, -3, -1]
    N = len(arr)
    findPermutation(arr, N + 1)
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Linq;
using System.Collections.Generic;
 
class GFG {
 
// Function to find the permutation of
// N integers from the given difference
// array A[]
static void findPermutation(int[] A, int N)
{
    int lasEle = 0;
 
    // Stores the resultant permutation
    int[] perm = new int[N];
    perm[0] = 0;
 
    for (int i = 1; i < N; i++) {
 
        // Update the value of lastEle
        lasEle += A[i - 1];
 
        // Initialize the value of
        // perm[i]
        perm[i] = lasEle;
    }
 
    // Stores the minimum element of
    // the array perm[]
    int sm = perm.Min();
 
    // Stores the elements of the
    // permutation array perm[]
   List st = new List();
    int mx = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update the value of perm[i]
        perm[i] += (-sm) + 1;
 
        // Update the value of mx
        mx = Math.Max(mx, perm[i]);
 
        // Insert the current element
        // in the hashset
        st.Add(perm[i]);
    }
 
    // Check if the maximum element and
    // the size of hashset is N or not
    if (mx == N && st.Count == N) {
 
        // Print the permutation
        for (int i = 0; i < N; i++) {
           Console.Write(perm[i] + " ");
        }
    }
 
    // Otherwise print -1
    else {
        Console.Write(-1 + " ");
    }
}
 
    // Driver Code
    static void Main()
    {
        int[] arr= { -1, 2, -3, -1 };
    int N = arr.Length;
    findPermutation(arr, N + 1);
    }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
4 3 5 2 1

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程