HackerRank Separate the Numbers problem solution YASH PAL, 31 July 2024 In this HackerRank Separate the Numbers, problem 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. Problem solution in Python programming. import sys q = int(input()) best = None for _ in range(q): s = input().strip() found = False for i in range(len(s)//2): a = s[:i+1] f = n = int(s[:i+1]) while len(a) < len(s): n += 1 a += str(n) if a == s: found = True print('YES',f) break if not found: print('NO') Problem solution in Java Programming. import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { private static boolean check(final String s, final long n) { final String numberString = String.valueOf(n); if (s.length() < numberString.length()) { return false; } else if (s.startsWith(numberString)) { final String nextString = s.substring(numberString.length()); final long nextNumber = n + 1L; return nextString.isEmpty() || check(nextString, nextNumber); } else { return false; } } private static long breakString(final String s) { for (int i = 0; i < s.length() / 2; i++) { final long start = Long.parseLong(s.substring(0, i + 1)); final String nextString = s.substring(i + 1); final long nextNumber = start + 1L; if (check(nextString, nextNumber)) { return start; } } return -1L; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int q = in.nextInt(); for(int a0 = 0; a0 < q; a0++){ String s = in.next(); final long start = breakString(s); if (start == -1) { System.out.println("NO"); } else { System.out.printf("YES %d%n", start); } } } } Problem solution in C++ programming. #include <bits/stdc++.h> using namespace std; int main(){ int q; cin >> q; for(int a0 = 0; a0 < q; a0++){ string s; cin >> s; // your code goes here bool poss=false; long long int ans=0; if(s[0]=='0'){ string ns="0"; long long int next=1; while(ns.size()<s.size()){ ns+=to_string(next); next++; } if(ns==s) poss=true; } else{ for(int i=1;i<=(s.size()/2);i++){ ans*=10; ans+=(s[i-1]-'0'); long long int next=ans+1; string ns=s.substr(0,i); while(ns.size()<s.size()){ ns+=to_string(next); next++; } if(ns==s){ poss=true; break; } } } if(poss&&s.size()>1) cout<<"YES "<<ans<<endl; else cout<<"NO"<<endl; } return 0; } Problem solution in C programming. #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> bool rec_is_beautiful(char *s, long long int* n) { // fprintf(stderr, "s=%s n=%lld; ", s, *n); long long int l = strlen(s); if (l == 1 && *n == 0 ) return false; if (l == 0) return true; if(*n <= 0) { long long int c = s[0]-'0'; if(c == 0) return false; long long int a0 = c; long long int a1 = a0+1; char *a1_str = malloc(sizeof(char)*(l+1)); sprintf(a1_str, "%lld", a1); long long int l_a1 = strlen(a1_str); if(l_a1 > l) return false; if(rec_is_beautiful(&s[1], &a0)) { *n = a0; return true; } return false; } else { long long int a1 = *n + 1; char *a1_str = malloc(sizeof(char)*(l+1)); sprintf(a1_str, "%lld", a1); long long int l_a1 = strlen(a1_str); if(l_a1 > l) return false; if(strncmp(a1_str, &s[0], l_a1) == 0 && rec_is_beautiful(&s[l_a1], &a1)) { return true; } else { *n = (*n)*10 + s[0]-'0'; return rec_is_beautiful(&s[1], n); } return false; } return false; } bool is_beautiful(char *s, long long int* n) { bool b = rec_is_beautiful(s, n); long long int l = strlen(s); char *n_str = malloc(sizeof(char)*(l+1)); sprintf(n_str, "%lld", *n); return b && l>strlen(n_str); } int main(){ long long int q; scanf("%lld",&q); for(long long int a0 = 0; a0 < q; a0++){ char* s = (char *)malloc(512000 * sizeof(char)); scanf("%s",s); long long int n = 0; if(is_beautiful(s, &n)) { printf("YES %lldn", n); } else { printf("NOn"); } } return 0; } Problem solution in JavaScript programming. process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; 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 //////////////////// var BigNumber = require('bignumber.js'); function main() { var q = parseInt(readLine()); for(var a0 = 0; a0 < q; a0++){ var s = readLine(); // your code goes here var flag = true; for (let len = 1; len<s.length-1; len++){ var first = new BigNumber(s.substr(0,len)); var num = new BigNumber(s.substr(0,len)); //console.log(first) if (s.length <= len) { continue; } var sNew = ''.concat(first.toString()); while (sNew.length < s.length) { num=num.add(1); sNew = sNew.concat(num.toString()); } //console.log(sNew) if (sNew===s) { console.log('YES '+first); flag = false; continue } } if (flag){ console.log('NO') } } } algorithm coding problems