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 Bonetrousle problem solution

YASH PAL, 31 July 202425 January 2026

In this HackerRank Bonetrousle problem solution we have given the values of n, k, and b for t trips to the store, determine which boxes Papyrus must purchase during each trip. For each trip, print a single line of b distinct space-separated integers denoting the box number for each box of spaghetti Papyrus purchases (recall that the store only has one box of each kind). If it’s not possible to buy n sticks of spaghetti by purchasing b boxes, print -1 instead.

HackerRank Bonetrousle problem solution

HackerRank Bonetrousle problem solution in Python.

t = int(input())
for _ in range(t):
    n, k, b = [int(x) for x in input().split()]
    cur_sum = int(b * (1 + b) / 2)
    res = [x + 1 for x in range(b)]
    if cur_sum > n:
        print(-1)
        continue
    max_shift = k - b;
    for i in reversed(range(b)):
        shift = min(max_shift, n - cur_sum)
        res[i] += shift
        cur_sum += shift
    if cur_sum < n:
        print(-1)
        continue
    print(' '.join([str(x) for x in res]))
        

Bonetrousle problem solution in Java.

import java.io.*;
import java.util.*;
import java.math.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        long[] result = new long[100000];
        for (int t = 0; t < T; t++) {
            long n = in.nextLong();
            long k = in.nextLong();
            int b = in.nextInt();
            long minSum = (long) (1 + b) * b / 2;
            BigInteger maxSum = BigInteger.valueOf(k + k - (b - 1)).multiply(BigInteger.valueOf(b)).divide(BigInteger.valueOf(2));
            if (n < minSum || BigInteger.valueOf(n).compareTo(maxSum) > 0) {
                System.out.println(-1);
            } else {
                long div = (n - minSum) / b;
                int mod = (int) ((n - minSum) % b);
                for (int i = 0; i < b; i++) {
                    result[i] = i + 1 + div;
                }
                
                if (mod != 0) {
                    long next = result[b - 1] + 1;
                    result[b - mod] = next;
                }
                
                StringBuilder out = new StringBuilder();
                for (int i = 0; i < b; i++) {
                    if (i > 0) {
                        out.append(" ");
                    }
                    out.append(result[i]);
                }
                System.out.println(out.toString());
            }
        }
    }
}

Problem solution in C++.

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define all( x ) x.begin(), x.end()
#define umin( x, y ) x = min( x, (y) )
#define umax( x, y ) x = max( x, (y) )

using namespace std;

typedef long long Lint;
typedef double db;
typedef pair<int,int> ii;

const int maxn = 100020;

Lint a, k, ar[maxn];
int b;

void solve() {
    scanf("%lld %lld %d",&a,&k,&b);
    Lint t = 0, beg = 0;
    for(int i=b;i>=1;i--) {
        t += k-i+1;
        if( t >= a ) break;
        if( i == 1 ) {
            printf("-1n");
            return;
        }
    }
    for(int i=b;i>=1;i--) {
        ar[i] = i;
        beg += i;
    }
    if( a < beg ) {
        printf("-1n");
        return;
    }
    Lint h = k;
    for(int i=b;i>=1;i--,h--) {
        if( a - beg > h-ar[i] ) beg += h - ar[i], ar[i] = h;
        else { ar[i] += a - beg; break; }
    }
    for(int i=1;i<b;i++) printf("%lld ",ar[i]); printf("%lldn",ar[b]);
}

int main() {

    int n;
    scanf("%d",&n);
    while( n-- ) solve();
    return 0;
}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int t;
long long int n, k, b;
long long int llt, llt2, llt3;
long long int i;
int has_printed;

scanf("%d", &t);

while (t--) {
scanf("%lld %lld %lld", &n, &k, &b);

llt = (b * (b + 1)) >> 1;
if (llt > n) {
printf("-1n");
continue;
}
llt2 = n - llt;

// divide llt2 with b
if ((llt2 % b) == 0) {
llt3 = llt2 / b;
has_printed = 0;
// if it is within limits, then we are good
if ((b + llt3) <= k) {
for (i = llt3 + 1; i <= llt3 + b; i++) {
if (has_printed) {
printf(" ");
}
printf("%lld", i);
has_printed = 1;
}
printf("n");
} else {
printf("-1n");
}
} else {
llt3 = llt2 / b;
// we are starting at lower end
if ((llt3 + b) >= k) {
printf("-1n");
} else {
llt = llt2 % b;
has_printed = 0;
for (i = llt3 + 1; i <= (llt3 + b - llt); i++) {
if (has_printed) {
printf(" ");
}
printf("%lld", i);
has_printed = 1;
}
for (; i <= (llt3 + b); i++) {
printf(" ");
printf("%lld", i + 1);
}
printf("n");
}
}
}

return 0;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

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