HackerRank Coders
HackerRank Coders
  • Home
  • HackerRank
  • LitCode
  • Codeshef
  • Contact Us

HakerRank

LeetCode

Contact

HackerRank Counting Valleys Problem Solution in C

#include<stdio.h>
int main()
{
  int n,valley=0,i;
  scanf("%d",&n);
  char arr[n];
  scanf("%s",arr);
  int level = 0;
  int starts=0;
  for(i=0;i<n;i++)
  {

    if(level == 0 && arr[i] == 'D')
    {
      starts=1;
    }
    if(level==0 && starts==1)
    {
      starts=0;
      valley++;
    }
    if(arr[i]=='U')
    {
      level+=1;
    }
    else if(arr[i] == 'D')
    {
      level-=1;
    }
  }
  printf("%d",valley);
}

HackerRank Counting Valleys Problem Solution in C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int test, sum = 0, count = 0;
    char c;
    bool flag = 1;
    cin >> test;
    c = getchar();
    while(test--)
    {
        c = getchar();
        if(c == 'U')
            ++sum;
        else if(c == 'D')
            --sum;
        if(sum == 0)
            flag = 1;
        else if(flag && sum < 0)
        {
            ++count;
            flag = 0;
        }
    }
    printf("%d", count);
    return 0;
}

HackerRank Counting Valleys Problem Solution in Javascript

function processData(input) {
    input = input.split('\n');
    var walk = input[1].split('');
    
    var count = 0;
    var sea = true;
    var valleys = 0;
    
    walk.forEach((step)=>{
        if(step==='U'){
            count++;
        } else {
            count--;
        }
        
        if(count === 0){
            sea = true;
        } else if (count!== 0 && sea === true){
            sea = false;
            if(count<0){
                valleys++;
            }
        }
    });
    console.log(valleys);
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

 


HackerRank Drawing Book 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 n; 
    scanf("%d",&n);
    int p; 
    scanf("%d",&p);
    if(n-p>p)
        printf("%d",(p/2));
    else
        printf("%d",((n-p)/2));
    return 0;
}

HackerRank Drawing Book 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 solve(n, p){
    //determine the target page position in # of turns from front
    let targetNumTurns = Math.floor(p/2);
    //determine end of book in # of turns from font
    let endNumTurns = Math.floor(n/2);
    //return optimal strategy
    //console.log(targetNumTurns);
    //console.log(endNumTurns);
    return Math.min(targetNumTurns, (endNumTurns - targetNumTurns));
}

function main() {
    var n = parseInt(readLine());
    var p = parseInt(readLine());
    var result = solve(n, p);
    process.stdout.write(""+result+"\n");

}

HackerRank Drawing Book Problem Solution in Python

#!/bin/python3

import sys
n = int(input().strip())
p = int(input().strip())
def do_that(n,k):
    if n%2==1:
        print(min(k//2,(n-k)//2))
    else:
        print(min(k//2,(n+1-k)//2))
do_that(n,p)        

 


HackerRank Sales by Match 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 n,i,j,count=1,sum=0; 
    scanf("%d",&n);
    int *c = malloc(sizeof(int) * n);
    for( i = 0; i < n; i++){
       scanf("%d",&c[i]);
        
    }
    for(i=0;i<n;i++)
        {
        for(j=i+1;j<n;j++)
            {
               if(c[i]==c[j]&&c[i]!=-1)
                   {
                   count++;
                     c[j]=-1;}
               }
           sum=sum+count/2;
        count=1;
        
    }
    printf("%d",sum);
    return 0;
}

HackerRank Sales by Match 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 n = parseInt(readLine());
    c = readLine().split(' ');
    c = c.map(Number);
    
    let counts = {}
    for (var i = 0; i < c.length; i++) {
        counts[c[i]] = counts[c[i]] === undefined ? 0.5 : counts[c[i]] + 0.5
    }
    let count = 0
    Object.keys(counts).map((a)=>count += Math.floor(counts[a]))
    console.log(count)
}

HackerRank Sales by Match Problem Solution in Python

input()

C = [0]*101
for color in map(int, input().strip().split()) :
    C[color] += 1

print(sum([c//2 for c in C]))

 


HackerRank Bill Division Problem Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n, k, total;
    int brian_tot = 0;
    scanf("%d %d\n", &n, &k);
    int *arr = (int*)malloc(sizeof(int) * n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d ", &arr[i]);
        if (i != k)
            brian_tot += arr[i];
    }
    scanf("\n%d", &total);
    if (brian_tot / 2 == total)
        printf("Bon Appetit");
    else
    {
        printf("%d", total - brian_tot/2);
    }
        
    return 0;
}

HackerRank Bill Division Problem Solution in JavaScript

function processData(input) {
    //Enter your code here
    
    var lines = input.split('\n');
    var n_k = lines[0].split(' ');
    var n = parseInt(n_k[0]);
    var k = parseInt(n_k[1]);
    var a = lines[1].split(' ');
    a = a.map(Number);
    var b_charged = parseInt(lines[2]);
    
    var b_actual = 0;
    for (var i = 0; i < a.length; i++) {
        if (i != k) {
            b_actual += a[i];         
        }
    }
    
    var difference = b_charged - (b_actual / 2);
    if (difference === 0)
        console.log("Bon Appetit");
    else
        console.log(difference);
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

HackerRank Bill Division Problem Solution in Python

n, k = list(map(int, input().split()))
L = list(map(int, input().split()))
c = int(input())
L.pop(k)
if sum(L)//2 == c:
    print ('Bon Appetit')
else:
    print (c - sum(L)//2)

 


HackerRank Day of the Programmer Problem Solution C Solution

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

void solve(int year){
    // Complete this function
    char str[20] = "13.09.2017";
    int num_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    if (year >= 1919) {
        if (((year % 400) == 0) || (((year % 4) == 0) && ((year % 100) != 0))) {
            str[1] = '2';
        }
    } else if (year <= 1917) {
        if ((year % 4) == 0) {
            str[1] = '2';
        }
    } else {
        //year = 1918
        str[0] = '2';
        str[1] = '6';
    }
    
    str[6] = '0' + year / 1000;
    str[7] = '0' + (year / 100) % 10;
    str[8] = '0' + (year / 10) % 10;
    str[9] = '0' + year % 10;

    printf("%s\n", str);
}

int main() {
    int year; 
    scanf("%d", &year);
    int result_size;
    solve(year);
    
    return 0;
}

HackerRank Day of the Programmer Problem Solution 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 solve(year){
    // Complete this function
    var days=[31,28,31,30,31,30,31,31,30,31,30,31];
    if(year===1918){
        days[1]=28-13;
    }
    else if(year<=1917){
        
        if(year%4===0){
        days[1]=29;
        }
    }
    else{
         if(year%400===0||(year%4===0&&year%100!==0)){
        days[1]=29;
        }
    }
    
    var count=0,i=0;
    while(count+days[i]<=256){
        count+=days[i];
        i++;
    }
    var day=256-count;
    var month=i+1;
   
    return `${day<10?'0'+day:day}.${month<10?'0'+month:month}.${year}`
}

function main() {
    var year = parseInt(readLine());
    var result = solve(year);
    process.stdout.write(""+result+"\n");

}

HackerRank Day of the Programmer Problem Solution Python

#!/bin/python3

import sys

def solve(year):
    if year<1918:
        if year%4==0:
            d = 12
        else:
            d = 13
    elif year>1918:
        if year%400==0 or (year%4 == 0 and year%100!=0):
            d = 12
        else:
            d =13
    else:
        d= 26

    return '{}.09.{}'.format(d,year)
year = int(input().strip())
result = solve(year)
print(result)



 


HackerRank Migratory Birds 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 n; 
    scanf("%d",&n);
    int *types = malloc(sizeof(int) * n);
    for(int types_i = 0; types_i < n; types_i++){
       scanf("%d",&types[types_i]);
    }
    int c[5] = {0};
    for (int i=0; i<n; i++){
        c[types[i]-1]++;
    }
    int max = -1;
    int maxi = -1;
    for (int i=0; i<5; i++) {
        if(c[i]>max){
            max = c[i];
            maxi = i;
        }
    }
    printf("%d\n", maxi+1);
   
    // your code goes here
    return 0; 
}

HackerRank Migratory Birds 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 n = parseInt(readLine()),
    types = readLine().split(' ');
    types = types.map(Number);
    // your code goes here

    var occ = [0,0,0,0,0],
        max_occ = 0,
        ans = 0
    for (var i = 0 ; i < types.length ; i++) occ[types[i]-1]++
    for (var i = 0 ; i < 5 ; i++) if (occ[i] > max_occ) max_occ = occ[i], ans = i
    
    //console.error(occ)
    console.log(ans + 1)
}

HackerRank Migratory Birds Problem Solution in Python

#!/bin/python3

import sys


n = int(input().strip())
types = list(map(int, input().strip().split(' ')))
# your code goes here
counter = [0 for ii in range(5)]
for t in types:
    counter[t-1] += 1

hf = 0
for nth, c in enumerate(counter[1:], start=1):
    if c > counter[hf]:
        hf = nth

print(hf+1)

 

 


HackerRank Divisible Sum Pairs Problem Solution in C

#include <stdio.h>
#include <stdlib.h>

int main()
{   int n,k,i,j;
    int c=0;
    
     scanf("%d%d",&n,&k);
     int arr[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);

    }


    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(((arr[i]+arr[j])%k)==0)
                c=c+1;
        }
    }
    printf("%d",c);
    return 0;
}

HackerRank Divisible Sum Pairs 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 n_temp = readLine().split(' ');
    var n = parseInt(n_temp[0]);
    var k = parseInt(n_temp[1]);
    a = readLine().split(' ');
    a = a.map(Number);
    var totalPair = 0;
    for(var i = 0; i < n - 1; i++) {
        for(var j = i + 1; j < n; j++) {
            
            if( i !== j && i < j ) {
                if((a[i] + a[j]) % k === 0) {
                    totalPair++;
                }
            }
            
        }
    }
    
    console.log(totalPair);

}

HackerRank Divisible Sum Pairs Problem Solution in Python

#!/bin/python3

import sys


n,k = input().strip().split(' ')
n,k = [int(n),int(k)]
a = [int(a_temp) for a_temp in input().strip().split(' ')]
t = 0
for i in range(n):
    c = a[i]
    for d in a[i+1:]:
        t += [0, 1][(c+d)%k == 0]
        
print(t)

 


HackerRank Subarray Division 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 n,res=0,sum=0,i,j; 
    scanf("%d",&n);
    int *s = malloc(sizeof(int) * n);
    for(int s_i = 0; s_i < n; s_i++){
       scanf("%d",&s[s_i]);
    }
    int d; 
    int m;
    res=0; 
    scanf("%d %d",&d,&m);
    for(i=0;i<n;i++)
        {
       // res=0;
        if(i+m-1<n)
            {sum=0;
            for(j=i;j<=i+m-1;j++)
                 sum=sum+s[j];
            if(sum==d)
            res++;
    }
    }
    printf("%d\n",res);
    return 0;
}

HackerRank Subarray Division 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 solve(n, s, d, m){
    let result = 0;
    
    for (let i = 0; i + m <= s.length; i++) {
        let sum = 0;
        
        for (let j = i; j < i + m; j++) {
            sum += s[j];
        }
        
        if (sum === d) {
            result++;
        }
    }
    
    return result;
}

function main() {
    var n = parseInt(readLine());
    s = readLine().split(' ');
    s = s.map(Number);
    var d_temp = readLine().split(' ');
    var d = parseInt(d_temp[0]);
    var m = parseInt(d_temp[1]);
    var result = solve(n, s, d, m);
    process.stdout.write(""+result+"\n");

}

HackerRank Subarray Division Problem Solution in Python

#!/bin/python3

import sys

def getWays(squares, d, m):
    ways=0
    for i in range(len(squares)-m+1):
        #print(sum(s[i:i+m]))
        if sum(s[i:i+m])==d:
            ways+=1
    return ways

n = int(input().strip())
s = list(map(int, input().strip().split(' ')))
d,m = input().strip().split(' ')
d,m = [int(d),int(m)]
result = getWays(s, d, m)
print(result)

 


HackerRank Breaking the Records 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 n; 
    int min, max, minc=0, maxc=0, i;
    scanf("%d",&n);
    int *score = malloc(sizeof(int) * n);
    for(int score_i = 0; score_i < n; score_i++){
       scanf("%d",&score[score_i]);
    }
    min=max=score[0];
    for(i=1;i<n;i++) {
        if(score[i]<min) {
            minc++;
            min=score[i];
        }
        if(score[i]>max) {
            maxc++;
            max=score[i];
        }
    }
    printf("%d %d",maxc,minc);
    return 0;
}

HackerRank Breaking the Records 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 getRecord(s){
    
    let min = s[0]
    let max = s[0]
    let numberOfTimesHighScore = 0
    let numberOfTimesLowScore = 0
    
    s.slice(1).forEach(score => {
        if (score > max) {
            numberOfTimesHighScore++
            max = score
        } else if (score < min) {
            numberOfTimesLowScore++
            min = score
        }
    })
    return [numberOfTimesHighScore, numberOfTimesLowScore]
    
}

function main() {
    var n = parseInt(readLine());
    s = readLine().split(' ');
    s = s.map(Number);
    var result = getRecord(s);
    console.log(result.join(" "));

}

HackerRank Breaking the Records Problem Solution in Python

#!/bin/python3

import sys


n = int(input())
score = list(map(int, input().strip().split(' ')))
# your code goes here
h = score[0]
hh = 0
l = score[0]
ll = 0
for i in range(n-1):
    if l > score[(i+1)]:
        hh += 1
        l = score[(i+1)]
    elif h < score[(i+1)]:
        ll +=1
        h = score[(i+1)]
print(ll,hh)

 


HackerRank Between Two Sets Problem Solution in C

#include <stdio.h>

long long gcd(long long a, long long b);
long long lcm(long long a, long long b);

int main()
{
    long long a[10], b[10], n, m, i, lcm_a, gcd_b, between = 0;

    scanf("%lld %lld\n", &n, &m);
    for (i = 0; i < n; ++i)
        scanf("%lld", &a[i]);
    for (i = 0; i < m; ++i)
        scanf("%lld", &b[i]);

    lcm_a = a[0];
    for (i = 1; i < n; ++i)
        lcm_a = lcm(lcm_a, a[i]);

    gcd_b = b[0];
    for (i = 1; i < m; ++i)
        gcd_b = gcd(gcd_b, b[i]);

    if (gcd_b % lcm_a == 0)
        for (i = lcm_a; i <= gcd_b; i += lcm_a)
            if (gcd_b % i == 0) ++between;

    printf("%lld", between);
    return 0;
}

long long gcd(long long a, long long b)
{
    long long t;
    while (b)
    {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

long long lcm(long long a, long long b)
{
    return a / gcd(a,b) * b;
}

HackerRank Between Two Sets 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 n_temp = readLine().split(' ');
    var n = parseInt(n_temp[0]);
    var m = parseInt(n_temp[1]);
    a = readLine().split(' ');
    a = a.map(Number);
    b = readLine().split(' ');
    b = b.map(Number);
    var lcm = getLCM(a);
    var gcd = getGCD(b);
    var count = 0;
    for(var i=lcm;i<=gcd;i+=lcm){
        if(gcd%i===0){count++}
    }
    console.log(count);
    
}
function LCM(a,b){
    return a*b/GCD(a,b)
}
function getLCM(a){
    var result = a[0];
    for(var i=1;i<a.length;i++){
        result = LCM(result,a[i]);
    }
    return result;
}
function GCD(a,b){
    var temp;
    while(b>0){
        temp = b;
        b = a%b;
        a = temp;
    }
    return a;
}
function getGCD(a){
   var result = a[0];
    for(var i=1;i<a.length;i++){
        result = GCD(result,a[i]);
    }
    return result;
}

HackerRank Between Two Sets Problem Solution in Python

#!/bin/python3

import sys


n,m = input().strip().split(' ')
n,m = [int(n),int(m)]
a = [int(a_temp) for a_temp in input().strip().split(' ')]
b = [int(b_temp) for b_temp in input().strip().split(' ')]

a.sort()
b.sort()
lower_b = a[n - 1];
upper_b = b[0] + 1;
l = []
for i in range(upper_b - lower_b):
    num = i + lower_b
    flag = 1;
    for j in range(n):
        if(num % a[j] != 0):
            flag = 0
            break
    for j in range(m):
        if(b[j] % num != 0):
            flag = 0
            break
    if(flag == 1):
        l.append(num)
    
print(len(l))
        

 


HackerRank Number Line Jumps 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 x1; 
    int v1; 
    int x2; 
    int v2;
    int m,count=0;
    scanf("%d %d %d %d",&x1,&v1,&x2,&v2);
    if(v2>=v1)
        printf("NO");
    else
        {if((x2-x1)%(v1-v2)==0)
        printf("YES");
        else
        printf("NO");}
    return 0;
}

HackerRank Number Line Jumps 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 x1_temp = readLine().split(' ');
    var x1 = parseInt(x1_temp[0]);
    var v1 = parseInt(x1_temp[1]);
    var x2 = parseInt(x1_temp[2]);
    var v2 = parseInt(x1_temp[3]);
    
    if ((x2>x1 && v2>v1) || (x1>x2 && v1>v2)) {
        console.log('NO');
        return;
    } else {
        if ((x1-x2)%(v2-v1) === 0) console.log('YES');
        else console.log('NO');
    }

}

HackerRank Number Line Jumps Problem Solution in Python

import sys
x1,v1,x2,v2 = input().strip().split(' ')
x1,v1,x2,v2 = [int(x1),int(v1),int(x2),int(v2)]
if v1<=v2:
    print("NO")
    exit()

n=(x1-x2)/(v2-v1)
if n==int(n):
    print("YES")
else:
    print("NO")

 


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)

 


Hackerrank Grading Students Problem Solution in C

#include <stdio.h>

int main(void) {
	// your code goes here
	int t,n,i,ans;
	scanf("%d",&t);
	for(i=1;i<=t;i++)
	{
	    scanf("%d",&n);
	    if(n<38 || n%5==0 || n%5==1 || n%5==2)
	    {
	        printf("%d\n",n);
	    }
	    else if(n%5==3)
	    {
	        ans=n+2;
	        printf("%d\n",ans);
	    }
	    else if(n%5==4)
	    {
	        ans=n+1;
	        printf("%d\n",ans);
	    }
	}
	return 0;
}

Hackerrank Grading Students 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 n = parseInt(readLine());
    for(var a0 = 0; a0 < n; a0++){
        var grade = parseInt(readLine());
        
        if(grade >= 38){
            var closest = Math.ceil(grade / 5) * 5;
            var diff = closest - grade;
            
            if(diff < 3){
                grade = closest;
            }
        }

        console.log(grade);
    }

}

Hackerrank Grading Students Problem Solution in Python

#!/bin/python3

import sys


n = int(input().strip())
for a0 in range(n):
    grade = int(input().strip())
    if grade > 37:
        if grade % 5 > 2:
            grade = grade + 5 - grade % 5
    print (grade)
           

 


HackerRank Time Conversion Problem Solution in C#include <stdio.h>

#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
       char time[10];
    int hr1,hr2,hr3,hr,min,sec,i;
    scanf("%s",time);
    if(time[8]=='A')
    {
        if(time[0]=='1'&&time[1]=='2')
            {
            time[0]='0';
            time[1]='0';
        }
        for(i=0;i<8;i++)
                    printf("%c",time[i]);
    }
    else if(time[8]=='P')
        {
            hr1=time[0]-'0';
            hr2=time[1]-'0';
            hr3=hr1*10+hr2;
            hr=12+hr3;
        if(hr==24)
            hr=12;
            printf("%d",hr);
        for(i=2;i<8;i++)
            printf("%c",time[i]);
    }
    return 0;

}

HackerRank Time Conversion Problem Solution in JavaScript

function processData(input) {
    //Enter your code here

    if(input.indexOf('PM') !== -1) {
        input = input.replace('PM', '');
        var data = input.split(':');
        var hour = parseInt(data[0], 10);
        if(hour !== 12) {
            hour += 12;
        }
        
        data[0] = hour;
        
        console.log(data.join(':'));
        

    } else {
        input = input.replace('AM', '');
        var data = input.split(':');
        var hour = parseInt(data[0], 10);
        if(hour === 12) {
            hour = 0;
        }
        
        data[0] = hour < 10 ? '0' + hour : hour;
        console.log(data.join(':'));
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

HackerRank Time Conversion Problem Solution in Python

inS = input()

if (inS[0:2] == '12'):
    if (inS[8:] == 'AM'):
        hrs = int(inS[0:2])
        hrs = hrs - 12
        hrs = str(hrs) + '0'
        outS = hrs + inS[2:8]
        print(outS)
    else:
        print(inS[0:8])
elif (inS == '12:00:00PM'):
    print('12:00:00')
elif (inS[8:] == 'AM'):
    print(inS[0:8])
else:
    hrs = int(inS[0:2])
    hrs = hrs + 12
    outS = str(hrs) + inS[2:8]
    print(outS)
Older Posts Home

POPULAR POSTS

  • HackerRank Sales by Match Problem Solution
  • HTML क्या है कैसे सीखें?
  • HackerRank Between Two Sets Problem Solution
  • CSS: A Step-by-Step Guide
  • LeetCode Longest Substring Without Repeating Characters Problem Solution

Categories

  • CSS 1
  • HackerRank 23
  • HTML 1
  • Leetcode 3

Search This Blog

Powered by Blogger

Designed by OddThemes | Distributed by Gooyaabi Templates