Pair With Given Sum in Unsorted Array

In this question, we have to find the pair with given sum in unsorted array. We have been given an unsorted array along with a number say k. You’ll have to look if there is a pair in the given array whose sum is equal to k. Brute Force Approach to Find Pair With Given … Read more

Insertion Sort – A Stable Sorting Algorithm

Have you ever played card? If yes, then you’ll feel that insertion sort is somewhat similar to arranging cards in your hand. While arranging cards you’ll place the first card in your hand. After picking the second card you’ll compare it with the existing card and place it in the correct order. In the same … Read more

Bubble Sort – A Stable Algorithm

Bubble sort is one of the simplest sorting algorithms that is based on swapping adjacent elements if they are present in the wrong order. It is a stable algorithm since it preserves the order of the original vector in case of repetition. Bubble Sort C++ Algorithm Implementation Dry Run Input: nums = [5, 2, 10, … Read more

Median of Two Sorted Arrays | Leetcode Problem #4

In this question, given two sorted arrays nums1 and nums2 of size m and n respectively, you have to return the median of two sorted arrays Goal: The overall run time complexity should be O(log(m+n)) Naive Solution to Find Median of Two Sorted Arrays In this solution, we create a vector and then insert all … Read more

Find Peak Element in Array | Leetcode Problem #162

A peak element is defined as an element that is strictly greater than its neighbor. In this question, you’ll be provided with an array in which you have to find peak element and return its index. In case the array contains multiple peak elements, return the index of any one of the peak elements. You … Read more

Program to Count Occurrence in a Sorted Array (From Naive to an Efficient Approach)

In this question, you’ll have to find the frequency of a given number(k) in a sorted array (nums[ ]). Below we have provided some of the examples so that you can have a better understanding of how to count occurrence in a sorted array. Naive Solution(Using Linear Search) In this approach, we use a linear … Read more

Window Sliding Technique – An Efficient Technique

The window sliding technique if helpful when you have to reduce the time complexity from O(N2) to O(N) by converting nested loop into a single loop in problems that includes linear sequences, like arrays. A window is a contiguous sequence of elements in an array and as the name suggests in the window sliding technique, … Read more

Sieve of Eratosthenes

The name seems to be threatening right? On the contrary, Sieve of Eratosthenes is a technique developed by one of the brilliant Greek Mathematicians, Eratosthenes, to eliminate complex looping multiplications or divisions. This algorithm removes all the numbers that do not meet a specific criterion, hence works like a sieve. This is one of the … Read more