ShareChat Coding Question – Solved

5 Live
Break The Bricks There are n bricks arranged in a row at positions numbered from 1 through n, inclusive. There is an array, newtons[n], that contains an integer indicating the number of newtons required to smash a brick., (A newton is a unit of force.) There are two hammers, one big and one small. The big hammer can smash any brick with one blow. The small hammer reduces the newtons required by 1 for each blow to a brick. For example, a brick requires 3 newtons of force. It will take 1 blow with the big hammer, or 3 blows with the small hammer to smash it. There is a limit to how many times the big hammer can be used. Determine 3 values: 1. the minimum number of blows to smash all the bricks 2. the 1-based indices of the bricks smashed by the big hammer, sorted ascending 3. the 1-based indices of the bricks smashed by the small hammer, sorted ascending Return the values as a 2-dimensional integer array, [[total hits], [big hammer hits], [small hammer hits]]. If a hammer is not used, its index array should be [-1]. Example bigHits = 0 newtons = [2] The big hammer cannot be used. The small hammer takes 2 blows to smash the single brick at index 1. The return array is [[2], [-1], [1]]. bigHits = 4 newtons = [3, 2, 5, 4, 6, 7, 9] In this case, it is best to use the big hammer on bricks at sorted indices [3, 5, 6, 7], using 4 hits to smash them all. The small hammer is used on sorted indices [1, 2, 4] which have newtons of 3, 2, and 4. It takes a total of 3 + 2 + 4 = 9 hits with the small hammer. The total blows required = 4 + 9 = 13. The return array is [[13], [3, 5, 6, 7], [1, 2, Function Description Complete the function breakTheBricks in the editor below. breakTheBricks has the following parameters: int bigHits: the maximum blows with the big hammer int newtons[n]: an array of distinct integers representing newtons required to smash each brick

Asked in: ShareChat

Image of the Question

Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| Given an n x m grid, where rows are numbered from 7 to n and columns from 1 to … |
| There are 'N' coders standing in a line, where i denotes the ith position of a … |
| A birthday party was attended by N number of kids, and each kid was given a uni… |
| Given a matrix of size m * n, where m denotes the number of rows (starting with… |
| A traveler is traveling from the city of Zeta to Omega. He starts with X amount… |
| As an operations engineer at Amazon, you are responsible for organizing the dis… |