Codeforce 486A – Calculating Function

Hi guys sorry for waiting too long, I just started to working at a startup in Jakarta. So this afternoon, at the office LOL, i just solved one question on codeforce.

It took me several minutes to find the pattern of the question, the pattern are :

1) By using even and odd number :
a) if the number is an even number then we should calculate by using (n/2).
b) then if we found out that the number is an odd one, just calculate it by using ((n+2)+1) *-1

Go and give it a try! the above pattern gave me a solid AC on my second try HAHA*

*beware of the questions that you should use high range variable, I tried using int at first and it gave me an error, then I changed it to long long int so that It can holds the result value.

Ok.. That’s for me now, See you on another post!


#include <iostream>

using namespace std;

int main(void)
{
    long long int n;
    cin >> n;
    long long int res = 0;

    if (n == 1){
        res = -1;
        cout << res << endl;
    }else if (n % 2 == 0){
        res = (n/2);
        cout << res << endl;
    }else{
        res = ((n/2) + 1) * -1;
        cout << res << endl;
    }
}

New posts await!

Hi guys would like to announce that I’ll posts some stuff regarding competitive programming. It’s challenging but it’s fun!

Here’s a screenshot that my directory looks like LOL. But its not a complete list actually, I’ve solved a lot questions on others sites like Codechef and Hackerrank too, but Idk where’s the files now.

Codeforce 580 A – Kefa and First Steps C++

Hi guys this is my first blog post about my passion in competitive programming, as to remind you I am not a native english speaker so I might use bad grammar here.

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int a[n];

    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
    }

    int high = 1;
    int highest = 1;

    for(int j = 1 ; j < n ; j++){
        if (a[j] >= a[j-1]){
            high++;
            if (high > highest) {
                highest = high;
            }
        }else high = 1;
    }

    cout << highest;
}

Here’s my solution on CF 580A – Kefa and First Steps, the main idea is to add a value into a variable highest if its value higher than the previous value in array, if it’s not then return the highest value to 1.

The time complexity of the above code is O(n) and the space complexity is O(n). I’ll find the shortest and more efficient code later.

Runtime : 187ms

Memory 400KB

And also if you have any suggestion about how can I improve the code, feel free to comment below this post! Thanks guys!

Design a site like this with WordPress.com
Get started