Tuesday, 30 August 2016

Solution of URI 2162 :: Peaks and Valleys


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>

int main()
{
    int n, i, cndtn = 1;
    scanf("%d", &n);
    int ara[n];
    for(i=0; i<n; i++)
        scanf("%d", &ara[i]);
    if(n==2 && ara[0]==ara[1]) cndtn = 0;
    else
    {
        for(i=2; i<n; i++)
        {
            if(ara[i] >= ara[i-1] && ara[i-1] >= ara[i-2])
            {
                cndtn = 0;
                break;
            }
            else if(ara[i] <= ara[i-1] && ara[i-1] <= ara[i-2])
            {
                cndtn = 0;
                break;
            }
        }
    }
    printf((cndtn==1)?"1\n":"0\n");
    return 0;
}

1 comment:

  1. #include

    int main() {
    int n, m, c, a, r=0;
    scanf("%d%d%d", &n, &a, &m);
    n -= 2;
    if ( a != m ) {
    r = 1;
    c = (a > m) ? 0 : 1;
    a = m;
    while( n-- > 0 ) {
    scanf("%d", &m);
    if ( (c%2==0 && m <= a) || (c%2==1 && m >= a) ) {
    r = 0;
    break;
    }
    a = m;
    c++;
    }
    }
    printf("%d\n", r);
    return 0;
    }

    ReplyDelete

Note: only a member of this blog may post a comment.