HackerRank Apple and Orange Problem Solution

 


HackerRank Apple and Orange Problem Solution in C

#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; 
    scanf("%d %d",&m,&n);
    int *apple = malloc(sizeof(int) * m);
    int x=0,y=0;
    for(int apple_i = 0; apple_i < m; apple_i++)
    {
       scanf("%d",&apple[apple_i]);
        if( (apple[apple_i]+a)>=s&&(apple[apple_i]+a)<=t)
            x++;
    }
    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)
            y++;
    }
    printf("%d\n%d",x,y);
    return 0;
}

HackerRank Apple and Orange Problem Solution in JavaScript

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var s_temp = readLine().split(' ');
    var s = parseInt(s_temp[0]);
    var t = parseInt(s_temp[1]);
    var a_temp = readLine().split(' ');
    var a = parseInt(a_temp[0]);
    var b = parseInt(a_temp[1]);
    var m_temp = readLine().split(' ');
    var m = parseInt(m_temp[0]);
    var n = parseInt(m_temp[1]);
    apple = readLine().split(' ');
    apple = apple.map(Number);
    orange = readLine().split(' ');
    orange = orange.map(Number);

    
    apple_fall = apple.map(function(a_temp) {
  return a + a_temp;
});

orange_fall = orange.map(function(o_temp) {
  return b + o_temp;
});


var apple_hit = 0, orange_hit = 0;
apple_fall.map(function(af){
  apple_hit += ( af >= s && af <= t ) ? 1 : 0;
});
orange_fall.map(function(of){
  orange_hit += ( of >=s && of <= t ) ? 1 : 0;
});

console.log(apple_hit);
console.log(orange_hit);
    
}

HackerRank Apple and Orange Problem Solution in Python

#!/bin/python3

import sys


s,t = input().strip().split(' ')
s,t = [int(s),int(t)]
a,b = input().strip().split(' ')
a,b = [int(a),int(b)]
m,n = input().strip().split(' ')
m,n = [int(m),int(n)]
apple = [int(apple_temp) for apple_temp in input().strip().split(' ')]
orange = [int(orange_temp) for orange_temp in input().strip().split(' ')]
ac=0
oc=0
for i in apple:
    if s<=i+a<=t:
        ac+=1
for i in orange:
    if s<=b+i<=t:
        oc+=1
print(ac)
print(oc)

0 Comments