-7

Apple and Orange problem.

Only 3 of 12 test cases are cleared. Can't think of anything else since hours.

sample input 0

7 11
5 15
3 2
-2 2 1
5 -6

sample output 0

1
1

Problem: https://www.hackerrank.com/challenges/apple-and-orange/problem

Code

int main(){

    int s; 
    int t; 
    scanf("%d %d",&s,&t);

    int a; 
    int b; 
    scanf("%d %d",&a,&b);

    int m; 
    int n; 
    scanf("%d %d",&m,&n);

    int *apple = malloc(sizeof(int) * m);
    for(int apple_i = 0; apple_i < m; apple_i++){
       scanf("%d",&apple[apple_i]);
    }


    int *orange = malloc(sizeof(int) * n);
    for(int orange_i = 0; orange_i < n; orange_i++){
       scanf("%d",&orange[orange_i]);
    }

    int fellap=0;
    int fellor=0;
    int d;

    for(int apple_i = 0; apple_i < m; apple_i++){
        d=apple[apple_i]+a;
        f(d>=s && d<=t){
            fellap++;
        }
    }

    for(int orange_i = 0; orange_i < n; orange_i ++){
        d=orange[orange_i]+b; 
        if(d>=s&&d<=t){
            fellor++;
        }
    }

    printf("%d\n", fellor);
    printf("%d\n", fellap);

    return 0;
}
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
SorainOne
  • 13
  • 5

1 Answers1

0

The bug in your code is a very trivial one. You have switched the output of oranges and apples. Change

printf("%d\n", fellor);
printf("%d\n", fellap);

to

printf("%d\n", fellap);
printf("%d\n", fellor);

I don't see the need of reading all values into an array. You can simply traverse the input and do the calculations at the same time. Here is an example that passes all test cases:

Working code that passes all test cases:

int main(){
    int s; 
    int t; 
    scanf("%d %d",&s,&t);
    int a; 
    int b; 
    scanf("%d %d",&a,&b);
    int m; 
    int n; 
    scanf("%d %d",&m,&n);

    int noApples=0;
    int noOranges=0;

    for(int apple_i = 0; apple_i < m; apple_i++){
        int apple;  
        scanf("%d",&apple);
        if (apple+a >= s && t >= apple+a) 
            noApples++;
    }

    for(int orange_i = 0; orange_i < n; orange_i++){
        int orange;
        scanf("%d",&orange);
        if (orange+b >= s && t >= orange+b)
            noOranges++;
    }
    printf("%d\n%d\n", noApples, noOranges);
    return 0;
}
klutt
  • 30,332
  • 17
  • 55
  • 95