📌  相关文章
📜  根据每两个连续元素之间的差异生成原始数组

📅  最后修改于: 2021-05-08 17:48:08             🧑  作者: Mango

给定N – 1 ,包含两个N的数组的两个连续元素之间的差在1到N的范围内。任务是使用给定的差异确定原始数组。如果可能,打印数组,否则打印-1
例子:

方法:由于我们要生成范围为[1,n]的排列,并且每个数字只能出现一次。举个例子

每个元素之间的连续差进行排序之后,我们将对p [i]进行排序,如果它们之间的差必须等于1。如果在任何索引处,我们都遇到一个元素p [i] ,使得p [i] – p [i – 1] != 1 ,则无法生成排列。为了生成最终的排列,我们将跟踪索引,我们可以使用map或unordered_map做到这一点。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the required permutation
void findPerm(int n, vector& differences)
{
    vector ans;
    ans.clear();
    ans.push_back(0);
  
    // Take x = 0 for simplicity
    int x = 0;
  
    // Calculate aint the differences
    // and store it in a vector
    for (int i = 0; i <= n - 2; ++i) {
        int diff = differences[i];
        x = x + diff;
        ans.push_back(x);
    }
  
    // Preserve the original array
    vector anss = ans;
    sort(ans.begin(), ans.end());
    int flag = -1;
  
    // Check if aint the consecutive elements
    // have difference = 1
    for (int i = 1; i <= n - 1; ++i) {
        int res = ans[i] - ans[i - 1];
        if (res != 1) {
            flag = 0;
        }
    }
  
    // If consecutive elements don't have
    // difference 1 at any position then
    // the answer is impossible
    if (flag == 0) {
        cout << -1;
        return;
    }
  
    // Else store the indices and values
    // at those indices in a map
    // and finainty print them
    else {
        unordered_map mpp;
        mpp.clear();
        int j = 1;
        vector value_at_index;
        for (auto& x : ans) {
            mpp[x] = j;
            ++j;
        }
        for (auto& x : anss) {
            value_at_index.push_back(mpp[x]);
        }
        for (auto& x : value_at_index) {
            cout << x << " ";
        }
        cout << endl;
    }
}
  
// Driver code
int main()
{
    vector differences;
    differences.push_back(2);
    differences.push_back(-3);
    differences.push_back(2);
    int n = differences.size() + 1;
    findPerm(n, differences);
  
    return 0;
}


Java
// Java program to implement the above approach
import java.util.*;
class GFG 
{
  
// Function to print the required permutation
static void findPerm(int n, Vector differences)
{
    Vector ans = new Vector();
    ans.clear();
    ans.add(0);
  
    // Take x = 0 for simplicity
    int x = 0;
  
    // Calculate aint the differences
    // and store it in a vector
    for (int i = 0; i <= n - 2; ++i) 
    {
        int diff = differences.get(i);
        x = x + diff;
        ans.add(x);
    }
  
    // Preserve the original array
    Vector anss = new Vector();
    for(Integer obj:ans)
        anss.add(obj);
          
    Collections.sort(ans);
    int flag = -1;
  
    // Check if aint the consecutive elements
    // have difference = 1
    for (int i = 1; i <= n - 1; ++i)
    {
        int res = ans.get(i) - ans.get(i - 1);
        if (res != 1) 
        {
            flag = 0;
        }
    }
  
    // If consecutive elements don't have
    // difference 1 at any position then
    // the answer is impossible
    if (flag == 0) 
    {
        System.out.print(-1);
        return;
    }
  
    // Else store the indices and values
    // at those indices in a map
    // and finainty print them
    else 
    {
        Map mpp = new HashMap<>();
        mpp.clear();
        int j = 1;
        Vector value_at_index = new Vector();
        for (Integer x1 : ans) 
        {
            mpp.put(x1, j);
            ++j;
        }
        for (Integer x2 : anss) 
        {
            value_at_index.add(mpp.get(x2));
        }
        for (Integer x3 : value_at_index)
        {
            System.out.print(x3 + " ");
        }
        System.out.println();
    }
}
  
// Driver code
public static void main(String[] args)
{
    Vector differences = new Vector();
    differences.add(2);
    differences.add(-3);
    differences.add(2);
    int n = differences.size() + 1;
    findPerm(n, differences);
    }
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function to print the required permutation
def findPerm(n, differences):
    ans = []
    ans.append(0)
  
    # Take x = 0 for simplicity
    x = 0
  
    # Calculate athe differences
    # and store it in a vector
    for i in range(n - 1):
        diff = differences[i]
        x = x + diff
        ans.append(x)
  
    # Preserve the original array
    anss = ans
    ans = sorted(ans)
    flag = -1
  
    # Check if athe consecutive elements
    # have difference = 1
    for i in range(1, n):
        res = ans[i] - ans[i - 1]
        if (res != 1):
            flag = 0
  
    # If consecutive elements don't have
    # difference 1 at any position then
    # the answer is impossible
    if (flag == 0):
        print("-1")
        return
  
    # Else store the indices and values
    # at those indices in a map
    # and finainty print them
    else:
        mpp = dict()
        j = 1
        value_at_index = []
        for x in ans:
            mpp[x] = j
            j += 1
  
        for x in anss:
            value_at_index.append(mpp[x])
  
        for x in value_at_index:
            print(x, end = " ")
        print()
  
# Driver code
differences=[]
differences.append(2)
differences.append(-3)
differences.append(2)
n = len(differences) + 1
findPerm(n, differences)
  
# This code is contributed by mohit kumar


C#
// C# program to implement the above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to print the required permutation
static void findPerm(int n, List differences)
{
    List ans = new List();
    ans.Clear();
    ans.Add(0);
  
    // Take x = 0 for simplicity
    int x = 0;
  
    // Calculate aint the differences
    // and store it in a vector
    for(int i = 0; i <= n - 2; ++i) 
    {
        int diff = differences[i];
        x = x + diff;
        ans.Add(x);
    }
  
    // Preserve the original array
    List anss = new List();
    foreach(int obj in ans)
        anss.Add(obj);
          
    ans.Sort();
    int flag = -1;
  
    // Check if aint the consecutive elements
    // have difference = 1
    for(int i = 1; i <= n - 1; ++i)
    {
        int res = ans[i] - ans[i - 1];
        if (res != 1) 
        {
            flag = 0;
        }
    }
  
    // If consecutive elements don't have
    // difference 1 at any position then
    // the answer is impossible
    if (flag == 0) 
    {
        Console.Write(-1);
        return;
    }
  
    // Else store the indices and values
    // at those indices in a map
    // and finainty print them
    else
    {
        Dictionary mpp = new Dictionary();
        mpp.Clear();
        int j = 1;
          
        List value_at_index = new List();
        foreach (int x1 in ans) 
        {
            mpp.Add(x1, j);
            ++j;
        }
          
        foreach (int x2 in anss) 
        {
            value_at_index.Add(mpp[x2]);
        }
          
        foreach (int x3 in value_at_index)
        {
            Console.Write(x3 + " ");
        }
        Console.WriteLine();
    }
}
  
// Driver code
public static void Main(String[] args)
{
    List differences = new List();
    differences.Add(2);
    differences.Add(-3);
    differences.Add(2);
      
    int n = differences.Count + 1;
      
    findPerm(n, differences);
}
}
  
// This code is contributed by Amit Katiyar


输出:
2 4 1 3