Sort by

recency

|

351 Discussions

|

  • + 0 comments

    Here is my c++ solution

    string larrysArray(vector<int> A) {
        if(A.size() == 2 && A[0] > A[1]) return "NO";
        else if(A.size() == 1 || A.size() == 0) return "YES";
        else {
            auto min_it = std::min_element(A.begin(), A.end());
            int tempIndex = distance(A.begin(), min_it);
            A.erase(A.begin() +  tempIndex);
            if(tempIndex % 2 != 0){
                swap(A[0], A[1]);
            }
            return larrysArray(A);
        }
    }
    
  • + 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";
    }