/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 0ms 320.0 KiB
#2 Accepted 1ms 324.0 KiB

Code

#include <stdio.h>

int is_permutation(int A[], int N) {
    int count[N + 1]; // count array to store frequency of each number
    for (int i = 1; i <= N; i++) {
        count[i] = 0; // initialize count array
    }

    for (int i = 0; i < N; i++) {
        if (A[i] < 1 || A[i] > N) {
            return 0; // if any number is out of range, it's not a permutation
        }
        count[A[i]]++; // increment count for each number
    }

    for (int i = 1; i <= N; i++) {
        if (count[i]!= 1) {
            return 0; // if any number appears more than once, it's not a permutation
        }
    }

    return 1; // if all conditions are met, it's a permutation
}

int main() {
    int T;
    scanf("%d", &T);

    for (int i = 0; i < T; i++) {
        int N;
        scanf("%d", &N);
        int A[N];
        for (int j = 0; j < N; j++) {
            scanf("%d", &A[j]);
        }

        if (is_permutation(A, N)) {
            printf("YES\n");
        } else {
            printf("NO\n");
        }
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1055 Array Permutation
Contest
Brain Booster #3
Language
C++17 (G++ 13.2.0)
Submit At
2024-05-06 15:50:15
Judged At
2024-11-11 03:33:50
Judged By
Score
100
Total Time
1ms
Peak Memory
324.0 KiB