**Bonetrousle**

### Problem Statement :

Here's a humerus joke: Why did Papyrus the skeleton go to the store by himself? Because he had no body to go with him! Did you like it? Don't worry, I've got a ton more. A skele-ton. Once upon a time, Papyrus the skeleton went to buy some pasta from the store. The store's inventory is bare-bones and they only sell one thing — boxes of uncooked spaghetti! The store always stocks exactly k boxes of pasta, and each box is numbered sequentially from 1 to k. This box number also corresponds to the number of sticks of spaghetti in the box, meaning the first box contains 1 stick, the second box contains 2 sticks, the third box contains 3 sticks, …, and the kth box contains k sticks. Because they only stock one box of each kind, the store has a tendon-cy to sell out of spaghetti. During each trip to the store, Papyrus likes to buy exactly n sticks of spaghetti by purchasing exactly b boxes (no more, no less). Not sure which boxes to purchase, Papyrus calls Sherlock Bones for help but he's also stumped! Do you have the guts to solve this puzzle? 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 sticks of spaghetti by purchasing boxes, print -1 instead. For example, Papyrus wants to purchase n =14 sticks of spaghetti in b = 3 boxes and the store has k=8 different box sizes. He can buy boxes of sizes [8,4,2], [7,5,2], [7,6,1] and other combinations. Any of the combinations will work. Function Description Complete the bonetrousle function in the editor below. It should return an array of integers. bonetrousle has the following parameter(s): n: the integer number of sticks to buy k: the integer number of box sizes the store carries b: the integer number of boxes to buy Input Format The first line contains a single integer t, the number of trips to the store. Each of the next t lines contains three space-separated integers n, k and b, the number of sticks to buy, the number of boxes for sale and the number of boxes to buy on this trip to the store. Constraints 1 <= t <= 20 1 <= b <= 10^5 1 <= n,k <= 10^18 b <= k

### Solution :

` ````
Solution in C :
In C++ :
#include <bits/stdc++.h>
#define FI(i,a,b) for(int i=(a);i<=(b);i++)
#define FD(i,a,b) for(int i=(a);i>=(b);i--)
#define LL long long
#define Ldouble long double
#define PI 3.1415926535897932384626
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define mp make_pair
#define fi first
#define se second
using namespace std;
int t, n;
LL ttl, sto, s[100005];
void solve(){
LL bs = 1LL * n * (n + 1) / 2, tar;
if(ttl < bs){
printf("-1\n");
return;
}
FI(i, 1, n) s[i] = i;
tar = ttl - bs;
FD(i, n, 1){
LL incre = max(0LL, min(tar, sto - (n - i) - s[i]));
s[i] += incre;
tar -= incre;
}
if(tar > 0){
printf("-1\n");
return;
}
FI(i, 1, n) printf("%lld%c", s[i], i == n ? '\n':' ');
return;
}
int main(){
scanf("%d", &t);
while(t--){
scanf("%lld %lld %d", &ttl, &sto, &n);
solve();
}
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int z = 0; z < t; z++) {
long n = sc.nextLong();
long k = sc.nextLong();
int b = sc.nextInt();
long diff = k-b;
long[] selected = new long[b];
long sum = 0;
for (int i = 0; i < b; i++) {
selected[i] = i+1;
sum += i+1;
}
if (sum > n) {
System.out.println(-1);
continue;
}
long next = k+1;
for (int i = b-1; i >= 0; i--) {
sum += diff;
if (sum < n) {
selected[i] = --next;
} else {
sum -= diff;
selected[i] = n-sum+selected[i];
sum = n;
break;
}
}
if (sum < n) {
System.out.println(-1);
continue;
}
StringBuilder print = new StringBuilder();
for (int i = 0; i < b; i++) {
if (i > 0)
print.append(" ");
print.append(selected[i]);
}
System.out.println(print);
}
}
}
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("-1\n");
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("-1\n");
}
} else {
llt3 = llt2 / b;
// we are starting at lower end
if ((llt3 + b) >= k) {
printf("-1\n");
} 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;
}
In Python3 :
def sum(l, r):
return r * (r + 1) // 2 - l * (l - 1) // 2
t = int(input())
while t > 0:
t -= 1
n, k, b = map(int, input().split())
if sum(1, b) > n or sum(k - b + 1, k) < n: print(-1)
else:
a = []
while n > 0:
x = min(k, n - sum(1, b - 1))
a += [x]
k = x - 1
b -= 1
n -= x
print(' '.join(map(str, a)))
```

## View More Similar Problems

## Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →## Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →## Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →