apple and orange hackerrank code example

Example 1: Apple and Orange hackerrank

static void countApplesAndOranges(int s, int t, int a, int b, int[] apples, int[] oranges) {
    int appleCount = 0;
    int orangeCount = 0;
    for(int i:apples){
        if(s<=i+a && i+a<=t)
            appleCount++;
    }
    for (int j : oranges) {
        if (s <= j + b && j+b <= t)
            orangeCount++;
    }
    System.out.println(appleCount);
    System.out.println(orangeCount);

}

Example 2: apple and orange hackerrank solution in c++

#include<iostream>
using namespace std;
 
int main()
{
      int s,t,a,b,m,n;
    cin>>s>>t>>a>>b>>m>>n;
    int arr[m];
    int arr1[n];
    int apple=0;
    int orange=0;
   

    for(int i=0;i<m;i++){
        cin>>arr[i];
        if(a+arr[i]>=s && a+arr[i]<=t){
            apple++;
        }
    }


     for(int i=0;i<n;i++){
        cin>>arr1[i];
        if(b+arr1[i]>=s && b+arr1[i]<=t){
            orange++;
        }
    }
    cout<<apple<<endl<<orange<<endl;



}

Tags:

Misc Example