Beautiful Days at the Movies

Beautiful Days at the Movies

Lily likes to play games with integers. She has created a new game where she determines the difference between a number and its reverse. For instance, given the number 12, its reverse is 21. Their difference is 9. The number 120 reversed is 21, and their difference is 99.

She decides to apply her game to decision making. She will look at a numbered range of days and will only go to a movie on a beautiful day.

Given a range of numbered days, [i…j] and a number k, determine the number of days in the range that are beautiful. Beautiful numbers are defined as numbers where |i - reverse(i)| is evenly divisible by k. If a day’s value is a beautiful number, it is a beautiful day. Print the number of beautiful days in the range.

Function Description

Complete the beautifulDays function in the editor below. It must return the number of beautiful days in the range.

beautifulDays has the following parameter(s):

  • i: the starting day number
  • j: the ending day number
  • k: the divisor

Input Format

A single line of three space-separated integers describing the respective values of i, j, and k.

Constraints

  • 1 <= i <= j <= 2 X 106
  • 1 <= k <= 2 X 100

Output Format

Print the number of beautiful days in the inclusive range between i and j.

Sample Input

1
20 23 6

Sample Output

1
2

Explanation

Lily may go to the movies on days 20, 21, 22, and 23. We perform the following calculations to determine which days are beautiful:

  • Day 20 is beautiful because the following evaluates to a whole number:
  • Day 21 is not beautiful because the following doesn’t evaluate to a whole number:
  • Day 22 is beautiful because the following evaluates to a whole number:
  • Day 23 is not beautiful because the following doesn’t evaluate to a whole number:

Only two days, 20 and 22, in this interval are beautiful. Thus, we print 2 as our answer.


Solution

1
2
3
4
5
6
7
8
9
// Complete the beautifulDays function below.
function beautifulDays(i, j, k) {
return new Array(j-i+1).fill(0).reduce((target, item, index) => {
let value = i + index;
!(Math.abs(value - +[...String(value)].reverse().join('')) % k) && target++;

return target;
}, 0);
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×