greedy

Please click on ads to support us..

Python Code:

def check(n,A):
    A=sorted(A)
    M=[x%2 for x in A]
    ci=0
    cp=0
    c=0
    imp=0
    par=0
    for i in range(n):
        if M[i]:
            c=c+1
            if ci==0:
                imp=A[i]
                ci=1
        else:
            if cp==0:
                par=A[i]
                cp=1
    if c==0 or c==n:
         print("YES")
    else:
        if imp<par:
            print("YES")
        else:
            print("NO")
t=int(input())
for _ in range(t):
    n=int(input())
    A=[int(x) for x in input().split()]
    check(n,A)

C++ Code:

/* header file */
#include<bits/stdc++.h>
#include<stack>
#include <queue>
#include <algorithm>
#include<iostream>
#include<string>
/* for faster cin cout */
#define fast() ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
/* sort function */
#define strsort() sort(s.begin(),s.end())
#define arrsort() int n1=sizeof(a)/sizeof(a[0]);sort(a,a+n1)
/* legth function */
#define strlen s.size()
#define arrlen int n1=sizeof(a)/sizeof(a[0])
#define intlen int n2=trunc(log10(n))+1
/* Conver function */
#define csti int csti=stoi(s)
/* Others Sortcut */
#define PI 3.1415926535897932384626433832795
#define nn "\n"
#define yes "YES"
#define no "NO"
#define case "Case "
#define elif else if
#define ll long long
/* testcase */
#define test() int t;cin>>t;while(t--)
#define testt() int t;cin>>t;for(int i=1; i<=t; i++)
using namespace std;
const long long MOD = 1e9 + 7;
/*  let's started  */
void solved()
{
    test()
    {
        int n;
        cin>>n;
        ll a[n];
        for(int i=0; i<n; i++)
            cin>>a[i];

        arrsort();
        int c=0,c1=0;
        for(int i=0; i<n; i++)
        {
            if(a[0]%2==1)
            {
                c=1;
                break;
            }
            else
            {
                if(a[i]%2==1)
                {
                    c1++;
                }
            }
        }
        if(c1==0||c==1) cout<<yes<<nn;
        else cout<<no<<nn;
    }
}

int main()
{
    fast();
    solved();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil