Sort by

recency

|

351 Discussions

|

  • + 0 comments

    string larrysArray(vector A) { int count = 0; int n = A.size(); for(int i=0; iA[j]){ count++; } } }

    if(count%2 == 0){ return "YES"; } return "NO"; }

  • + 0 comments

    c++ code string larrysArray(vector A) { int count = 0; int n = A.size(); for(int i=0; iA[j]){ count++; } } }

    if(count%2 == 0){ return "YES"; } return "NO"; }

  • + 0 comments

    //code develop by POLY in C

    #include<stdio.h>
    int main() {
        int t;
        scanf("%d",&t);
        while (t--)
        {
            int n;
            int arr[1000];
            scanf("%d",&n);
            for (int i=0;i<n;i++)
                scanf("%d",&arr[i]);
            int inv = 0;
            for (int i=0;i<n;i++)
                for (int j=i+1;j<n;j++)
                    if (arr[i] > arr[j])
                        inv ++;
            if (inv%2==0)
                printf("YES\n");
            else
                printf("NO\n");
        }
        return 0;
    }
    
  • + 0 comments

    Solution O(N^3) in C++ with sorting, cause I think it's the purpose of these challenges.

    int rotate(vector<int>& A, int pos) {
        int validPos = std::min(std::max(1, pos), (int)A.size() - 2);
        int tmp = A[validPos - 1];
        A[validPos - 1] = A[validPos];
        A[validPos] = A[validPos + 1];
        A[validPos + 1] = tmp;
        return pos - 1;
    }
    
     void move(vector<int>& A, int currPos, int dstPos) {
         while (currPos != dstPos) {
             currPos = rotate(A, currPos);
         }
     }
    
    string larrysArray(vector<int> A) {
        int base = 1;
        int N = A.size();
        for (int i = 0; i < N - 2; i++) {
            for (int j = i; j < N; j++) {
                if (A[j] == base) {
                    move(A, j, i);
                    break;
                }
            }
            base++;
        }
        if (A[N - 3] <= A[N - 2] && A[N - 2] <= A[N - 1]) {
            return "YES";
        }
        return "NO";
    }
    
  • + 1 comment
    def larrysArray(A):
        count = 0
        
        for i in range(len(A)):
            for j in range(i+1, len(A)):
                if A[i] > A[j]:
                    count += 1
        
        return 'YES' if count % 2 == 0 else 'NO'