Biased Chandan

QUESTION

Chandan is an extremely biased person, and he dislikes people who fail to solve all the problems in the interview he takes for hiring people. There are n people on a day who came to be interviewed by Chandan.\n\nChandan rates every candidate from 0 to 10. He has to output the total ratings of all the people who came in a day. But, here’s the problem: Chandan gets extremely frustrated when someone ends up scoring a 0 in the interview. So in frustration he ends up removing the candidate who scored that 0, and also removes the candidate who came before him. If there is no candidate before the one who scores a 0, he does nothing.\n\nYou’ve to find the summation of all the ratings in a day for Chandan.\n\nInput constraints:\nThe first line of input will contain an integer n. The next n lines will contain an integer, where the ith integer represents the rating of the ith person.\n\nOutput constraints:\nPrint the required sum.\n\nConstraints:\n1 n 5 * 10^3\n0 Value of ratings 10.

“TESTCASE_1”: “5\n2\n3\n0\n7\n0\n###—###SEPERATOR—###—\n2”, “TESTCASE_2”: “5\n2\n3\n6\n7\n0\n###—###SEPERATOR—###—\n11”, “TESTCASE_3”: “10\n2\n3\n6\n7\n0\n0\n0\n8\n0\n3\n###—###SEPERATOR—###—\n5”, “TESTCASE_4”: “4\n0\n0\n8\n3\n###—###SEPERATOR—###—\n11”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<stdio.h>
#include<stdlib.h>
 
int main()
{
    int n;
    scanf("%d",&n);
    int arr[n];
    int i;
    int j=0;
    for(i=0;i<n;i++)
    {
        int q;
        scanf("%d",&q);
        if(q==0)
        {
            if(j==0)
                j=0;
            else
            {
                j--;
                arr[j]=q;
 
            }
        }
        else
        {
            arr[j]=q;
            j++;
        }
    }
    int sum=0;
    for(i=0;i<j;i++)
    {
        sum+=arr[i];
    }
    printf("%d\n",sum);
    return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro