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

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 →

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →

Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →

Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

View Solution →

Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

View Solution →

Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a

View Solution →