AtCode A - To Be Saikyo

 Problem Description Link: https://atcoder.jp/contests/abc313/tasks/abc313_a


Accepted Solution

#include<bits/stdc++.h>
using namespace std;

int sol(int N) {
    int i, a, mx = -1, first=0, cnt = 0;
    cin >> first;
    mx = max(mx, first);
    for(i=1; i<N; i++) {
        cin >> a;
        if(a == first) {
            cnt += 1;
        }
        mx = max(mx, a);
    }
   
    int x = mx - first;

    if(cnt == 0 && mx != first) {
        return x + 1;
    }
    else if(cnt == 0 && mx == first) {
        return 0;
    }
    else if(cnt >= 1 && cnt != (N-1)) {
        return x + 1;
    }
    else {
        return x + 1;
    }
}

int main() {
    int N;
    cin >> N;
    int ans = sol(N);
    cout << ans << "\n";
    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number