# Category Archives: Array

## 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 »

## Find triplet having maximum product in an array

Problem Find 3 numbers from the array such that their product is maximum. Sample Input [1,2,2,2] Sample Output 8 Approach We can sort the given array and end up on 2 cases: Case 1: The maximum product is obtained by the last 3 numbers. Case 2: The maximum product is obtained by the first 2 numbers and the… Read More »

## Find the minimum index of a repeating element in an array

Problem Given an array of integers, find the first repeating element in it. We need to find the element that occurs more than once and whose index of first occurrence is smallest.  Sample Input [1, 2, 2, 1] Sample Output  1 Explanation 1 is the first one occurring twice. Approach We can use sets to solve this efficiently.… Read More »

## Find a pair with a minimum absolute difference in an array

Problem Given an unsorted array, find the minimum difference between any pair in a given array and print the array. Sample Input [1, 2, 2, 1] Sample Output 1 1 Approach Sort array in ascending order. Initialize the difference as INT_MAX. Compare all adjacent pairs in a sorted array and keep track of the minimum difference and update… Read More »

## Find two non-overlapping pairs having the same sum in an array

Problem Given an unsorted array of integers. The task is to find any two non-overlapping pairs whose sum is equal. Two pairs are said to be non-overlapping if all the elements of the pairs are at different indices. Sample Input [1, 7, 9, 3] Sample Output 1 9 7 3 Explanation They both have sum as 10 Approach… Read More »

## Difference Between ArrayList and LinkedList

The list is an abstract data type available in functional programming languages that stores ordered values. It is analogous to arrays in object-oriented programming languages. Lists allow us to store, manipulate, and retrieve data easily. Programming languages that support list data types have their ways of creating lists. We can create a list by inserting data items sequentially… Read More »

## Shuffle an array according to the given order of elements

Problem Given two integer arrays of the same size, arr[] and index[], reorder elements in arr[] according to the given index array. Sample Input [10, 11, 12] [1, 0, 2] Sample Output 11, 10, 12 0, 1, 2 Approach We can traverse through 0 to n-1 (where n is the size of the arrays) and keep placing the… Read More »

## Count the number of strictly increasing subarrays in an array

Problem Given an array, count the total number of strictly increasing subarrays in it. Sample Input [1, 2, 3, 1] Sample Output 3 Explanation Let’s say the endpoints of a strictly increasing subarray are start and end. Then, the subarray arr[start, end+1] will be strictly increasing if the element at end+1 is greater than the element at end.… Read More »

## Find minimum sum of a subarray of size k

Problem Given an array of integers and a number k, find the minimum sum of a subarray of size k.  Brute Force approach A simple solution is to construct all subarrays of size k, compute their sums, and ultimately return the sum with the lowest value. This solution has a time complexity of O(N*k). Let’s look at one… Read More »

## Subarray with Sum k

Problem Given an unsorted array arr of size N, find a contiguous subarray that adds to a given number k. Brute Force Approach An easy approach is to go through all subarrays one by one and calculate the total of each subarray. The programme that follows implements the basic solution. Run two loops: the outer loop selects a… Read More »