All possible odd length subarrays JavaScript


In this problem statement, our task is to find all the possible odd length subarrays with the help of Javascript functionalities. This task can be done with the help of some built in functions of Javascript or we can solve it by multiple for loops.

Logic for the given problem

The problem stated that we have to get all the possible odd length of the subarrays in Javascript programming language. The meaning of add length is that the length of the subarrays should be 1, 3, 5, 7,.....end so on. So our task is to filter the length with the odd lengths of subarrays.

Basically the code should generate all the possible subarrays of the input array and then filter out the subarrays with the even length. And add all the remaining subarrays (with odd lengths) to a new array. So the result will be the all possible odd length subarrays of the input array.

Algorithm

Step 1 − Start the program by defining the array of integer numbers to which we have to find out the odd subarrays.

Step 2 − After that we will declare an empty array which will store the subarrays of odd length.

Step 3 − Now we will iterate over each index with a for loop in the input array with the length of the array.

Step 4 − At this step we will start another for loop to iterate over every index from the current index of the array till the length of the array.

Step 5 − After all the above steps, we will initialize an empty or blank array to memorize the current subarray.

Step 6 − At this step we will iterate over all the index from the current index to the end of the index. And we will add the current element to the subarray.

Step 7 − After adding the current element to the subarray, we will check that the length of the subarray is odd or not.

Step 8 − Now we will check that the length of the subarray is odd so add the subarray to the list of odd length subarrays.

Step 9 − At the end we have all the odd length of subarrays and print them to show the output.

Code for the algorithm

const arr = [10, 20, 30, 40, 50];
// to store the odd-length subarrays
const oddSubarrays = [];

// iterate each index in the input array
for(let i = 0; i < arr.length; i++) {
   for(let j = i; j < arr.length; j++) {
      const subarr = []; 
      // to store the current subarray
      for(let k = i; k <= j; k++) {
         subarr.push(arr[k]);
      }
      if(subarr.length % 2 !== 0) {
         oddSubarrays.push(subarr);
      }
   }
}
 // list of odd length subarrays
console.log(oddSubarrays);

Complexity

Let's suppose n is the length of the input array so the time complexity for the above algorithm is O(n^3). Because we have used three for nested loops to iterate and get the desired outcome as per our problem statement. And the space complexity of the code is also O(n^3) because the oddSubarrays array holds all the possible subarrays of the input array.

Conclusion

As per the problem given we have fairly implemented the odd length subarrays with the help of three nested for loops in Javascript. Our main task in this algorithm was to filter out the even length subarrays and keep odd length subarrays. But this is the straightforward approach to implement these kinds of problems in Javascript. And this algorithm has high time and space complexity which can not be considered for larger arrays.

Updated on: 18-May-2023

296 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements