In this post, we will solve the Separate the Numbers HackerRank Solution. This problem (Separate the Numbers) is a part of the HackerRank Problem Solving series.
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:
- a[i] – a[i-1] = 1 for any 1<i<=n (i.e., each element in the sequence is 1 more than the previous element).
- No a[i] contains a leading zero. For example, we can split into the sequence , but it is not beautiful because and have leading zeroes.
- The contents of the sequence cannot be rearranged. For example, we can split into the sequence , but it is not beautiful because it breaks our first constraint (i.e., ).
The diagram below depicts some beautiful strings:
Perform queries where each query consists of some integer string . For each query, print whether or not the string is beautiful on a new line. If it is beautiful, print YES x
, where is the first number of the increasing sequence. If there are multiple such values of , choose the smallest. Otherwise, print NO
.
Function Description
Complete the separateNumbers function in the editor below.
separateNumbers has the following parameter:
- s: an integer value represented as a string
Prints
– string: Print a string as described above. Return nothing.
Input Format
The first line contains an integer , the number of strings to evaluate.
Each of the next lines contains an integer string to query.
Separate the Numbers Hacker Rank Solution
Problem solution in Python programming:
def separateNumbers(s): l = len(s) ls = [] flag = 0 for i in range(1,l//2+1): st = s[:i] temp = st count = 1 while len(temp) < len(s): temp = temp + str(int(st)+count) count += 1 if temp == s: print(f"YES {st}") flag = 1 break if flag == 0: print("NO")
Problem solution in JavaScript programming:
function separateNumbers(s) { const len = s.length; let first = 0; // first number of the increasing sequence let nDigits = 1; // "91011", 1 = "9", 2 = "91", 3 = "910", ... let p = 0; // start index let q = 1; // end index let incr = 1; // increment let number = 0; // string to BigInt let search = 0; // number to search (number + 1) let next = 0; // string segment for look up while (q < len) { number = BigInt(s.slice(p, q)); search = (number + 1n).toString(); nDigits = search.length; next = s.slice(q, q + nDigits); if (search[0] !== '0' && search === next) { if (p === 0) first = number; p = q; q = p + nDigits; if (q >= len) { console.log('YES ' + first); return 1; } } else { p = 0; q = (++incr); } } console.log('NO'); }
Problem solution in Java programming:
public static void separateNumbers(String s) { // Write your code here String sub = ""; boolean isValid = false; for (int i = 1; i <= s.length() / 2; i++) { sub = s.substring(0, i); long num = Long.parseLong(sub); StringBuilder valid = new StringBuilder(sub); while (valid.length() < s.length()) { valid.append(++num); } if (s.equals(valid.toString())) { isValid = true; break; } } System.out.println(isValid ? "YES " + sub : "NO"); }
Leave a Reply