We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My solution in C passed the initial test but gave a compiler message "Abort" on the other tests.
Can someone please help me find the error in my code?
I searched other discussions and any comments posted for C solutions applied to cases where the inputs were captured in main() as opposed to being sent as arguments to the getMax() function.
#include<assert.h>#include<ctype.h>#include<limits.h>#include<math.h>#include<stdbool.h>#include<stddef.h>#include<stdint.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX_SIZE 100000typedefstruct{int*arr;intsize;inttop;}Stack;char*readline();char*ltrim(char*);char*rtrim(char*);intparse_int(char*);intisEmpty(Stack*s);intisFull(Stack*s);voidpush(Stack*s,intdata);intpop(Stack*s);intmaximum(Stack*s);intisEmpty(Stack*s){if(s->top==-1)return1;return0;}intisFull(Stack*s){if(s->top==s->size-1)return1;return0;}voidpush(Stack*s,intdata){if(!isFull(s)){s->top++;s->arr[s->top]=data;}else{printf("Error: stack overflow\n");}}intpop(Stack*s){intans=-1;if(isEmpty(s)){printf("Error: No element to pop\n");}else{ans=s->arr[s->top];s->top--;}returnans;}intmaximum(Stack*s){intmax=s->arr[0];for(inti=0;i<=s->top;i++){if(max<s->arr[i])max=s->arr[i];}returnmax;}/* * Complete the 'getMax' function below. * * The function is expected to return an INTEGER_ARRAY. * The function accepts STRING_ARRAY operations as parameter. *//* * To return the integer array from the function, you should: * - Store the size of the array to be returned in the result_count variable * - Allocate the array statically or dynamically * * For example, * int* return_integer_array_using_static_allocation(int* result_count) { * *result_count = 5; * * static int a[5] = {1, 2, 3, 4, 5}; * * return a; * } * * int* return_integer_array_using_dynamic_allocation(int* result_count) { * *result_count = 5; * * int *a = malloc(5 * sizeof(int)); * * for (int i = 0; i < 5; i++) { * *(a + i) = i + 1; * } * * return a; * } * */int*getMax(intoperations_count,char**operations,int*result_count){Stack*s=malloc(sizeof(Stack));s->size=operations_count+1;s->top=-1;s->arr=malloc(s->size*sizeof(int));int*a=malloc(*result_count*sizeof(int));char*input;intcnt=0;for(inti=0;i<operations_count;i++){input=operations[i];intn=strlen(input);if(n>1){charqt[2];strncpy(qt,input,1);qt[1]='\0';intquery_type=atoi(qt);if(query_type==1){char*ptr;charelement[10];intj=0;ptr=input;ptr++;while(*ptr!='\0'){if(strcmp(ptr," ")==0)ptr++;else{element[j++]=*ptr;ptr++;}}element[j]='\0';intvalue=atoi(element);push(s,value);}}else{charqt[2];strncpy(qt,input,1);qt[1]='\0';intquery_type=atoi(qt);if(query_type==2){pop(s);}elseif(query_type==3){a[cnt]=maximum(s);cnt++;}}}*result_count=cnt;returna;}intmain(){FILE*fptr=fopen(getenv("OUTPUT_PATH"),"w");intn=parse_int(ltrim(rtrim(readline())));char**ops=malloc(n*sizeof(char*));for(inti=0;i<n;i++){char*ops_item=readline();*(ops+i)=ops_item;}intres_count;int*res=getMax(n,ops,&res_count);for(inti=0;i<res_count;i++){fprintf(fptr,"%d",*(res+i));if(i!=res_count-1){fprintf(fptr,"\n");}}fprintf(fptr,"\n");fclose(fptr);return0;}char*readline(){size_talloc_length=1024;size_tdata_length=0;char*data=malloc(alloc_length);while(true){char*cursor=data+data_length;char*line=fgets(cursor,alloc_length-data_length,stdin);if(!line){break;}data_length+=strlen(cursor);if(data_length<alloc_length-1||data[data_length-1]=='\n'){break;}alloc_length<<=1;data=realloc(data,alloc_length);if(!data){data='\0';break;}}if(data[data_length-1]=='\n'){data[data_length-1]='\0';data=realloc(data,data_length);if(!data){data='\0';}}else{data=realloc(data,data_length+1);if(!data){data='\0';}else{data[data_length]='\0';}}returndata;}char*ltrim(char*str){if(!str){return'\0';}if(!*str){returnstr;}while(*str!='\0'&&isspace(*str)){str++;}returnstr;}char*rtrim(char*str){if(!str){return'\0';}if(!*str){returnstr;}char*end=str+strlen(str)-1;while(end>=str&&isspace(*end)){end--;}*(end+1)='\0';returnstr;}intparse_int(char*str){char*endptr;intvalue=strtol(str,&endptr,10);if(endptr==str||*endptr!='\0'){exit(EXIT_FAILURE);}returnvalue;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Element
You are viewing a single comment's thread. Return to all comments →
My solution in C passed the initial test but gave a compiler message "Abort" on the other tests.
Can someone please help me find the error in my code?
I searched other discussions and any comments posted for C solutions applied to cases where the inputs were captured in main() as opposed to being sent as arguments to the getMax() function.