Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerRank Separate the Numbers problem solution

YASH PAL, 31 July 202422 January 2026

HackerRank Separate the Numbers problem solution – 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.

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 z, 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.

HackerRank Separate the Numbers problem solution

HackerRank Separate the Numbers problem solution in Python.

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')

Separate the Numbers problem solution in Java.

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')        
      }
    }

}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes