给定大小为N – 1的数组Q [] ,使得每个Q [i] = P [i + 1] – P [i]其中P []是前N个自然数的预突变,任务是找到这个排列。如果找不到有效的排列P [],则打印-1 。
例子:
Input: Q[] = {-2, 1}
Output: 3 1 2
Input: Q[] = {1, 1, 1, 1}
Output: 1 2 3 4 5
方法:这是一个数学算法问题。令P [i] = x 。因此, P [i + 1] = P [i] +(P [i +1] – P [i])= x + Q [i] (因为Q [i] = P [i +1] – P [i ] )。
因此, P [i + 2] = P [i] +(P [i + 1] – P [i])+(P [i + 2] – P [i + 1])= x + Q [i] + Q [i + 1] 。观察一下,这里形成的图案。 P除了[x,x + Q [1],x + Q [1] + Q [2] +…+ x + Q [1] + Q [2] +…+ Q [n – 1]]之外什么都没有x = P [i]仍然未知。
让我们有一个排列P’ ,其中P'[i] = P [i] – x 。因此, P’= [0,Q [1],Q [1] + Q [2],Q [1] + Q [2] + Q [3],…,Q [1] + Q [2] + …+ Q [n – 1]] 。
为了找到x ,让我们找到P’中的最小元素。设为P'[k] 。因此, x = 1 – P'[k] 。这是因为原始置换P具有从1到n的整数,因此1可以是P中的最小元素。找到x之后,将x添加到每个P’中以获得原始排列P。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to find the required permutation
void findPerm(int Q[], int n)
{
int minval = 0, qsum = 0;
for (int i = 0; i < n - 1; i++) {
// Each element in P' is like a
// cumulative sum in Q
qsum += Q[i];
// minval is the minimum
// value in P'
if (qsum < minval)
minval = qsum;
}
vector P(n);
P[0] = 1 - minval;
// To check if each entry in P
// is from the range [1, n]
bool permFound = true;
for (int i = 0; i < n - 1; i++) {
P[i + 1] = P[i] + Q[i];
// Invalid permutation
if (P[i + 1] > n || P[i + 1] < 1) {
permFound = false;
break;
}
}
// If a valid permutation exists
if (permFound) {
// Print the permutation
for (int i = 0; i < n; i++) {
cout << P[i] << " ";
}
}
else {
// No valid permutation
cout << -1;
}
}
// Driver code
int main()
{
int Q[] = { -2, 1 };
int n = 1 + (sizeof(Q) / sizeof(int));
findPerm(Q, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to find the required permutation
static void findPerm(int Q[], int n)
{
int minval = 0, qsum = 0;
for (int i = 0; i < n - 1; i++)
{
// Each element in P' is like a
// cumulative sum in Q
qsum += Q[i];
// minval is the minimum
// value in P'
if (qsum < minval)
minval = qsum;
}
int []P = new int[n];
P[0] = 1 - minval;
// To check if each entry in P
// is from the range [1, n]
boolean permFound = true;
for (int i = 0; i < n - 1; i++)
{
P[i + 1] = P[i] + Q[i];
// Invalid permutation
if (P[i + 1] > n || P[i + 1] < 1)
{
permFound = false;
break;
}
}
// If a valid permutation exists
if (permFound)
{
// Print the permutation
for (int i = 0; i < n; i++)
{
System.out.print(P[i]+ " ");
}
}
else
{
// No valid permutation
System.out.print(-1);
}
}
// Driver code
public static void main(String[] args)
{
int Q[] = { -2, 1 };
int n = 1 + Q.length;
findPerm(Q, n);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to find the required permutation
def findPerm(Q, n) :
minval = 0; qsum = 0;
for i in range(n - 1) :
# Each element in P' is like a
# cumulative sum in Q
qsum += Q[i];
# minval is the minimum
# value in P'
if (qsum < minval) :
minval = qsum;
P = [0]*n;
P[0] = 1 - minval;
# To check if each entry in P
# is from the range [1, n]
permFound = True;
for i in range(n - 1) :
P[i + 1] = P[i] + Q[i];
# Invalid permutation
if (P[i + 1] > n or P[i + 1] < 1) :
permFound = False;
break;
# If a valid permutation exists
if (permFound) :
# Print the permutation
for i in range(n) :
print(P[i],end=" ");
else :
# No valid permutation
print(-1);
# Driver code
if __name__ == "__main__" :
Q = [ -2, 1 ];
n = 1 + len(Q) ;
findPerm(Q, n);
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the required permutation
static void findPerm(int []Q, int n)
{
int minval = 0, qsum = 0;
for (int i = 0; i < n - 1; i++)
{
// Each element in P' is like a
// cumulative sum in Q
qsum += Q[i];
// minval is the minimum
// value in P'
if (qsum < minval)
minval = qsum;
}
int []P = new int[n];
P[0] = 1 - minval;
// To check if each entry in P
// is from the range [1, n]
bool permFound = true;
for (int i = 0; i < n - 1; i++)
{
P[i + 1] = P[i] + Q[i];
// Invalid permutation
if (P[i + 1] > n || P[i + 1] < 1)
{
permFound = false;
break;
}
}
// If a valid permutation exists
if (permFound)
{
// Print the permutation
for (int i = 0; i < n; i++)
{
Console.Write(P[i]+ " ");
}
}
else
{
// No valid permutation
Console.Write(-1);
}
}
// Driver code
public static void Main(String[] args)
{
int []Q = { -2, 1 };
int n = 1 + Q.Length;
findPerm(Q, n);
}
}
// This code is contributed by PrinciRaj1992
3 1 2