Permutation Swaps
Practice
3.6 (14 votes)
Linear search
Algorithms
Greedy algorithm
Problem
36% Success 4573 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

A permutation of length \(N\) is an array of \(N\) integers such that every integer from \(1\) to \(N\) appears exactly once. For example, \([2, 3, 5, 4, 1] \) is a permutation of length \(5\), while \([1,2, 2], [4, 5, 1, 3] \) are not permutations.

You are given an array \(A\) having \(N\) integers. You can apply the following operation on the array \(A\) any number of times:

  • Choose two indices \(i, j\) such that \(1 \le i \lt j \le N\), then decrease \(1\) from \(A_i\) and add \(1\) to \(A_j\).

Find if it is possible to convert the array \(A\) into a permutation of length \(N\).

Input format

  • The first line of input contains an integer \(T\) denoting the number of test cases. The description of each test case is as follows:
  • The first line of each test case contains an integer \(N\) denoting the length of the array \(A\).
  • The second line of each test case contains \(N\) integers \(A_1, A_2,\dots, A_N\), denoting the elements of the array \(A\).

Output format

For each test case, print YES if it is possible to obtain a permutation from the array \(A\) using the given operation, otherwise print NO.

Constraints

\(1\le T \le 10^5 \\ 1 \leq N \le 3\cdot 10^5\\ 1\le A_i \le 10^9\\ \text{Sum of $N$ over all test cases does not exceed }3\cdot 10^5.\)

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
10 votes
Tags:
Linear SearchAlgorithmsGreedy algorithm
Points:20
176 votes
Tags:
Ad-HocApprovedBasic ProgrammingEasyOpen
Points:30
718 votes
Tags:
ReadyHiringEasy-MediumApprovedSegment tree