# Category: Beginner

## Find the first repeating character in a given string

Objec­tive: Given a string, write an algo­rithm to find the first repeat­ing char­ac­ter in it. Exam­ple: String input = “hori­zon tuto­ri­als” Out­put: ‘o’ String input = “algo­rithms” Out­put: No repeat­ing char­ac­ter found. Approach: Naive approach:…

## Reverse the given Array without using built in function

Objec­tive: Given a array, write an algo­rithm to reverse the array. Exam­ple: int a[] = {1, 2, 3, 4, 5} Out­put: {5, 4, 3, 2, 1} Approach: It’s obvi­ous that you can­not use any built-in…

## Implement Stack Using Linked List

Objec­tive: Write an algo­rithm to imple­ment Stack using Linked List. If you do not know about then for starters its abstract data type in which fol­lows the prin­ci­ple of LIFO (Last-In-First-Out) which means the…

## Doubly Linked List Complete Implementation

In this arti­cle we will see what is dou­bly linked list, how it is dif­fer­ent from other linked list and how to imple­ment it. Ear­lier we have seen what is Singly Linked List and Circular…

## Circular Linked List Complete Implementation

Ear­lier we have seen what is Singly Linked List and How to imple­ment it. In a way you say that it’s an exten­sion of singly linked list. I would sug­gest that if you do…

## Get the Sum of all left leaves in a Binary tree

Objec­tive: Given a binary tree, find the sum of all the nodes which are left as well as leaves nodes. Exam­ple:     Approach: Approach is quite sim­ple. Do the inorder tra­ver­sal check if…

## Binary Tree-Postorder Traversal — Non Recursive Approach

Objec­tive: Given a binary tree, write a non recur­sive or iter­a­tive algo­rithm for pos­torder tra­ver­sal. Exam­ple: Ear­lier we have seen “What is pos­torder tra­ver­sal and recur­sive algo­rithm for it”, In this arti­cle we will…

## Binary Tree — Preorder Traversal — Non Recursive Approach

Objec­tive: Given a binary tree, write a non recur­sive or iter­a­tive algo­rithm for pre­order tra­ver­sal. Exam­ple: Ear­lier we have seen “What is pre­order tra­ver­sal and recur­sive algo­rithm for it”, In this arti­cle we will…

## Binary Tree-Inorder Traversal — Non Recursive Approach

Objec­tive: Given a binary tree, write a non recur­sive or iter­a­tive algo­rithm for Inorder tra­ver­sal. Exam­ple: Ear­lier we have seen “What is Inorder tra­ver­sal and recur­sive algo­rithm for it”, In this arti­cle we will…

## Delete the Binary Tree

Objec­tive: Given a binary tree, write an algo­rithm to delete it. This is one of the basic prob­lem in trees. if you are new to trees then this prob­lem will help you build your…

## Search the Element in a binary tree — With and Without Recursion

Objec­tive: Given a binary tree and a given num­ber x, Write an recur­sive algo­rithm to search the ele­ment in the tree. This is one of the very basic prob­lems of tree. If you are new…

## Tree Traversals

There are mul­ti­ple ways to in which you can tra­verse a tree. In this arti­cle we will see these tra­ver­sals in detail. If you are new to trees then I would rec­om­mend that you…

## Find the Size of a Binary Tree without Recursion

Objec­tive: Given a binary tree, Write an non-recursive algo­rithm to find the size of the tree. Note : Size of the tree is num­ber of nodes in the tree Approach: In our ear­lier post (link) we…

## Find the Kth Smallest/Largest Element in an Array

Objec­tive: Given an array of inte­gers. find the Kth Smallest/largest ele­ment in the array. Exam­ple: int[] A = { 1, 2, 10, 20, 40, 32, 44, 51, 6 }; K=4. 4th small­est ele­ment in given…

## Priority Queue Implementation

Ear­lier in we have seen Min-Heap and Max-Heap Imple­men­ta­tion. Pri­or­ity Queue is its built-in imple­men­ta­tion in Java. In this arti­cle we will see how to per­form Min-Heap and Max-Heap using Pri­or­ity Queue. Brief: A priority…

## Find the Second Largest Element in an Array

Objec­tive: Given an array of inte­gers. find the sec­ond largest ele­ment in the array. Exam­ple: int[] A = { 1, 2, 10, 20, 40, 32, 44, 51, 6 }; Sec­ond largest Ele­ment : 44 Approach:…

## Breadth-First Search/Traversal in a Binary Tree

Breadth-First Search ( or Tra­ver­sal) also know as Level Order Tra­ver­sal. Exam­ple: What is Breadth First Search: Breadth-first search (BFS) is an algo­rithm for tra­vers­ing or search­ing tree or graph data struc­tures. It starts…

## Sort Names by their Last Names.

Objec­tive: Given a list of names ( first name and last name), sort the list by their last names. Exam­ple: List [] = {“Daen­erys Tar­garyen”, “Jon Snow”, ” Tyrion Lan­nis­ter”, ” Jof­frey Baratheon”} Out­put: [Joffrey…

## Introduction To Backtracking Programming

What is Back­track­ing Pro­gram­ming?? Recur­sion is the key in back­track­ing pro­gram­ming. As the name sug­gests we back­track to find the solu­tion. We start with one pos­si­ble move out of many avail­able moves and try…

## Dynamic Programming — Stairs Climbing Puzzle

Objec­tive: A child is climb­ing up a stair­case with n steps, and can hop either 1 step, 2 steps, or 3 steps at a time. Imple­ment a method to count how many pos­si­ble ways…

## Introduction To Dynamic Programming — Fibonacci Series

What is Dynamic Pro­gram­ming: Dynamic pro­gram­ming is a tech­nique to solve the recur­sive prob­lems in more effi­cient man­ner. Many times in recur­sion we solve the sub-problems repeat­edly. In dynamic pro­gram­ming we store the solution…

## XOR Method">Find a Missing Number From a Sequence of Consecutive Numbers | XOR Method

Input:  Array, arrA[] with a miss­ing num­ber and Range Out­put : miss­ing num­ber Exam­ple: int A[] = { 1, 2, 7, 6, 3, 4 }; int range = 7; Out­put: MIss­ing No is :5 In…

## Find the Max element in a Given Binary Tree

Objec­tive: — Given a binary tree , Find the max ele­ment in it. Exam­ple: Approach: Use Recur­sion. Max will the Max(root, max ele­ment in left sub­tree, max ele­ment in right­sub­tree) Recur­sively solve for max element…

## Check If One Binary is Mirror Tree of another Binary Tree.

Objec­tive: — Given two binary trees check if they are mir­ror image of each other. Exam­ple: Approach:

## Print All The Full Nodes in a Binary Tree

Objec­tive: Given a binary tree, print all nodes will are full nodes. Full Nodes: Nodes Which has both the chil­dren, left and right are called Full Nodes Approach: quite sim­ple Solu­tion. Do the any of the…

## Magic Index — Find Index In Sorted Array Such That A[i] = i.

Objec­tive: Given a sorted array of dis­tinct inte­gers, Find the Magic index or Fixed point in the array. Magic Index or Fixed Point: Magic index or a Fixed point in an array is an index…

## Find numbers which are palindrome in both their decimal and octal Representations

Objec­tive: Given a range of inte­gers, find all the num­bers which are palin­drome when they are rep­re­sented in Dec­i­mal Value( base 10) and in Octal value(base 8). Exam­ple : Num­ber : 373 (Dec­i­mal) and digits…

## Construct a Special Triangle from a Given Array

Objec­tive: Given an array of inte­gers such that first level will print all the ele­ments in the array and from then at each level num­ber of ele­ments will be one less than the previous…

## Goldbach’s Conjecture

Goldbach’s con­jec­ture — Every even inte­ger greater than 2 can be rep­re­sented as the sum of two primes num­bers. Exam­ple: Given Num­ber : 200 Prime Num­bers are 3 197 Prime Num­bers are 7 193 Prime…

## Convert Decimal into Irreducible Fraction

Objec­tive: Given a dec­i­mal num­ber, con­vert it into irre­ducible frac­tion. Irre­ducible Frac­tion : An irre­ducible frac­tion is a frac­tion in which the numer­a­tor and denom­i­na­tor are inte­gers that have no other com­mon divi­sors than…

## Clock Angle Problem

Objec­tive: Find the Angle between hour hand and minute hand at the given time. Exam­ple: Time : 12:45 Input : hour = 12, Minute = 45 Out­put : 112.5 Time : 3:30 Input : hour…

## Towers Of Hanoi

The Tower of Hanoi is a math­e­mat­i­cal game or puz­zle. It con­sists of three rods, and a num­ber of disks of dif­fer­ent sizes which can slide onto any rod. The objec­tive of the puz­zle is…

## Find The Missing Duplicate in a Given Array.

Objec­tive: — Given an Inte­ger array. Array con­tains dupli­cates of all the num­bers in array except one num­ber . Find that num­ber. Exam­ple : int [] A = { 2,1,3,5,5,3,2,1,6,7,7,8,8}; Out­put : Miss­ing duplicate…

## OR use only Max() function.">Sort 3 Integers without using if condition OR use only Max() function.

Objec­tive: — Given three inte­gers, sort them with­out using if con­di­tion. Appraoch: Say 3 inte­gers are, a, b, c. Find the max­i­mum of a, b, c using Max() func­tion. mul­ti­ply all inte­gers by –1. Again…

## GCD)">Euclidean algorithm — Greatest Common Divisor(GCD)

The great­est com­mon divi­sor (GCD) of two or more inte­gers, when at least one of them is not zero, is the largest pos­i­tive inte­ger that divides the num­bers with­out a remain­der. For exam­ple, the…

## Delete X Nodes After Y Nodes In a Linked List

Objec­tive: Given a Linked List and x and y. Delete x num­ber of nodes after y nodes from the start. Exam­ple: ->10->20->30->40->50->60->70->80->90->100->110->120 Deleted 4 Nodes after 5 Nodes ->10->20->30->40->50->100->110->120 Approach:

## Depth First Search/Traversal in Binary Tree

Objec­tive: — Given a Binary Search Tree, Do the Depth First Search/Traversal . Appraoch: Approach is quite sim­ple, use Stack. First add the add to Stack. Pop out an ele­ment from Stack and add its right…

## Check if Array is Consecutive Integers

Objec­tive: Given a array of unsorted num­bers, check if all the num­bers in the array are con­sec­u­tive num­bers. Exam­ples: int [] arrA = {21,24,22,26,23,25}; — True (All the inte­gers are con­sec­u­tive from 21 to…

## Find intersection between Two Sorted Arrays.

Objec­tive: Given two sorted arrays, Find inter­sec­tion point between them. Exam­ples: int[] a = { 1, 2, 3, 6, 8, 10 }; int[] b = { 4, 5, 6, 11, 15, 20 }; Output:…

## Given a binary tree, Convert it into its Mirror Tree

Objec­tive: Given a binary tree, Con­vert it into its Mir­ror Tree Mir­ror Tree: Mir­ror Tree of a binary tree is where left and right child of every node of given binary tree is interex­changed. Input:…

## Given a binary tree, Print All the Nodes that don’t have Siblings.

Objec­tive: Given a binary tree, Print All the Nodes that don’t have sib­lings. Note: sib­ling node is the node which has the same par­ent, so you need to print the nodes who is a…

## BST’s are Identical">Check if Two BST’s are Identical

Objec­tive: Given Two binary Search Trees, Check if both are iden­ti­cal. Input: Two binary Search Trees Approach:

## Find all common numbers in given three sorted arrays.

Objec­tive: Given three sorted(ascending order) arrays of inte­gers, find out all the com­mon ele­ments in them. Input: Three sorted arrays. Out­put: All the com­mon ele­ments. Exam­ples : Array A = {1,2,3,4,5,6,7,8,9,10}; Array B = {1,3,5,6,7,8,12};…

## Minimum number that cannot be formed by any subset of an array

Objec­tive: Given a sorted array of pos­i­tive inte­gers, find out the small­est inte­ger which can­not be rep­re­sented as the sum of any sub­set of the array Input: A Sorted Array Out­put: The small­est num­ber which…

## Find the Size of the Binary Tree

Objec­tive: Given a Binary tree, Find the size of the tree. Note : Size of the tree is num­ber of nodes in the tree Input: A Binary Tree. Out­put: Size of the tree. Exam­ple : Approach :

## Find whether if a Given Binary Tree is Balanced?

Objec­tive: Given a binary tree, Find whether if a Given Binary Tree is Bal­anced? What is bal­anced Tree: A bal­anced tree is a tree in which dif­fer­ence between heights of sub-trees of any node…

## OR Height of a Binary Tree">Find the Maximum Depth OR Height of a Binary Tree

Objec­tive: Given a binary tree, find the height of it Input: A Binary Tree Out­put: Height of a binary tree Exam­ple: Approach:

## Binary Search Tree Complete Implementation.

Binary Tree : A data struc­ture in which we have nodes con­tain­ing data and two ref­er­ences to other nodes, one on the left and one on the right. Binary Tree con­sist of Nodes Nodes are nothing…

## Reverse a Linked List — Part 2

This post is the exten­sion of our ear­lier post Reverse a linked list. Here We have pro­vided the bet­ter recur­sive solu­tion in which we start revers­ing the list from the end. Objec­tive: Reverse the…

## Delete a Node in the Middle of a linked list, Given only access to that Node

Objec­tive: Write a pro­gram to Delete a Node in the Mid­dle of a linked list, Given only access to that Node Exam­ple: Orig­i­nal List : ->1->2->8->3->7->0->4 After Delet­ing the mid node (say 7) : ->1->2->8->3->0->4…