Apple and Orange – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
C++ Apple and Orange HackerRank Solution
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <ctime>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
const double eps = 1e-8;
const int maxn = 112345;
int main()
{
//fread("");
//fwrite("");
int s,t,a,b,m,n,x;
int aa,bb;
aa = bb = 0;
scanf("%d%d%d%d%d%d",&s,&t,&a,&b,&m,&n);
while(m--)
{
scanf("%d",&x);
if(s <= a+x && a+x <= t) aa++;
}
while(n--)
{
scanf("%d",&x);
if(s <= b+x && b+x <= t) bb++;
}
printf("%d\n%d\n",aa,bb);
return 0;
}
Java Apple and Orange HackerRank Solution
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int s = in.nextInt();
int t = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
int m = in.nextInt();
int n = in.nextInt();
int[] apple = new int[m];
int app = 0;
for(int apple_i=0; apple_i < m; apple_i++){
apple[apple_i] = in.nextInt();
if (a + apple[apple_i] >= s && a + apple[apple_i] <= t) {
app++;
}
}
int[] orange = new int[n];
int or = 0;
for(int orange_i=0; orange_i < n; orange_i++){
orange[orange_i] = in.nextInt();
if (b + orange[orange_i] >= s && b + orange[orange_i] <= t) {
or++;
}
}
System.out.println(app);
System.out.println(or);
}
}
Python 3 Apple and Orange HackerRank Solution
s,t = map(int, input().strip().split(' '))
a,b = map(int, input().strip().split(' '))
m,n = map(int, input().strip().split(' '))
apple = list(map(int, input().strip().split(' ')))
orange = list(map(int, input().strip().split(' ')))
sapple = sum([1 for f in apple if (f+a) >= s and (f+a) <= t])
sorange = sum([1 for f in orange if (f+b) >= s and (f+b) <= t])
print (sapple)
print (sorange)
Python 2 Apple and Orange HackerRank Solution
#!/bin/python
import sys
s,t = raw_input().strip().split(' ')
s,t = [int(s),int(t)]
a,b = raw_input().strip().split(' ')
a,b = [int(a),int(b)]
m,n = raw_input().strip().split(' ')
m,n = [int(m),int(n)]
apple = map(int,raw_input().strip().split(' '))
orange = map(int,raw_input().strip().split(' '))
acount=0
for i in apple:
if s<=a+i<=t:
acount+=1
ocount=0
for i in orange:
if s<=b+i<=t:
ocount+=1
print acount
print ocount
C Apple and Orange HackerRank Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int s;
int t;
scanf("%d %d",&s,&t);
int a;
int b;
scanf("%d %d",&a,&b);
int m;
int n;
int ans1=0,ans2=0;
scanf("%d %d",&m,&n);
int *apple = malloc(sizeof(int) * m);
for(int apple_i = 0; apple_i < m; apple_i++){
scanf("%d",&apple[apple_i]);
if((apple[apple_i]+a)<=t && (apple[apple_i]+a)>=s)
ans1++;
}
int *orange = malloc(sizeof(int) * n);
for(int orange_i = 0; orange_i < n; orange_i++){
scanf("%d",&orange[orange_i]);
if((orange[orange_i]+b)<=t && (orange[orange_i]+b)>=s)
ans2++;
}
printf("%d\n%d",ans1,ans2);
return 0;
}