08
Jan
2020

Hackerrank Apple and Orange (C++)

C++ :

void countApplesAndOranges(int s, int t, int a, int b, vector<int> apples, vector<int> oranges) {
    int noOfApples = 0, noOfOranges = 0, fallDist = 0;

    for(int i = 0; i < apples.size(); i++){
        fallDist = a + apples[i];
        if(fallDist >= s && fallDist <= t){
            noOfApples++;
        }
    }

    for(int j = 0; j < oranges.size(); j++){
        fallDist = b + oranges[j];
        if(fallDist >= s && fallDist <= t){
            noOfOranges++;
        }
    }

    cout << noOfApples << endl << noOfOranges << endl;
}

Explanation:

Basically doing some simple arithmetic and counting the numbers that appear in a range for 2 separate vectors. Nothing really complicated here.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *