Category Archives: Data Structure

Check if one string can be made another by swapping at most once

Problem Given two strings A and B, check if A can be made equal to B by swapping two characters of A at most once. Sample Input “aba” “aab” Sample Output True Explanation Swap index 1 with index 2 to get “aab” Approach We use two indices, can1 and can2, to store up to two different positions between… Read More »

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 < arr < … < 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 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 »

What is Pseudocode? Pseudocode Examples

If you are new to programming and starting your journey by learning a new programming language. You come across the term pseudocode and wonder what pseudocode is and why to learn and use it? This article will discuss what pseudocode is in computer science and programming and why to use it. For a better understanding, we will practice… 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 »

Introduction to Star Schema in Data Warehouse Modeling

A data warehouse (DW), also known as a computing data warehouse, is a type of data management system that supports various business intelligence activities, particularly analytics. It acts as a central repository for data collected from heterogeneous sources. It stores all historical data in one place, and this data is used for generating analytical reports that help enterprises… 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 »

Implement power function without using multiplication and division operators (Recursive Approach)

Problem Given two positive integers x and y, implement xy without using multiplication or division operator Sample Input x = 2 y = 3 Sample Output 8 Explanation 23 = 8 Approach We will use the below properties of exponents to solve this problem: xy = x * xy-1 x0 = 1 The above properties resemble recursion as… Read More »