7 kyu

Strange mathematics

744 of 2,060LarisaOvchinnikova

Description:

In a certain kingdom, strange mathematics is taught at school. Its main difference from ordinary mathematics is that the numbers in it are not ordered in ascending order, but lexicographically, as in a dictionary (first by the first digit, then, if the first digit is equal, by the second, and so on). In addition, we do not consider an infinite set of natural numbers, but only the first n numbers.

So, for example, if n = 11, then the numbers in strange mathematics are ordered as follows:

1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9.

Help your students to learn this science: write a function that receives two integer numbers: n (total amount of numbers in strange mathematics) and k (number from sequence) and returns the location of a given number k in the order defined in strange mathematics.

For example, if n = 11 and k = 2, the function should return 4 as the answer.

Input: 1 <= n <= 100 000 , 1 <= k <= n.

Output: position of the number k in sequence of the first n natural numbers in lexicographic order. Numbering starts with 1.

Examples:

strange_math(11, 2) == 4
strange_math(15, 5) == 11
strange_math(15, 15) == 7
strangeMath(11, 2) === 4
strangeMath(15, 5) === 11
strangeMath(15, 15) === 7
Sorting
Arrays
Fundamentals

Stats:

CreatedMar 7, 2021
PublishedMar 9, 2021
Warriors Trained3499
Total Skips59
Total Code Submissions4814
Total Times Completed2060
Python Completions744
JavaScript Completions1198
Java Completions169
Total Stars46
% of votes with a positive feedback rating92% of 448
Total "Very Satisfied" Votes385
Total "Somewhat Satisfied" Votes51
Total "Not Satisfied" Votes12
Total Rank Assessments40
Average Assessed Rank
7 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • LarisaOvchinnikova Avatar
  • WestwardLand968 Avatar
  • diggi55 Avatar
Ad