Category: Array

Word break problem dynamic programming

Problem : Given a string and an array of words, find out if the input string can be bro­ken into a space-separated sequence of one or more words. For example, inputDict = [“I” ,...

Pascals triangle javascript code

Problem : Create a pascal’s triangle using javascript. More details about Pascal’s triangle pattern can be found here. Input: #Rows = 6 Output: Logic : Pascal’s triangle can be simulated using 2-D array While...

Flatten nested array javascript

Problem : Given a nested array, flatten the given array. Input : [1, 2, 3, [4, 5], [6, [7, 8]]] Output : [1, 2, 3, 4, 5, 6, 7, 8] Logic : Iterate throgh...

Generate all permutations of a given array using backtracking

A permutation is a rearrangement of the elements in a list. A string/array of length n has n! permutation. Input: An array // [‘A’, ‘B’, ‘C’] Output: [‘A’, ‘B’, ‘C’] [‘A’, ‘C’, ‘B’], [‘B’,...

Find a pair of elements from an array whose sum equals a given number

Input: An array of n integers and given a number X. Expected output: All the unique pairs of elements (a, b), whose summation is equal to X. Example <pre>Given<span class=”pl-k”>var</span> unSortedArr <span class=”pl-k”>=</span> [<span...

Generate all combinations of an array

Input : [ 1, 2, 3 ]   Output : [  ‘ ‘,  1,  2,  12,  3,  13,  23,  123  ]   Logic : There are 2^n possible combinations for the array of...

Find consecutive segments in an sorted array

Problem : Number list compressing. Given an sorted array. Input: sorted number list [ 1, 2, 3,10, 25, 26, 30, 31, 32, 33] Output: find consecutive segments print: 1-3, 10, 25-26, 30-33 Logic :...

Check if an element exists in an array, based on that update an array

Problem description : Write a function which detects if the given element exists in an array or not. If it does not exist then adds it to an array If it does exist then...

Convert the given array of elements to a string

Problem description : Write a function that converts a given array into a string. Input : An Array Output : A String Logic : Iterate through array concatenate the new element into the string...

Remove duplicates from the given array

Problem description : Write a function that removes duplicates entries from a given array. Input : An Array // [‘Heart5’, ‘Diamond6’, ‘Club7’, ‘Spade8’, ‘Diamond6’, ‘Club7’] Output : An Array // [‘Heart5’, ‘Diamond6’, ‘Club7’, ‘Spade8’]...