HackerRank C- Apple and Orange




Complete the countApplesAndOranges function in the editor below. It should print the number of apples and oranges that land on Sam's house, each on a separate line.

countApplesAndOranges has the following parameter(s):

s: integer, starting point of Sam's house location.
t: integer, ending location of Sam's house location.
a: integer, location of the Apple tree.
b: integer, location of the Orange tree.
apples: integer array, distances at which each apple falls from the tree.
oranges: integer array, distances at which each orange falls from the tree.



  1. #include<stdio.h>
  2. int main(){ int s;
  3. int t,count1=0,count2=0;
  4. scanf("%d %d",&s,&t);
  5. int a;
  6. int b;
  7. scanf("%d %d",&a,&b);
  8. int m;
  9. int n;
  10. scanf("%d %d",&m,&n);
  11. int temp;
  12. for(int apple_i = 0; apple_i < m; apple_i++)
  13. { scanf("%d",&temp);
  14. if(temp+a>=s && temp+a<=t)
  15. { count1++; }
  16. }
  17. for(int orange_i = 0; orange_i < n; orange_i++)
  18. { scanf("%d",&temp);
  19. if(temp+b>=s && temp+b<=t)
  20. { count2++; }
  21. }
  22. printf("%d\n",count1);
  23. printf("%d",count2); return 0;
  24. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube