Split a range of number to a specific number of intervals JavaScript


In this problem statement, our task is to write the function splitting a range of numbers to a specific number of intervals with the help of Javascript. So for doing this task we will have to give start, end and the intervals.

Understanding the problem statement

The problem statement is saying to create a function which can split a range of numbers into a specific number of intervals. And the inputs will be starting and ending numbers of the range and the desired number of intervals. So the outcome should be the array of subarrays which shows each interval. These subarrays will contain the start and end numbers of that interval.

For example - if the range is from o to 100 and we have to split the range into 5 intervals so the function should return an array of 5 subarrays. So every subarray should represent an interval of size 20. And the first subarray will contain the start and end numbers of the first interval, the second subarray will contain the start and end numbers of the second interval and so on till the last subarray contain the start and end numbers of the final interval.

Logic for the above problem

We will develop a function to solve the given problem. To begin, we will divide the range size by the number of intervals to determine the size of every interval. And then we will create a new blank array to keep the intervals. With the help of a loop we will iterate over the number of intervals and determine the starting and ending points for every interval. Finally we will return the interval array which includes the split range.

Algorithm

Step 1 − Create a function splitRange which takes three parameters: rangeStart, is the starting number of the range, rangeEnd, is the ending number of the range and numIntervals, is the number of intervals that the range will be split into.

Step 2 − Inside the above method we will divide the range size with the number of intervals to calculate the size of every interval.

Step 3 − As we have to work with the intervals so to store them we need to create a blank array.

Step 4 − The function will work on the beginning and ending numbers for intervals in the for loop.

Step 5 − After getting the intervals we will push them onto the intervals array and they will look like a subarray. So these subarrays will have beginning and ending numbers.

Step 6 − At the end we will see an array of subarray containing the split range.

Code for the algorithm

//function to split the range in array
function splitRange(rangeStart, rangeEnd, numIntervals) {
    const intervalSize = (rangeEnd - rangeStart) / numIntervals;
    const intervals = [];

    for (let i = 0; i < numIntervals; i++) {
        const start = rangeStart + i * intervalSize;
        const end = start + intervalSize;
        intervals.push([start, end]);
    }
    return intervals;
}
const rangeStart = 0;
const rangeEnd = 100;
const numIntervals = 5;
const intervals = splitRange(rangeStart, rangeEnd, numIntervals);
console.log(intervals);

Complexity

The time complexity algorithm is O(n) in which n is the number of intervals. The reason behind this complexity is the loop we have defined in the function integrates over the number of intervals to compute the start and end numbers for every interval. The space complexity for the function is also O(n) here n is the number of intervals. Because the function is creating an array to hold the intervals so it will contain n subarrays representing every interval. And the size of the array is proportional to the number of intervals so the space complexity is O(n).

Conclusion

The above code provides a simple and efficient solution to split a range of the number to a specific number of intervals in Javascript. And it has an O(n) time complexity and O(n) space complexity.

Updated on: 18-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements