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 :



title-img


                            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

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →