Apple and Orange

Sam's house has an apple tree and an orange tree that yield an abundance of fruit. Using the information given below, determine the number of apples and oranges that land on Sam's house.

In the diagram below:

  • The red region denotes the house, where s is the start point, and t is the endpoint. The apple tree is to the left of the house, and the orange tree is to its right.
  • Assume the trees are located on a single point, where the apple tree is at point a, and the orange tree is at a point b.
  • When a fruit falls from its tree, it lands d units of distance from its tree of origin along the -axis. *A negative value of d means the fruit fell d units to the tree's left, and a positive value of d means it falls d units to the tree's right. *

Given the value of d for m apples and n oranges, determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range )?

Example:

Input: s=7,t=11,a=5,b=15,m=3,n=2,x[m]={-2,2,1},y[n]={5,-6}
Output: 1
1

Approach

Java

public class AppleOrange {
    public static void main(String[] args) {
        int s = 7, t = 11, a = 5, b = 15;
        int x[] = { -221 }, y[] = { 5, -6 };
        countApplesAndOranges(s, t, a, b, x, y);
    }

    static void countApplesAndOranges(int sint tint aint b
                    int[] xint[] y) {
        int m = x.length;
        int n = y.length;
        int p = 0, q = 0;

        if (a < s && a < t && a < b && s < t && s < b && t < b) {
            for (int i = 0; i < m; i++) {
                if (x[i] + a >= s && x[i] + a <= t)
                    p++;
            }
            for (int i = 0; i < n; i++) {
                if (b + y[i] >= s && b + y[i] <= t)
                    q++;
            }
        }
        System.out.println(p);
        System.out.println(q);

    }
}

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{

    long int s = 7t = 11a = 5b = 15m = 3n = 2ip = 0q = 0;
    long int x[m] = {-221}, y[n] = {5, -6};

    if (a < s && a < t && a < b && s < t && s < b && t < b)
    {
        for (i = 0i < mi++)
        {
            if (x[i] + a >= s && x[i] + a <= t)
                p++;
        }
        for (i = 0i < ni++)
        {
            if (b + y[i] >= s && b + y[i] <= t)
                q++;
        }
    }
    cout << p << "\n"
         << q << "\n";
    return 0;
}


No comments:

Post a Comment