The longestConsecutive function will return the length of the longest sequence if consecutive numbers in the input array

Finding the Longest Consecutive Sequence in an Array

Problem Statement We have to implement the longestConsecutive function that takes an integer array as input and returns the length of the longest sequence of consecutive integers. For example, in array [4, 2, 7, 8, 1, 5, 6, 0] we have two sequences of consecutive integers: [0, 1, 2] and [4, 5, 6, 7, 8]. The longest sequence ([4, 5, 6, 7, 8]) has length 5. ...

November 9, 2023 · 6 min · Avnish
The isValidSudoku function will return true if the input matrix represents a valid sudoku grid and false otherwise

Checking the Validity of a Sudoku Grid

Problem Statement We have to implement an isValidSudoku function that takes a 9x9 matrix (representing a Sudoku grid) as input and returns true if the grid is valid and false otherwise. Sudoku Grid A Sudoku grid is valid if Every row contains digits from 1 to 9 without repetition. Rows in a Sudoku Grid Every column contains digits from 1 to 9 without repetition. ...

November 2, 2023 · 12 min · Avnish
The productExceptSelf function will returns an array of products of all the elements without the element at the same index in input array

Building a Product Array without the Element Itself

Problem Statement We have to implement a productExceptSelf function that takes an integer array nums as input and returns another array answer as output. The element answer[i] will be the product of all elements in inputArray except inputArray[i]. The implementation should not contain a division (/) operation because the answer array could also be created by iterating over all elements in inputArray and dividing them from the product of the complete array. ...

October 28, 2023 · 5 min · Avnish
The topkFrequent function will return the top k most frequent elements in an array

Finding Most Frequent Elements in an Array

Problem Statement We have to implement a topKFrequent function that takes an integer array and a value k as input and returns the k most frequent elements in the input array. For example, topKFrequent({1, 1, 2, 2, 3, 5, 2}, 2) will return the top 2 most frequent elements in the array i.e. {2, 1}. If multiple elements have the same frequency, then any of them could be returned in the solution as long as its length is k. ...

October 26, 2023 · 7 min · Avnish
The twoSums function will return the indices of two elements in the input array that sum up to the target value

Finding Elements in an Array that Sum Up to a Target Value

Problem Statement We have to implement a twoSums() function that takes an integer array and a target value as inputs. It returns the indexes of two elements in the input array which could be summed up to the target value. It is assumed that the input array contains only one valid pair of elements that sum up to the target value. Brute Force Solution The brute-force solution will find the sum of all element pairs in the array using nested loops. ...

October 15, 2023 · 4 min · Avnish
The containsDuplicate function takes an array as input and returns true or false depending on the fact that array contains duplicate elements or not

Checking an Array for Duplicate Values

Problem Statement We have to implement a function containsDuplicate() that takes an integer array as input and returns true if an element occurs more than once and false otherwise. Brute Force Solution The simplest solution for this problem would be two nested loops, where the first loop will select an element and the second loop will select another element from the array and compare them. On the first occurrence of a duplicate element, the function will exit while returning true. ...

October 10, 2023 · 4 min · Avnish
Arrays, Strings, and HashMaps

Arrays, Strings, and HashMaps

Data structures like arrays, strings, and hashmaps are available by default in most programming languages. They facilitate storage of large amounts of data in an efficient format which makes it easier (and sometimes relatively faster) to access during runtime. Arrays An array is a sequential store of data (referred to as elements). In languages like Python, an array can store elements with multiple data types, like, [1, "Hello, World", True] but for languages like Go, C++, and Java an array can store elements of a singular data type. ...

September 29, 2023 · 9 min · Avnish