DEV Community

Ajay Kumar Verma
Ajay Kumar Verma

Posted on • Originally published at weekendtutorial.com

1

How to generate all subarrays in javascript in 2022?

How to generate all subarrays in javascript in 2022?

Hello Readers,

Interviewers like to ask questions about the subarrays as it gives a clear indication of the logical thinking and aptitude of a candidate.

Considering the above, we are going to start a new tag MCAIQ (most commonly asked interview questions) on weekendtutorial that will contain the most frequently asked questions in the interview. This will be your single stop for all such questions and interview preparations.

we will see how to generate all the subarrays of an array in javascript using iterative and recursive ways both.


Generate all subarrays using iteration

Problem Statement

Given an array, generate all the possible subarrays of the given array using an iterative way.

Input : [1, 2, 3]

Output: [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]

Input: [1, 2]

Output: [1], [1, 2], [2]
Enter fullscreen mode Exit fullscreen mode

Implementation

function generateAllSubArrays(arr) {
const n = arr.length;
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
// we have index i and j of the subarray, let's print it
let ans = [];
for (let k = i; k <= j; k++) {
ans.push(arr[k]);
}
console.log(ans);
}
}
}
console.log('all subarrays for [1,2,3]');
generateAllSubArrays([1,2,3]);
console.log('all subarrays for [1,2]');
generateAllSubArrays([1,2]);
view raw iterative.js hosted with ❤ by GitHub

Generate all subarrays using recursion

Implementation

function generateAllSubArrays(arr, n, start, end) {
if (start >= n && end >= n) return;
if (end >= n) {
generateAllSubArrays(arr, n, start+1, start+1);
} else {
let ans = [];
for (let k = start; k <= end; k++) {
ans.push(arr[k]);
};
console.log(ans);
generateAllSubArrays(arr, n, start, end+1);
}
}
console.log('all subarrays for [1,2,3]');
generateAllSubArrays([1,2,3], 3, 0, 0);
console.log('all subarrays for [1,2]');
generateAllSubArrays([1,2], 2, 0, 0);
view raw recursion.js hosted with ❤ by GitHub

I hope you have learned something if you like the article please share it with friends and help weekendtutorial to grow. This site is run by ads and it is required for it to be alive and grow.

Read the following articles –

  1. Learn Nginx in-depth
  2. Implement binary tree using javascript

This is all for today. Please visit our other articles, and keep learning.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay