JavaScript (Separate the Numbers)

Good Evening, I’ve been going a hacker-Rank > Mount-Blue Job Challenge whose description is something like this :-

Separate the Numbers

A numeric string, s, is beautiful if it can be split into a sequence of two or more positive integers, a(1),a(2) ,…,a(n) satisfying the following conditions:

1.a(i)-a(i-1)=1 for any1<i<=n (i.e., each element in the sequence is 1 more than the previous element).
2. No a(i) contains a leading zero. For example, we can split, s=10203 into the sequence {1,02,03} , but it is not beautiful because 02 and 03 have leading zeroes.
3. The contents of the sequence cannot be rearranged. For example, we can split s = 312 into the sequence {3,1,2} , but it is not beautiful because it breaks our first constraint (i.e., ).

The diagram below depicts some beautiful strings:


Perform q queries where each query consists of some integer string s . For each query, print whether or not the string is beautiful on a new line. If it is beautiful, print YES x, where ‘x’ is the first number of the increasing sequence. If there are multiple such values of ‘x’ , choose the smallest. Otherwise, print NO.

The code which I have come up is something like this

function separateNumbers(s) {
    // Write your code here
    let strEle = []; let ele = ''
    let len = parseInt(s.length/2)
    for(let i=0; i<len; i++){
       ele = ele + s[i]
    let posStr = []
    for(let i=0; i<len; i++){
      let d = strEle[i]; ele = ''
      for(let j=0; j<s.length; j++){
          ele = ele + d++
    let cond = 0; let start = 0
    for(let i=0; i<len; i++){
        start = strEle[i]
      console.log('YES', start)
    return ''
let s = separateNumbers('1234')

This code runs perfectly for most of the cases but there are three cases for which this code is giving wrong answer (case-4, case-16, case-18). I’m unable to find out what parameters are there inside these three cases if you know please help me find it

1 Like

Plz can you help me find the entities for which this code is failing to execute

you need to look for an applicable method. how about regex ?

I can see 34 people looked at this and kinda no-ped out.
I guess you are advanced.

Well I’m not that advanced just fine with nested loops and about the regex, I don’t have much command on it just know some basic level, but still I’m working on it.
And yeah many people have viewed my query and are giving no response. I’m also wondering why didn’t they understood what my question is ? Or are they having some problem understanding the logic behind and want a detailed explanation.
Because if that’s the case I can explain both the question and the code and logic behind it

your code prints NO for function call separateNumbers('4')
what do you mean by saying that code is giving wrong answer for case-4?

your question is fine. Diving into it, however, requires certain amount of time and effort, especially since it is not part of freecodecamp curriculum
from my own experience requesting help here, it may take some time before someone will find it possible to answer your question

1 Like

Hi Shounak,
What are the constraints, can a zero be present in the starting of the string?

No, not like that each cases has ‘n’ no. numeric strings for which I’ve to tell whether they are beautiful string or not and if yes than what is their starting digit.

For Example :-
Q.1 ) 7

here, the top number ‘7’ represents the total no. numeric strings

Ans.) YES 1
YES 99

Q.2) 4 (here , 4 represents only how many strings we have to check)

Ans.2) YES 999
YES 98

like this all the cases have been defined

No the string should not start with zero .

total no. of numeric string in each cases should between 1 to 10.
Total length of string should not exceed 32.
and each digit should come in the range in 0 to 9.

and like this there are 22 cases to be checked where except 3 cases (case-4, case-16, case-18) every case has been passed by my code.

And yes this question is not from curriculum itself its the problem from HankerRank, MountBlue Job challenge the link is :-
Programming problems and Competitions :: HackerRank
Enter Contest
5’th page - question no.8

yeah I think this is a toughy…:laughing: But it is awesome. It is good exercise. It got us thinking and working together.

Thank you.

how about the array.sort() method ?

No man it’s not telling us to sort any number or array, basically the question is telling that we have to check whether a number is beautiful string or not.

A beautiful numeric string is a number which doesn’t start with 0 which has all its digit incremented by 1 and we should not rearrange the digits of the given number.

for example ‘1234’ is a beautiful string which has starting digit ‘1’ then all are incrementing by 1 that is { ‘1’, ‘2’, ‘3’, ‘4’}

‘9101112’ is a beautiful string starting with ‘9’ then incrementing by 1 { ‘9’, ‘10’, ‘11’, ‘12’}

but, ‘010203’ is not beautiful as its starting from zero and ‘312’ is not beautiful as none of the digit increments by 1 (arranging is prohibited)

And to solve this problem I’ve come up with a logic, that, first of all I’ve found out the starting digits for the given number. For example,

If the number is ‘1234’, then all the possible digit which can be taken as starting digits are ‘1’ & ‘12’, through which we can form digits like if we take ‘1’ then we will form number ‘1234’ and if we take ‘12’ we can form ‘1213’. Then if we check both the number that first number is matching with the given number so yes the given number ‘1234’ is beautiful string which starts from ‘1’

Similarly, if we take 9101112, the starting digits may be considered as ‘9’ , ‘91’ & ‘910’, the first digit will form ‘9101112’, second will form ‘91929394’, third will form ‘910911912’, from above strings we can clearly see that the string which starts from ‘9’ is matching the given string so yes the given string 9101112 is a beautiful string staring with digit ‘9’.

well, if we take 92939495, we can take the starting digits as ‘9’ , ‘92’ , ‘929’ , ‘9293’, the first digit ‘9’ will form ‘910111213’, second ‘92’ will form ‘92939495’, third will form ‘929’, will form ‘929930931’, fourth will form ‘92939294’ , we can clearly see that our second string matches the given number 92939495 so its beautiful number starting with ‘92’.

i delved into the workings of the sort method once and I know it compares the previous index to the next and I was thinking perhaps we can uterlie that compare function and take advantage of it somehow.
I will look more into it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.