When it is required to find the maximum sub array using Kadane’s algorithm, a method is defined that helps find the maximum of the sub array. Iterators are used to keep track of the maximum sub array.Below is the demonstration of the same −Example Live Demodef find_max_sub_array(my_list, beg, end): max_end_at_i = max_seen_till_now = my_list[beg] max_left_at_i = max_left_till_now = beg max_right_till_now = beg + 1 for i in range(beg + 1, end): if max_end_at_i > 0: max_end_at_i += my_list[i] else: max_end_at_i = my_list[i] ... Read More
ProblemWe are required to write a JavaScript function that takes in an array of literals that might contain some 0s. Our function should tweak the array such that all the zeroes are pushed to the end and all non-zero elements hold their relative positions.ExampleFollowing is the code − Live Democonst arr = [5, 0, 1, 0, -3, 0, 4, 6]; const moveAllZero = (arr = []) => { const res = []; let currIndex = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(el === 0){ res.push(0); }else{ res.splice(currIndex, undefined, el); currIndex++; }; }; return res; }; console.log(moveAllZero(arr));OutputFollowing is the console output −[ 5, 1, -3, 4, 6, 0, 0, 0 ]
ProblemWe are required to write a JavaScript function that takes in an array of integers arr. Our function should return the string ‘odd’ if the sum of all the elements of the array is odd or ‘even’ if it’s even.ExampleFollowing is the code − Live Democonst arr = [5, 1, 8, 4, 6, 9]; const assignSum = (arr = []) => { const sum = arr.reduce((acc, val) => { return acc + val; }, 0); const isSumEven = sum % 2 === 0; return isSumEven ? 'even' : 'odd'; }; console.log(assignSum(arr));OutputFollowing is the console output −odd
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should construct and return an array of N * N order (2-D array), in which the 1s take all the spiralling positions starting from [0, 0] and all the 0s take non-spiralling positions.Therefore, for n = 5, the output will look like −[ [ 1, 1, 1, 1, 1 ], [ 0, 0, 0, 0, 1 ], [ 1, 1, 1, 0, 1 ], [ 1, 0, 0, 0, 1 ], [ 1, 1, 1, 1, 1 ] ]ExampleFollowing ... Read More
ProblemWe are required to write a JavaScript function that lives on the prototype object of the string class.This function should simply change case of all the alphabets present in the string to uppercase and return the new string.ExampleFollowing is the code − Live Democonst str = 'This is a lowercase String'; String.prototype.customToUpperCase = function(){ const legend = 'abcdefghijklmnopqrstuvwxyz'; const UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; let res = ''; for(let i = 0; i < this.length; i++){ const el = this[i]; const index = legend.indexOf(el); if(index !== -1){ ... Read More
ProblemSuppose the following number name string −const str = 'TOWNE';If we rearrange this string, we can find two number names in it 2 (TWO) and 1 (ONE).Therefore, we expect an output of 21We are required to write a JavaScript function that takes in one such string and returns the numbers present in the string.ExampleFollowing is the code − Live Democonst str = 'TOWNE'; const findNumber = (str = '') => { function stringPermutations(str) { const res = []; if (str.length == 1) return [str]; if (str.length == 2) return [str, str[1]+str[0]]; ... Read More
ProblemWe are required to write a JavaScript function that takes in an array of numbers (positive and negative). Our function should calculate and return the sum of all the positive numbers present in the array.ExampleFollowing is the code − Live Democonst arr = [5, -5, -3, -5, -7, -8, 1, 9]; const sumPositives = (arr = []) => { const isPositive = num => typeof num === 'number' && num > 0; const res = arr.reduce((acc, val) => { if(isPositive(val)){ acc += val; }; return acc; }, 0); return res; }; console.log(sumPositives(arr));OutputFollowing is the console output −15
ProblemWe are required to write a JavaScript function that takes in an array of integers. Our function should return the sum of all the integers that have an even index, multiplied by the integer at the last index.const arr = [4, 1, 6, 8, 3, 9];Expected output −const output = 117;ExampleFollowing is the code − Live Democonst arr = [4, 1, 6, 8, 3, 9]; const evenLast = (arr = []) => { if (arr.length === 0) { return 0 } else { const sub = arr.filter((_, index) => index%2===0) const sum = sub.reduce((a,b) => a+b) const posEl = arr[arr.length -1] const res = sum*posEl return res } } console.log(evenLast(arr));OutputFollowing is the console output −117
When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Demofrom collections import deque def add_edge(adj: list, u, v): adj[u].append(v) adj[v].append(u) def detect_cycle(adj: list, s, V, visited: list): parent = [-1] * ... Read More
When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Democlass Graph_structure: def __init__(self, V): self.V = V self.adj = [[] for i in range(V)] def DFS_Utility(self, temp, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP