Double inversions
Practice
2.6 (31 votes)
Arrays
Data structures
1 D
Math
Problem
77% Success 4472 Attempts 20 Points 3s Time Limit 256MB Memory 1024 KB Max Code

Consider a certain permutation of integers from \(1\) to \(n\) as \(​​​​A = \{a_1, a_2,..., a_n\}\) and reverse of array \(A\) as \(R\), that is, \(R = \{a_n, a_{n -1},...,a_1\}\). You are given the inversions at each position of array \(A\) and \(R\) as \(\{IA_1, IA_2,….., IA_n\}\) and \(\{IR_1, IR_2,….., IR_n\}\) respectively.

Find the original array \(A\). If, there are multiple solutions, print any of them. If there is no solution, then print -1.

Note: The inversion of array \(arr\) for position \(i\) is defined as the count of positions \(j\) satisfying the following condition \(arr_j > arr_i\) and \(1 \leq j < i\).

Input format

  • The first line contains \(T\) denoting the number of test cases.
  • For each test case:
    • The first line contains \(n\) denoting the number of elements.
    • The second line contains the elements \(\{IA_1, IA_2,….., IA_n\}\).
    • The third line contains the elements \(\{IR_1, IR_2,….., IR_n\}\).

Output format

For each test case, print the array \(A\) in the space-separated format or -1 if no solution exists. Each test case should be answered in a new line. 

Constraints 

\(1 \leq T \leq 10 \\ 1 \leq n \leq 10^5 \\ 0 \leq IA_i, IR_i <n \space \forall \space i \in [1, n]\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
73 votes
Tags:
ArraysData Structures1-D
Points:20
83 votes
Tags:
SortingArraysImplementationData StructuresSet1-D
Points:20
131 votes
Tags:
Ad-HocBasic ProgrammingEasy