Apple and Orange - HackerRank || HackerRank Apple and Orange Solution

Problem Name: Apple and Orange

Judge: HackerRank

Problem linkApple and Orange


HackerRank Apple and Orange Solution in C

#include <stdio.h>
int main()
{
    int s, t, a, b, m, n, apple_count = 0, orange_count = 0;
    scanf("%d %d", &s, &t);
    scanf("%d %d", &a, &b);
    scanf("%d %d", &m, &n);
    int apple[m], orange[n];
    for (int i = 0; i < m; i++)
    {
        scanf("%d", &apple[i]);
        if (apple[i] + a >= s && apple[i] + a <= t) apple_count++;
    }
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &orange[i]);
        if (b + orange[i] <= t && b + orange[i] >= s) orange_count++;
    }
    printf("%d\n%d\n", apple_count, orange_count);
    return 0;
}

HackerRank Apple and Orange Solution in C++/Cpp

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int s, t, a, b, m, n, apple_count = 0, orange_count = 0, i;
    cin >> s >> t;
    cin >> a >> b;
    cin >> m >> n;
    int apple[m], orange[n];
    for (i = 0; i < m; i++)
    {
        cin >> apple[i];
        if (apple[i] + a >= s && apple[i] + a <= t) apple_count++;
    }
    for (i = 0; i < n; i++)
    {
        cin >> orange[i];
        if (b + orange[i] <= t && b + orange[i] >= s) orange_count++;
    }
    cout << apple_count << endl;
    cout << orange_count << endl;
    return 0;
}

HackerRank Apple and Orange Solution in Java

import java.util.Scanner;

public class Apple_and_Orange {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int s = input.nextInt();
        int t = input.nextInt();
        int a = input.nextInt();
        int b = input.nextInt();
        int m = input.nextInt();
        int n = input.nextInt();
        int[] apple = new int[m];
        int[] orange = new int[n];
        int i, apple_in = 0, orange_in = 0;
        for (i = 0; i < m; i++) {
            apple[i] = input.nextInt();
            if (a + apple[i] >= s && a + apple[i] <= t) apple_in++;
        }
        for (i = 0; i < n; i++) {
            orange[i] = input.nextInt();
            if (b + orange[i] <= t && b + orange[i] >= s) orange_in++;
        }
        System.out.println(apple_in + "\n" + orange_in);
    }
}




Next Post Previous Post
No Comment
Add Comment
comment url