HackerRank Separate the Numbers Solution Yashwant Parihar, April 25, 2023April 28, 2023 In this post, we will solve HackerRank Separate the Numbers Problem Solution. 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 s = 10203 into the sequence {1, 02, 03}, but it is not beautiful because 02 and 03 have leading zeroes. 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., 1-3 #1).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. 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 q, the number of strings to evaluate.Each of the next q lines contains an integer string s to query. Sample Input 0 7 1234 91011 99100 101103 010203 13 1 Sample Output 0 YES 1 YES 9 YES 99 NO NO NO NO Explanation 0The first three numbers are beautiful (see the diagram above). The remaining numbers are not beautiful: For s 101103, all possible splits violate the first and/or second conditions. = For s = 010203, it starts with a zero so all possible splits violate the second condition. For 8 = 13, the only possible split is {1,3}, which violates the first condition. For s = 1, there are no possible splits because & only has one digit. Sample Input 1 4 99910001001 7891011 9899100 999100010001 Sample Output 1 YES 999 YES 7 YES 98 NO HackerRank Separate the Numbers Problem Solution Separate the Numbers C Solution #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> unsigned long nblen(unsigned long nb) { unsigned long len; len = 0; if (nb == 0) return (1); while (nb > 0) { nb /=10; len++; } return len; } unsigned long isbeautifull(char* str, unsigned long nbl) { char* ptr; if (strlen(str) == nbl) return (nbl); if (strlen(str) < 2*nbl) return (0); char* first = (char *)malloc((nbl +1)*sizeof(char)); char* second = (char *)malloc((nbl + 2)*sizeof(char)); strncpy(first, str, nbl); strncpy(second, str+nbl, nblen(strtoul(first,&ptr,10) + 1)); char* next = (char *)malloc((strlen(str)-nbl+2)*sizeof(char)); strncpy(next, str+nbl, strlen(str)-nbl); if((strtoul(first,&ptr,10) + 1 == strtoul(second,&ptr,10)) && isbeautifull((next), strlen(second)) == strlen(second)) return (nbl); return (0); } int main(){ int q; scanf("%d",&q); for(int a0 = 0; a0 < q; a0++){ char* s = (char *)malloc(512000 * sizeof(char)); scanf("%s",s); unsigned long mlen; unsigned long i; mlen = strlen(s)/2; i = 1; if(strlen(s) > 2) { while (i <= mlen) { if (isbeautifull(s, i) == i) { char* nb = (char *)malloc((i+1)*sizeof(char)); strncpy(nb,s,i); printf("YES %s\n", nb); break; } else { i++; if (i > mlen) printf("NO\n"); } } free(s); } else printf("NO\n"); } return (0); } Separate the Numbers C++ Solution #include <bits/stdc++.h> using namespace std; #define ms(s, n) memset(s, n, sizeof(s)) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--) #define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define FORalld(it, a) for (__typeof((a).rbegin()) it = (a).rbegin(); it != (a).rend(); it++) #define sz(a) int((a).size()) #define present(t, x) (t.find(x) != t.end()) #define all(a) (a).begin(), (a).end() #define uni(a) (a).erase(unique(all(a)), (a).end()) #define pb push_back #define pf push_front #define mp make_pair #define fi first #define se second #define prec(n) fixed<<setprecision(n) #define bit(n, i) (((n) >> (i)) & 1) #define bitcount(n) __builtin_popcountll(n) typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pi; typedef vector<int> vi; typedef vector<pi> vii; const int MOD = (int) 1e9 + 7; const int INF = (int) 1e9; const ll LINF = (ll) 1e18; const ld PI = acos((ld) -1); const ld EPS = 1e-9; inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;} inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;} template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;} template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;} template<class T> inline T isqrt(T k) {T r = sqrt(k) + 1; while (r * r > k) r--; return r;} template<class T> inline T icbrt(T k) {T r = cbrt(k) + 1; while (r * r * r > k) r--; return r;} inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;} inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;} inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;} inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);} inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;} inline int sign(ld x, ld y) {return sign(x - y);} long long query(string s) { long long res = 0; FOR(i, 0, sz(s)) { res = res * 10 + s[i] - '0'; } return res; } string query(long long n) { string res = ""; while (n) { res += '0' + n % 10; n /= 10; } reverse(all(res)); return res; } void solve() { int q; cin >> q; while (q--) { string s; cin >> s; if (s[0] == '0') { cout << "NO\n"; continue; } long long ans = LINF; FOR(i, 1, min(16, sz(s) / 2) + 1) { string t = s.substr(0, i); long long k = query(t); while (sz(t) < sz(s)) { t += query(++k); } if (s == t) { ans = query(s.substr(0, i)); break; } } if (ans == LINF) { cout << "NO\n"; } else { cout << "YES " << ans << "\n"; } } } int main() { #ifdef _LOCAL_ freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); #else ios_base::sync_with_stdio(0); cin.tie(0); #endif solve(); cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"; return 0; } Separate the Numbers C Sharp Solution using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; class Solution { static BigInteger bkt(string s, BigInteger lastnr, int curPos) { if(s.Length == 1) return -1; if(curPos == s.Length) return lastnr; for(int i = curPos; i < s.Length; i++) { if(curPos == 0 && i >= s.Length / 2) return -1; BigInteger curNr = BigInteger.Parse(s.Substring(curPos, i - curPos + 1)); if(curNr == 0) return - 1; if(curPos == 0 || curNr == lastnr + 1) if(bkt(s, curNr, i + 1) != -1) return curNr; } return -1; } static void Main(String[] args) { int q = Convert.ToInt32(Console.ReadLine()); for(int a0 = 0; a0 < q; a0++){ string s = Console.ReadLine(); BigInteger res = bkt(s, 0, 0); if(res != -1 && res != 0) Console.WriteLine("YES " + res); else Console.WriteLine("NO"); } } } Separate the Numbers Java Solution import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; class Result { /* * Complete the 'separateNumbers' function below. * * The function accepts STRING s as parameter. */ public static void separateNumbers(String s) { // Write your code here for (int i = 1; i <= s.length() / 2; i++) { String str = s; long value = Long.parseLong(s.substring(0, i)); long startValue = value; boolean failed = false; while(str.length() > 0) { String subs = str.substring(0, Math.min(String.valueOf(value).length(), str.length()) ); if (!String.valueOf(Long.parseLong(subs)).equals(String.valueOf(value))) { failed = true; break; } str = str.substring( String.valueOf(value).length() ); value++; } if (!failed) { System.out.println("YES " + startValue); return; } } System.out.println("NO"); } } public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int q = Integer.parseInt(bufferedReader.readLine().trim()); IntStream.range(0, q).forEach(qItr -> { try { String s = bufferedReader.readLine(); Result.separateNumbers(s); } catch (IOException ex) { throw new RuntimeException(ex); } }); bufferedReader.close(); } } Separate the Numbers JavaScript Solution process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; let BigNumber = require('bignumber.js'); 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 processLine(s) { let first; for (let l = 1; l <= s.length / 2; l++) { let i = l; let nb = first = new BigNumber(s.substr(0, l)); while (i < s.length) { let l2 = (nb.add(1)).toString().length; let next = new BigNumber(s.substr(i, l2)); i += l2; if (!next.equals(nb.add(1))) break; nb = next; if (i >= s.length) return console.log(`YES ${first}`); } } return console.log('NO'); } function main() { var q = parseInt(readLine()); for (var a0 = 0; a0 < q; a0++) { var s = readLine(); processLine(s); // your code goes here } } Separate the Numbers Python Solution #!/bin/python3 import sys q = int(input().strip()) for a0 in range(q): s = [int(x) for x in input().strip()] # your code goes here if s[0] == 0: print('NO') continue num = s[0] genislik = 1 l = [] found = False while genislik < (len(s)//2) + 1: l = [] q = 0 for i in range(genislik): l.append(s[i]) num = int(''.join(map(str, l))) q += len(l) not_found = False while q < len(s): qy = 0 num += 1 nm_arr = [int(x) for x in str(num)] if s[q] == 0: not_found = True break for z in range(q, q+len(nm_arr)): if z == len(s): not_found = True break if nm_arr[qy] != s[z]: not_found = True break qy += 1 if not_found is True: break else: q += len(nm_arr) genislik += 1 if q == len(s): print('YES {}'.format(int(''.join(map(str, l))))) found = True break if found is False: print('NO') Other Solutions HackerRank Funny String Problem Solution HackerRank Counting Sort 1 Problem Solution c C# C++ HackerRank Solutions java javascript python CcppCSharpHackerrank Solutionsjavajavascriptpython