Category Archives: Data Structure

Mountain Array

Problem Given an array of integers, check if it is a mountain array or not. An array is called a mountain array if: Length of array >= 3 There exists some i with 0 < i < (length of array) – 1 such that: arr[0] < arr[1] < … < arr[i – 1] < arr[i] arr[i] > arr[i… Read More »

Count triplets which form an inversion in an array

Problem Given an array, you need to count all inversion triplets. Inversion triplets are such that i<j<k and arr[i]>arr[j]>arr[k], for some indices i, j, and k. Sample Input [1, 2, 4, 3, 2] Sample Output 1   Brute Force Approach You can consider every triplet present in the array and check the condition mentioned in the problem statement.… Read More »

Longest Consecutive Sequence in Linear time

Problem Given an array of integers. Find the length of the longest possible sequence from the array such that the integers of the sequence are consecutive integers. Recall that a “sequence” does not need to contain array elements adjacent to each other. Sample Input [1, 3, 4, 7, 2, 9] Sample Output 4 Explanation Below are the consecutive… Read More »