Competitive Programming Solutions

GitHub
back to dashboard

E. Alternating Array

Codeforces - Contest #3.1 · limit: 1s · memory: 256MB · view on codeforces

input

3
3 4 2

output

1
Main.java
static void solve() {

        // Write your solution here
        int n = in.nextInt();
        int[] a = readIntArray(n);
        int fNeg = 0;
        int sNeg = 0;
        for(int i = 0; i < n; i++){
                if(i % 2 == 0){
                    if(a[i] > 0)
                        fNeg++;
                    else
                        sNeg++;
                }
                else{
                    if(a[i] > 0)
                        sNeg++;
                    else
                        fNeg++;
                }
        }
        out.print(Math.min(fNeg,sNeg));
    }