Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 states that we have to get all the possible odd length subarrays in JavaScript programming language. The meaning of odd length is that the length of the subarrays should be 1, 3, 5, 7, and so on. So our task is to filter the subarrays with odd lengths.
Basically the code should generate all the possible subarrays of the input array and then filter out the subarrays with even length. The result will be 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 ? Declare an empty array which will store the subarrays of odd length.
Step 3 ? Iterate over each starting index with a for loop in the input array.
Step 4 ? Start another for loop to iterate over every ending index from the current starting index till the end of the array.
Step 5 ? Initialize an empty array to store the current subarray.
Step 6 ? Iterate from the starting index to the ending index and add each element to the current subarray.
Step 7 ? Check if the length of the current subarray is odd.
Step 8 ? If the length is odd, add the subarray to the list of odd length subarrays.
Step 9 ? Finally, print all the odd length subarrays.
Implementation
const arr = [10, 20, 30, 40, 50];
// to store the odd-length subarrays
const oddSubarrays = [];
// iterate each starting index in the input array
for(let i = 0; i < arr.length; i++) {
// iterate each ending index from current starting index
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]);
}
// check if subarray length is odd
if(subarr.length % 2 !== 0) {
oddSubarrays.push(subarr);
}
}
}
console.log("All odd length subarrays:");
console.log(oddSubarrays);
All odd length subarrays: [ [ 10 ], [ 10, 20, 30 ], [ 10, 20, 30, 40, 50 ], [ 20 ], [ 20, 30, 40 ], [ 30 ], [ 30, 40, 50 ], [ 40 ], [ 50 ] ]
Optimized Approach Using Array.slice()
We can simplify the code by using JavaScript's built-in slice() method:
const arr = [1, 2, 3, 4];
const oddSubarrays = [];
for(let i = 0; i < arr.length; i++) {
for(let j = i; j < arr.length; j++) {
const subarr = arr.slice(i, j + 1);
if(subarr.length % 2 !== 0) {
oddSubarrays.push(subarr);
}
}
}
console.log("Odd length subarrays using slice():");
console.log(oddSubarrays);
Odd length subarrays using slice(): [ [ 1 ], [ 1, 2, 3 ], [ 2 ], [ 3 ], [ 3, 4 ], [ 4 ] ]
Complexity Analysis
Let's suppose n is the length of the input array:
- Time Complexity: O(n³) - We have three nested operations: two for loops and the subarray creation/copying.
- Space Complexity: O(n³) - The oddSubarrays array stores all possible odd-length subarrays.
Practical Example
function getAllOddLengthSubarrays(nums) {
const result = [];
for(let start = 0; start < nums.length; start++) {
for(let end = start; end < nums.length; end++) {
const subarray = nums.slice(start, end + 1);
if(subarray.length % 2 === 1) {
result.push(subarray);
}
}
}
return result;
}
// Test with different arrays
console.log("Array [1, 2]:");
console.log(getAllOddLengthSubarrays([1, 2]));
console.log("\nArray [1, 2, 3, 4, 5]:");
console.log(getAllOddLengthSubarrays([1, 2, 3, 4, 5]));
Array [1, 2]: [ [ 1 ], [ 2 ] ] Array [1, 2, 3, 4, 5]: [ [ 1 ], [ 1, 2, 3 ], [ 1, 2, 3, 4, 5 ], [ 2 ], [ 2, 3, 4 ], [ 3 ], [ 3, 4, 5 ], [ 4 ], [ 5 ] ]
Conclusion
We successfully implemented finding all odd length subarrays using nested loops in JavaScript. While this approach has O(n³) complexity, it provides a clear understanding of subarray generation and filtering based on length criteria.
