Find the minimum distance between two numbers
Last Updated :
23 Jul, 2025
Given an unsorted array arr[] and two numbers x and y, find the minimum distance between x and y in arr[]. The array might also contain duplicates. You may assume that both x and y are different and present in arr[].
Examples:
Input: arr[] = {1, 2}, x = 1, y = 2
Output: Minimum distance between 1 and 2 is 1.
Explanation: 1 is at index 0 and 2 is at index 1, so the distance is 1
Input: arr[] = {3, 4, 5}, x = 3, y = 5
Output: Minimum distance between 3 and 5 is 2.
Explanation: 3 is at index 0 and 5 is at index 2, so the distance is 2
Input: arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3}, x = 3, y = 6
Output: Minimum distance between 3 and 6 is 4.
Explanation: 3 is at index 0 and 6 is at index 4, so the distance is 4
Input: arr[] = {2, 5, 3, 5, 4, 4, 2, 3}, x = 3, y = 2
Output: Minimum distance between 3 and 2 is 1.
Explanation: 3 is at index 7 and 2 is at index 6, so the distance is 1
Method 1:
The task is to find the distance between two given numbers, So find the distance between any two elements using nested loops. The outer loop for selecting the first element (x) and the inner loop is for traversing the array in search for the other element (y) and taking the minimum distance between them.
Follow the steps below to implement the above idea:
- Create a variable m = INT_MAX
- Run a nested loop, the outer loop runs from start to end (loop counter i), the inner loop runs from i+1 to end (loop counter j).
- If the ith element is x and jth element is y or vice versa, update m as m = min(m,j-i)
- Print the value of m as minimum distance
Below is the implementation of the above approach:
C++
// C++ program to Find the minimum
// distance between two numbers
#include <bits/stdc++.h>
using namespace std;
int minDist(int arr[], int n, int x, int y)
{
int i, j;
int min_dist = INT_MAX;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if ((x == arr[i] && y == arr[j]
|| y == arr[i] && x == arr[j])
&& min_dist > abs(i - j)) {
min_dist = abs(i - j);
}
}
}
if (min_dist > n) {
return -1;
}
return min_dist;
}
/* Driver code */
int main()
{
int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 3;
int y = 6;
cout << "Minimum distance between " << x << " and " << y
<< " is " << minDist(arr, n, x, y) << endl;
}
// This code is contributed by Shivi_Aggarwal
C
// C program to Find the minimum
// distance between two numbers
#include <limits.h> // for INT_MAX
#include <stdio.h>
#include <stdlib.h> // for abs()
int minDist(int arr[], int n, int x, int y)
{
int i, j;
int min_dist = INT_MAX;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if ((x == arr[i] && y == arr[j]
|| y == arr[i] && x == arr[j])
&& min_dist > abs(i - j)) {
min_dist = abs(i - j);
}
}
}
if (min_dist > n) {
return -1;
}
return min_dist;
}
/* Driver program to test above function */
int main()
{
int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 0;
int y = 6;
printf("Minimum distance between %d and %d is %d\n", x,
y, minDist(arr, n, x, y));
return 0;
}
Java
// Java Program to Find the minimum
// distance between two numbers
import java.io.*;
class MinimumDistance {
int minDist(int arr[], int n, int x, int y)
{
int i, j;
int min_dist = Integer.MAX_VALUE;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if ((x == arr[i] && y == arr[j]
|| y == arr[i] && x == arr[j])
&& min_dist > Math.abs(i - j))
min_dist = Math.abs(i - j);
}
}
if (min_dist > n) {
return -1;
}
return min_dist;
}
public static void main(String[] args)
{
MinimumDistance min = new MinimumDistance();
int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 };
int n = arr.length;
int x = 0;
int y = 6;
System.out.println("Minimum distance between " + x
+ " and " + y + " is "
+ min.minDist(arr, n, x, y));
}
}
Python3
# Python3 code to Find the minimum
# distance between two numbers
def minDist(arr, n, x, y):
min_dist = 99999999
for i in range(n):
for j in range(i + 1, n):
if (x == arr[i] and y == arr[j] or
y == arr[i] and x == arr[j]) and min_dist > abs(i-j):
min_dist = abs(i-j)
return min_dist
# Driver code
arr = [3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3]
n = len(arr)
x = 3
y = 6
print("Minimum distance between ", x, " and ",
y, "is", minDist(arr, n, x, y))
# This code is contributed by "Abhishek Sharma 44"
C#
// C# code to Find the minimum
// distance between two numbers
using System;
class GFG {
static int minDist(int []arr, int n,
int x, int y)
{
int i, j;
int min_dist = int.MaxValue;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if ((x == arr[i] &&
y == arr[j] ||
y == arr[i] &&
x == arr[j])
&& min_dist >
Math.Abs(i - j))
min_dist =
Math.Abs(i - j);
}
}
return min_dist;
}
// Driver function
public static void Main()
{
int []arr = {3, 5, 4, 2, 6,
5, 6, 6, 5, 4, 8, 3};
int n = arr.Length;
int x = 3;
int y = 6;
Console.WriteLine("Minimum "
+ "distance between "
+ x + " and " + y + " is "
+ minDist(arr, n, x, y));
}
}
// This code is contributed by Sam007
JavaScript
<script>
// Javascript program to find the minimum
// distance between two numbers
function minDist(arr, n, x, y)
{
var i, j;
var min_dist = Number.MAX_VALUE;
for(i = 0; i < n; i++)
{
for(j = i + 1; j < n; j++)
{
if ((x == arr[i] && y == arr[j] ||
y == arr[i] && x == arr[j]) &&
min_dist > Math.abs(i - j))
min_dist = Math.abs(i - j);
}
}
if(min_dist>n)
{
return -1;
}
return min_dist;
}
// Driver code
var arr = [ 3, 5, 4, 2, 6, 5,
6, 6, 5, 4, 8, 3 ];
var n = arr.length;
var x = 3;
var y = 6;
document.write("Minimum distance between " + x +
" and " + y + " is " +
minDist(arr, n, x, y));
// This code is contributed by gauravrajput1
</script>
PHP
<?php
// PHP program to Find the minimum
// distance between two numbers
function minDist($arr, $n, $x, $y)
{
$i; $j;
$min_dist = PHP_INT_MAX;
for ($i = 0; $i < $n; $i++)
{
for ($j = $i + 1; $j < $n; $j++)
{
if( ($x == $arr[$i] and $y == $arr[$j] or
$y == $arr[$i] and $x == $arr[$j]) and
$min_dist > abs($i - $j))
{
$min_dist = abs($i - $j);
}
}
}
if($min_dist>$n)
{
return -1;
}
return $min_dist;
}
// Driver Code
$arr = array(3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3);
$n = count($arr);
$x = 0;
$y = 6;
echo "Minimum distance between ",$x, " and ",$y," is ";
echo minDist($arr, $n, $x, $y);
// This code is contributed by anuj_67.
?>
OutputMinimum distance between 3 and 6 is 4
Time Complexity: O(n^2), Nested loop is used to traverse the array.
Auxiliary Space: O(1), no extra space is required.
Method 2:
The basic approach is to check only consecutive pairs of x and y. For every element x or y, check the index of the previous occurrence of x or y and if the previous occurring element is not similar to current element update the minimum distance. But a question arises what if an x is preceded by another x and that is preceded by a y, then how to get the minimum distance between pairs. By analyzing closely it can be seen that every x followed by a y or vice versa can only be the closest pair (minimum distance) so ignore all other pairs.
Follow the steps below to implement the above idea:
- Create a variable prev=-1 and m= INT_MAX
- Traverse through the array from start to end.
- If the current element is x or y, prev is not equal to -1 and array[prev] is not equal to current element then update m = min(current_index - prev, m), i.e. find the distance between consecutive pairs and update m with it.
- print the value of m
Below is the implementation of the above approach:
C++
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
int minDist(int arr[], int n, int x, int y)
{
//previous index and min distance
int p = -1, min_dist = INT_MAX;
for(int i=0 ; i<n ; i++)
{
if(arr[i]==x || arr[i]==y)
{
//we will check if p is not equal to -1 and
//If the element at current index matches with
//the element at index p , If yes then update
//the minimum distance if needed
if( p != -1 && arr[i] != arr[p])
min_dist = min(min_dist , i-p);
//update the previous index
p=i;
}
}
//If distance is equal to int max
if(min_dist==INT_MAX)
return -1;
return min_dist;
}
/* Driver code */
int main()
{
int arr[] = {3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 3;
int y = 6;
cout << "Minimum distance between " << x <<
" and " << y << " is "<<
minDist(arr, n, x, y) << endl;
return 0;
}
// This code is contributed by Mukul singh.
C
#include <stdio.h>
#include <limits.h> // For INT_MAX
//returns minimum of two numbers
int min(int a ,int b)
{
if(a < b)
return a;
return b;
}
int minDist(int arr[], int n, int x, int y)
{
//previous index and min distance
int i=0,p=-1, min_dist=INT_MAX;
for(i=0 ; i<n ; i++)
{
if(arr[i] ==x || arr[i] == y)
{
//we will check if p is not equal to -1 and
//If the element at current index matches with
//the element at index p , If yes then update
//the minimum distance if needed
if(p != -1 && arr[i] != arr[p])
min_dist = min(min_dist,i-p);
//update the previous index
p=i;
}
}
//If distance is equal to int max
if(min_dist==INT_MAX)
return -1;
return min_dist;
}
/* Driver program to test above function */
int main()
{
int arr[] ={3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3};
int n = sizeof(arr)/sizeof(arr[0]);
int x = 3;
int y = 6;
printf("Minimum distance between %d and %d is %d\n", x, y,
minDist(arr, n, x, y));
return 0;
}
Java
import java.io.*;
class MinimumDistance
{
int minDist(int arr[], int n, int x, int y)
{
//previous index and min distance
int i=0,p=-1, min_dist=Integer.MAX_VALUE;
for(i=0 ; i<n ; i++)
{
if(arr[i] ==x || arr[i] == y)
{
//we will check if p is not equal to -1 and
//If the element at current index matches with
//the element at index p , If yes then update
//the minimum distance if needed
if(p != -1 && arr[i] != arr[p])
min_dist = Math.min(min_dist,i-p);
//update the previous index
p=i;
}
}
//If distance is equal to int max
if(min_dist==Integer.MAX_VALUE)
return -1;
return min_dist;
}
/* Driver program to test above functions */
public static void main(String[] args) {
MinimumDistance min = new MinimumDistance();
int arr[] = {3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3};
int n = arr.length;
int x = 3;
int y = 6;
System.out.println("Minimum distance between " + x + " and " + y
+ " is " + min.minDist(arr, n, x, y));
}
}
Python3
import sys
def minDist(arr, n, x, y):
#previous index and min distance
i=0
p=-1
min_dist = sys.maxsize;
for i in range(n):
if(arr[i] ==x or arr[i] == y):
#we will check if p is not equal to -1 and
#If the element at current index matches with
#the element at index p , If yes then update
#the minimum distance if needed
if(p != -1 and arr[i] != arr[p]):
min_dist = min(min_dist,i-p)
#update the previous index
p=i
#If distance is equal to int max
if(min_dist == sys.maxsize):
return -1
return min_dist
# Driver program to test above function */
arr = [3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3]
n = len(arr)
x = 3
y = 6
print ("Minimum distance between %d and %d is %d\n"%( x, y,minDist(arr, n, x, y)));
# This code is contributed by Shreyanshi Arun.
C#
// C# program to Find the minimum
// distance between two numbers
using System;
class MinimumDistance {
static int minDist(int []arr, int n,
int x, int y)
{
//previous index and min distance
int i=0,p=-1, min_dist=int.MaxValue;
for(i=0 ; i<n ; i++)
{
if(arr[i] ==x || arr[i] == y)
{
//we will check if p is not equal to -1 and
//If the element at current index matches with
//the element at index p , If yes then update
//the minimum distance if needed
if(p != -1 && arr[i] != arr[p])
min_dist = Math.Min(min_dist,i-p);
//update the previous index
p=i;
}
}
//If distance is equal to int max
if(min_dist==int.MaxValue)
return -1;
return min_dist;
}
// Driver Code
public static void Main()
{
int []arr = {3, 5, 4, 2, 6, 3,
0, 0, 5, 4, 8, 3};
int n = arr.Length;
int x = 3;
int y = 6;
Console.WriteLine("Minimum distance between " + x + " and " + y
+ " is " + minDist(arr, n, x, y));
}
}
// This code is contributed by anuj_67.
JavaScript
<script>
function minDist(arr , n , x , y)
{
// previous index and min distance
var i=0,p=-1, min_dist=Number.MAX_VALUE;
for(i=0 ; i<n ; i++)
{
if(arr[i] ==x || arr[i] == y)
{
// we will check if p is not equal to -1 and
// If the element at current index matches with
// the element at index p , If yes then update
// the minimum distance if needed
if(p != -1 && arr[i] != arr[p])
min_dist = Math.min(min_dist,i-p);
// update the previous index
p=i;
}
}
// If distance is equal to var max
if(min_dist==Number.MAX_VALUE)
return -1;
return min_dist;
}
/* Driver program to test above functions */
var arr = [3, 5, 4, 2, 6, 3, 0, 0, 5, 4, 8, 3];
var n = arr.length;
var x = 3;
var y = 6;
document.write("Minimum distance between " + x + " and " + y
+ " is " + minDist(arr, n, x, y));
// This code contributed by shikhasingrajput
</script>
PHP
<?php
// PHP program to Find the minimum
// distance between two numbers
function minDist($arr, $n, $x, $y)
{
//previous index and min distance
$i=0;
$p=-1;
$min_dist=PHP_INT_MAX;
for($i=0 ; $i<$n ; $i++)
{
if($arr[$i] ==$x || $arr[$i] == $y)
{
//we will check if p is not equal to -1 and
//If the element at current index matches with
//the element at index p , If yes then update
//the minimum distance if needed
if($p != -1 && $arr[$i] != $arr[$p])
$min_dist = min($min_dist,$i-$p);
//update the previous index
$p=$i;
}
}
//If distance is equal to int max
if($min_dist==PHP_INT_MAX)
return -1;
return $min_dist;
}
/* Driver program to test above function */
$arr =array(3, 5, 4, 2, 6, 3, 0, 0, 5,
4, 8, 3);
$n = count($arr);
$x = 3;
$y = 6;
echo "Minimum distance between $x and ",
"$y is ", minDist($arr, $n, $x, $y);
// This code is contributed by anuj_67.
?>
OutputMinimum distance between 3 and 6 is 1
Time Complexity: O(n), Only one traversal of the array is needed.
Auxiliary Space: O(1), As no extra space is required.
Method 3:
The problem says that we want a minimum distance between x and y. So the approach is traverse the array and while traversing in array if we got the number as x or y then we will store the difference between indices of previously found x or y and newly find x or y and like this for every time we will try to minimize the difference.
Follow the steps below to implement the above idea:
- Create variables idx1 = -1, idx2 = -1 and min_dist = INT_MAX;
- Traverse the array from i = 0 to i = n-1 where n is the size of array.
- While traversing if the current element is x then store index of current element in idx1 or if the current element is y then store index of current element in idx2.
- If idx1 and idx2 variables are not equal to -1 then store minimum of min_dist, difference of idx1 and idx2 into ans.
- At the end of traversal, if idx1 or idx2 are still -1(x or y not found in array) then return -1 or else return min_dist.
Below is the implementation of the above approach:
C++
// C++ program to Find the minimum
// distance between two numbers
#include <bits/stdc++.h>
using namespace std;
int minDist(int arr[], int n, int x, int y)
{
//idx1 and idx2 will store indices of
//x or y and min_dist will store the minimum difference
int idx1=-1,idx2=-1,min_dist = INT_MAX;
for(int i=0;i<n;i++)
{
//if current element is x then change idx1
if(arr[i]==x)
{
idx1=i;
}
//if current element is y then change idx2
else if(arr[i]==y)
{
idx2=i;
}
//if x and y both found in array
//then only find the difference and store it in min_dist
if(idx1!=-1 && idx2!=-1)
min_dist=min(min_dist,abs(idx1-idx2));
}
//if left or right did not found in array
//then return -1
if(idx1==-1||idx2==-1)
return -1;
//return the minimum distance
else
return min_dist;
}
/* Driver code */
int main()
{
int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 3;
int y = 6;
cout << "Minimum distance between " << x << " and " << y
<< " is " << minDist(arr, n, x, y) << endl;
}
Java
// Java program to Find the minimum
// distance between two numbers
import java.io.*;
public class GFG {
static int minDist(int arr[], int n, int x, int y)
{
// idx1 and idx2 will store indices of
// x or y and min_dist will store the minimum
// difference
int idx1 = -1, idx2 = -1,
min_dist = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
// if current element is x then change idx1
if (arr[i] == x) {
idx1 = i;
}
// if current element is y then change idx2
else if (arr[i] == y) {
idx2 = i;
}
// if x and y both found in array
// then only find the difference and store it in
// min_dist
if (idx1 != -1 && idx2 != -1)
min_dist = Math.min(min_dist,
Math.abs(idx1 - idx2));
}
// if left or right did not found in array
// then return -1
if (idx1 == -1 || idx2 == -1)
return -1;
// return the minimum distance
else
return min_dist;
}
/* Driver code */
public static void main(String[] args)
{
int arr[] = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3 };
int n = arr.length;
int x = 3;
int y = 6;
System.out.println("Minimum distance between " + x
+ " and " + y + " is "
+ minDist(arr, n, x, y));
}
}
// This code is contributed by Lovely Jain
Python3
# Python program to Find the minimum
# distance between two numbers
import sys
def minDist(arr, n, x, y) :
# idx1 and idx2 will store indices of
# x or y and min_dist will store the minimum difference
idx1=-1; idx2=-1; min_dist = sys.maxsize;
for i in range(n) :
# if current element is x then change idx1
if arr[i]==x :
idx1=i
# if current element is y then change idx2
elif arr[i]==y :
idx2=i
# if x and y both found in array
# then only find the difference and store it in min_dist
if idx1!=-1 and idx2!=-1 :
min_dist=min(min_dist,abs(idx1-idx2));
# if left or right did not found in array
# then return -1
if idx1==-1 or idx2==-1 :
return -1
# return the minimum distance
else :
return min_dist
# Driver code
if __name__ == "__main__" :
arr = [ 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3]
n = len(arr)
x = 3
y = 6
print ("Minimum distance between %d and %d is %d\n"%( x, y,minDist(arr, n, x, y)));
# this code is contributed by aditya942003patil
C#
// C# program to Find the minimum
// distance between two numbers
using System;
class MinimumDistance {
static int minDist(int []arr, int n,
int x, int y)
{
//idx1 and idx2 will store indices of
//x or y and min_dist will store the minimum difference
int idx1=-1,idx2=-1,min_dist = int.MaxValue;
for(int i=0;i<n;i++)
{
//if current element is x then change idx1
if(arr[i]==x)
{
idx1=i;
}
//if current element is y then change idx2
else if(arr[i]==y)
{
idx2=i;
}
//if x and y both found in array
//then only find the difference and store it in min_dist
if(idx1!=-1 && idx2!=-1)
min_dist=Math.Min(min_dist,Math.Abs(idx1-idx2));
}
//if left or right did not found in array
//then return -1
if(idx1==-1||idx2==-1)
return -1;
//return the minimum distance
else
return min_dist;
}
// Driver Code
public static void Main()
{
int []arr = { 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3};
int n = arr.Length;
int x = 3;
int y = 6;
Console.WriteLine("Minimum distance between " + x + " and " + y
+ " is " + minDist(arr, n, x, y));
}
}
// This code is contributed by aditya942003patil.
JavaScript
<script>
function minDist(arr , n , x , y)
{
//idx1 and idx2 will store indices of
//x or y and min_dist will store the minimum difference
var idx1=-1,idx2=-1,min_dist = Number.MAX_VALUE;
for(var i=0;i<n;i++)
{
//if current element is x then change idx1
if(arr[i]==x)
{
idx1=i;
}
//if current element is y then change idx2
else if(arr[i]==y)
{
idx2=i;
}
//if x and y both found in array
//then only find the difference and store it in min_dist
if(idx1!=-1 && idx2!=-1)
min_dist=Math.min(min_dist,Math.abs(idx1-idx2));
}
//if left or right did not found in array
//then return -1
if(idx1==-1||idx2==-1)
return -1;
//return the minimum distance
else
return min_dist;
}
/* Driver program to test above functions */
var arr = [ 3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3];
var n = arr.length;
var x = 3;
var y = 6;
document.write("Minimum distance between " + x + " and " + y
+ " is " + minDist(arr, n, x, y));
// This code contributed by aditya942003patil
</script>
OutputMinimum distance between 3 and 6 is 4
Time Complexity: O(n), Only one traversal of the array is required.
Auxiliary Space: O(1), No extra space is required.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem