We are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers upto n.For example: If the number n is 24.Then the output should be −const output = [2, 3, 5, 7, 11, 13, 17, 19, 23];Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 24; const isPrime = num => { let count = 2; while(count < (num / 2)+1){ if(num % count !== 0){ count++; continue; }; return false; }; return true; }; const primeUpto = num => { if(num < 2){ return []; }; const res = [2]; for(let i = 3; i
We have an array of arrays of boolean like this −const arr = [[true, false, false], [false, false, false], [false, false, true]];We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator.Let’s write the code for this function. We will be using Array.prototype.reduce() function to achieve this.ExampleThe code for this will be −const arr = [[true, false, false], [false, false, false], [false, false, true]]; const andMerge = (arr = []) => { return arr.reduce((acc, val) => { ... Read More
We have two arrays of numbers like these −const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];We are required to write a JavaScript function that takes in two such arrays and returns the element from arrays that are not common to both.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; const difference = (first, second) => { const res = []; ... Read More
Consider we have an array of Numbers that looks like this −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78];We are required to write a function that counts how many of the elements are in the array below / above a given number.For example, if the number is 60 −The answer should be 5 elements below it (54, 54, 43, 3, 23) and 5 element par it (65, 73, 78, 76, 78) Therefore, let’s write the code for this function −ExampleThe code for this will be −const array = [54, 54, 65, 73, 43, ... Read More
We are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index.For example: If the input array is −const input = [1, 3, 1, 3, 5, 7, 5, 4];OutputThen the output should be −const output = [2, 6, 7, 10, 4];// all the duplicate ones are summed to index 0// all the duplicate threes are summed to index 1 and so on.Therefore, let’s write the code for this function −ExampleThe code for this will be −const input = [1, 3, 1, 3, 5, 7, 5, ... Read More
Suppose we have a singly linked list, and another value k, we have to reverse every k contiguous group of nodes.So, if the input is like List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3, then the output will be [3, 2, 1, 6, 5, 4, 9, 8, 7, 10, ]To solve this, we will follow these steps −tmp := a new node with value 0next of tmp := nodeprev := null, curr := nulllp := temp, lc := currcnt := kwhile curr is not null, doprev := nullwhile cnt > 0 and curr ... Read More
Suppose we have a directed graph, we have to find its reverse so if an edge goes from u to v, it now goes from v to u. Here input will be an adjacency list, and if there are n nodes, the nodes will be (0, 1, ..., n-1).So, if the input is likethen the output will beTo solve this, we will follow these steps −ans := a list of n different lists, where n is number of verticesfor each index i, and adjacent list l in graph, dofor each x in l, doinsert i at the end of ans[x]return ... Read More
Suppose we have a linked list, we have to reverse it. So if the list is like 2 -> 4 -> 6 -> 8, then the new reversed list will be 8 -> 6 -> 4 -> 2.To solve this, we will follow this approach −Define one procedure to perform list reversal in recursive way as solve(head, back)if head is not present, then return headtemp := head.nexthead.next := backback := headif temp is empty, then return headhead := tempreturn solve(head, back)Let us see the following implementation to get better understanding −Exampleclass ListNode: def __init__(self, data, next = None): ... Read More
Suppose we have a string s, we repeatedly delete the first consecutive duplicate characters. We have to find the final string.So, if the input is like s = "xyyyxxz", then the output will be "z", as "yyy" are the first consecutive duplicate characters which will be deleted. So we have "xxxz". Then "xxx" will be deleted to end up with "z".To solve this, we will follow these steps −stack := a new stacki := 0while i < size of s, doif stack is not empty and top of stack is same as s[i], thenx := delete last element from stackwhile ... Read More
Suppose we have a string of parentheses; we have to write a function to compute the minimum number of parentheses to be removed to make the string correct (each open parenthesis is eventually closed).So, if the input is like "(()))(", then the output will be 2, as the correct string is "(())", remove ")(".To solve this, we will follow these steps −total := 0, temp := 0for each p in s, doif p is same as "(", thentotal := total + 1otherwise when p is same as ")" and total is not 0, thentotal := total - 1otherwise, temp := ... Read More