Articles for category: Data Structures and Algorithms

The Celebrity Problem

You go to a party of N people, and you have to find out the celebrity over there. Scope This article tells about how to solve celebrity problem. In this article we will learn brute force approach to solve celebrity problem. In this article we will learn to solve celebrity problem using graphs In this ...

Chocolate Distribution Problem

Problem Statement In the above chocolate distribution problem, you will be given an array of n integers where each value represents the number of chocolates in a packet. Each packet can have a variable number of chocolates. There are m students, the task is to distribute chocolate packets such that: Rules : Input Format : ...

LRU Cache Implementation

Problem Statement Design a data structure to implement following the operations of Least Recently Used (LRU) Cache – Note – The compulsion here is that, you need to implement such that both the put and the get operation take O(1) time on an average. LRU Cache Before beginning with the LRU Cache, let’s see what does a cache means? We know that we ...

N/3 Repeat Number

Given an array of integers of size n, we have to find out if any integer occurs more than n/3 times in the array in linear time and constant extra space. If we find an element then print the element else print -1. Scope This article tells about Moore’s Voting Algorithm In this article we ...

Find Second Largest Number in Array

You are given an array of numbers (integers) and the task is to find second largest number in array. Takeaways The most efficient approach to find second largest number in array uses only one loop. Find Second Largest Element in an Array Refer to the Example and Explanationsections for more details about how to find second largest number ...

Reverse Doubly Linked List

Problem Statement You are given a pointer or reference to the doubly linked list’s head node and the task is to reverse a doubly linked list (the order should be reversed). After changing the order, you need to return the pointer or reference to the head node of the reversed doubly linked list. Refer to the example and explanation section for more details and the approach section to understand ...

Tree Traversal

The tree is a hierarchical non-linear data structure that is used to organize, store, and manipulated the data efficiently. The tree traversal in the data structure is a technique that is used to traverse or visit the nodes of a tree (for printing, modifying, etc.). As we know all the nodes of a tree form ...

Painters Partition Problem

In Painters Partition problem, we have M boards of length {l1, l2,... lm} to paint, and there are N painters available. In Painters Partition, each painter takes one unit of time to paint one unit of the board. Calculate the minimum amount of time to complete this task while keeping in mind that any painter ...

Merge Two Sorted Arrays

Problem Statement Given two sorted integer arrays of size m and n, Your task is to merge both the given sorted arrays into one so that the output array is also sorted. The image below visualises the merge two sorted arrays problem. The constraints for the merge two sorted arrays problem are given below. Example ...