Broad Network


TieRopes at app.codility.com/programmers in JavaScript Explained: Tie adjacent ropes to achieve the maximum number of ropes of length >= K.

Task score : 100% - Correctness : 100% ; Performance : 100%
Detected time complexity : O(N)
Lesson 16 at app.codility.com/programmers
The solution and its explanation are given below.

Category of Article : Technology | Computers and Software | Algorithm

By: Chrysanthus Date Published: 3 Oct 2025

Problem

There are N ropes numbered from 0 to N - 1, whose lengths are given in an array A, lying on the floor in a line. For each I (0 ≤ I < N), the length of rope I on the line is A[I]. 

We say that two ropes I and I + 1 are adjacent. Two adjacent ropes can be tied together with a knot, and the length of the tied rope is the sum of lengths of both ropes. The resulting new rope can then be tied again.

For a given integer K, the goal is to tie the ropes in such a way that the number of ropes whose length is greater than or equal to K is maximal.

For example, consider K = 4 and array A such that: 

    A[0] = 1

    A[1] = 2

    A[2] = 3

    A[3] = 4

    A[4] = 1

    A[5] = 1

    A[6] = 3

The ropes are shown in the figure below. 

 

We can tie:

 

·         rope 1 with rope 2 to produce a rope of length A[1] + A[2] = 5;

·         rope 4 with rope 5 with rope 6 to produce a rope of length A[4] + A[5] + A[6] = 5.

After that, there will be three ropes whose lengths are greater than or equal to K = 4. It is not possible to produce four such ropes. 

Write a function:

    function solution(K, A);

that, given an integer K and a non-empty array A of N integers, returns the maximum number of ropes of length greater than or equal to K that can be created.

For example, given K = 4 and array A such that: 

    A[0] = 1

    A[1] = 2

    A[2] = 3

    A[3] = 4

    A[4] = 1

    A[5] = 1

    A[6] = 3

the function should return 3, as explained above. 

Write an efficient algorithm for the following assumptions:

 

·         N is an integer within the range [1..100,000];

·         K is an integer within the range [1..1,000,000,000];

·         each element of array A is an integer within the range [1..1,000,000,000].

Note

If all the ropes above are tied, then the sum will be greater than or equal to K=4. The sum will be A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6] = 1 + 2 + 3 + 4 + 1 + 1 + 3 = 15. The count here is 1 for one long rope. 

If the first 4 ropes are tied together and the last 3 ropes are also tied together, making two groups of ropes, then the sum of each group will be greater than or equal to 4. The sums are A[0] + A[1] + A[2] + A[3] = 1 + 2 + 3 + 4 = 10 and A[4] + A[5] + A[6] = 1 + 1 + 3 = 5. The count here is 2 for 2 resulting ropes.

If rope 0, rope 1 with rope 2 are tied together, and rope 3 stays alone, and rope 4, rope 5 with rope 6 are tied together, making three groups of ropes, then the sum of each group will be greater than or equal to 4. The sums are A[0] + A[1] + A[2] = 1 + 2 + 3 = 6 and A[3] = 4 and A[4] + A[5] + A[6] = 1 + 1 + 3 = 5. The count here is 3 for 3 resulting ropes. Note that rope 0 (A[0]) was note taken into consideration in the illustration, in the problem description. 

The maximum possible count is what is required.

Strategy

The lesson for this task (problem) is on greedy algorithm, so the candidate should expect that greedy algorithm is needed in the solution. 

Begin from the first element and be adding the elements, as the given list is scanned. If the resulting sum is greater than or equal to K, the count is increased by 1, and then a new adding sequence starts. An element whose length is equal to or greater than K, will still fit into the scheme. This is a good example of greedy algorithm.

Smart Solution

The program is (read the code and comments): 

<script type='text/javascript'>
    "use strict";

function solution(K, A) {
    let N = A.length;
    let count = 0;
    let length = 0;
    
    for(let i=0;i<N;i++){
        length = length + A[i];
        if (length >= K){    
            count++;
            length = 0;    //resetting length
        }
    }

    return count;
}

    let A = [1, 2, 3, 4, 1, 1, 3];
    let ret = solution(4, A);
    document.write(ret + '<br>');
    
</script>

The output is 3. 

At app.codility.com/programmers the scoring and detected time complexity for this solution() is:

    Task score : 100% -  Correctness : 100% ; Performance : 100%

    Detected time complexity : O(N) 

With this solution() function code, all elements were taken into consideration.

Thanks for reading.

The rest of the 17 lessons, with explanation of lessons, explanation of tasks, and explanation of solutions, can be found at (click): Explanations in JavaScript for the 17 Algorithm Lessons at https://app.codility.com/programmers with Problems and Solutions





Related Links

More Related Links

Cousins

BACK

Comments