Suppose we have an integer k and also have a tree with n nodes, we have to count the number of distinct pairs of vertices which have a exact k distance.So, if the input is like k = 2then the output will be 4To solve this, we will follow these steps −N := 5005graph := adjacency list of size Nvertex_count := a 2d matrix of size 505 x 5005res := 0Define a function insert_edge() . This will take x, yinsert y at the end of graph[x]insert x at the end of graph[y]Define a function dfs() . This will take v, ... Read More
Suppose we have a binary matrix. We have to count the number of islands in it. An island is place that is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. We can assume that all four edges of the grid are all surrounded by water.Suppose the grid is like −11000110000010000011There are three islands.To solve this, we will follow these steps −There will be two methods, one will be used to count number of islands called numIslands() and makeWater(). The makeWater() will be like −if number of rows in the grid is 0, then return 0n ... Read More
Suppose we have an array with n numbers, we have to return the number of consecutive zero’s at the end after multiplying all the n numbers.So, if the input is like [200, 20, 5, 30, 40, 14], then the output will be 6 as 200 * 20 * 5 * 30 * 40 * 14 = 336000000, there are six 0s at the end.To solve this, we will follow these steps −Define a function count_fact_two() . This will take ncount := 0while n mod 2 is 0, docount := count + 1n := n / 2 (only the quotient as ... Read More
Suppose we have one equation in this form: a + b = c, now any one of the terms of a, b or c is missing. We have to find the missing one.So, if the input is like ? + 4 = 9, then the output will be 5To solve this, we will follow these steps −delete all blank spaces from the string and change (+ and = to comma ', ')elements := a list of elements by splitting the string separated by commaidx := 0for i in range 0 to size of elements, doif elements[i] is not numeric, thenidx ... Read More
Suppose there are n number of cashiers exchanging the money, at the moment, the i-th cashier had ki number of people in front of him/her. Now, the j-th person in the line to i-th cashier had m[i, j] notes. We have to find how early can one exchange his/her notes. We have to keep in mind that the cashier spent 5 seconds to scan a single note.After completing scanning of every note for the customer, he/she took 15 seconds to exchange the notes.So, if the input is like Input : n = 6, k = [12, 12, 12, 12, 12, ... Read More
Let’s say, we have an array of objects like this −const arr = [ { id: 1, parent_id: 0, title: 'Movies' }, { id: 2, parent_id: 0, title: 'Music' }, { id: 3, parent_id: 1, title: 'Russian movies' }, { id: 4, parent_id: 2, title: 'Russian music' }, { id: 5, parent_id: 3, title: 'New' }, { id: 6, parent_id: 3, title: 'Top10' }, { id: 7, parent_id: 4, title: 'New' }, { id: 8, parent_id: 4, title: 'Top10' }, { id: 9, parent_id: 0, title: 'Soft' } ];We are required ... Read More
We have an array of strings each of which contain one or more numbers like this −const arr = ['di5aster', 'ca1amity', 'cod3', 'ho2me3', 'ca11ing'];We are required to write a sorting function that sorts this array in ascending order of the numbers present in the strings. The correct order will be −const output = [ 'ca1amity', 'cod3', 'di5aster', 'ca11ing', 'ho2me3' ];Therefore, let’s write the code for this problem −Exampleconst arr = ['di5aster', 'ca1amity', 'cod3', 'ho2me3', 'ca11ing']; const filterNumber = str => { return +str .split("") .filter(el => el.charCodeAt() >= 48 && el.charCodeAt() { return filterNumber(a) - ... Read More
Let’s say, we are required to write a function that takes in an array of Numbers and number n, where n >= any number of the array. The function is required to break the array into subarrays if the sum of consecutive elements of the array exceeds the number n.For example −// if the original array is: const arr = [2, 1, 2, 1, 1, 1, 1, 1]; // and the number n is 4 // then the output array should be: const output = [ [ 2, 1 ], [ 2, 1, 1 ], [ 1, 1, 1 ] ... Read More
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end.For example − If we find 4 duplicate values we have to remove then all and insert four empty strings at the end.Therefore, let’s write the code for this problem −Exampleconst arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => { const creds = arr.reduce((acc, val, ind, array) => { let { count, res } ... Read More
We have to write a simple function in JavaScript that takes in an array of Numbers (nested to any level) and return the greatest number present in the array.For example − If the input array is −const arr = [ 34, 65, 67, [ 43, 76, 87, 23, 56, 7, [ 54, 7, 87, 23, 79, 314, 2 ], 54 ], 54, 4, 2 ];Then the output should be −314We will use recursion to find the greatest number in the array. ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP