Sorted subsequence of size 3
Last Updated :
23 Jul, 2025
Given an array of n integers, find the 3 elements such that a[i] < a[j] < a[k] and i < j < k in 0(n) time. If there are multiple such triplets, then print any one of them.
Examples:
Input: arr[] = {12, 11, 10, 5, 6, 2, 30}
Output: 5, 6, 30
Explanation: As 5 < 6 < 30, and they
appear in the same sequence in the array
Input: arr[] = {1, 2, 3, 4}
Output: 1, 2, 3 OR 1, 2, 4 OR 2, 3, 4
Explanation: As the array is sorted, for every i, j, k,
where i < j < k, arr[i] < arr[j] < arr[k]
Input: arr[] = {4, 3, 2, 1}
Output: No such triplet exists.
Naive Approach - O(n) Time and O(n) Space
The main motive is to find an element which has an element smaller than itself on the left side of the array and an element greater than itself on the right side of the array, if there is any such element then there exists a triplet that satisfies the criteria.
- Create an auxiliary array smaller[0..n-1]. smaller[i] stores the index of a number which is smaller than arr[i] and is on the left side. The array contains -1 if there is no such element.
- Create another auxiliary array greater[0..n-1]. greater[i] stores the index of a number which is greater than arr[i] and is on the right side of arr[i]. The array contains -1 if there is no such element.
- Finally traverse both smaller[] and greater[] and find the index [i] for which both smaller[i] and greater[i] are not equal to -1.
C++
#include <bits/stdc++.h>
using namespace std;
// A function to find a sorted sub-sequence of size 3
vector<int> find3Numbers(vector<int> &arr)
{
int n = arr.size();
// Fill smaller[] such that smaller[i] stores the
// index of a smaller element on the left side
vector<int> smaller(n, -1);
int min = 0;
for (int i = 1; i < n; i++)
{
if (arr[i] <= arr[min])
min = i;
else
smaller[i] = min;
}
// Fill greater[] such that greater[i] stores the
// index of a greater element on the right side
vector<int> greater(n, -1);
int max = n - 1;
for (int i = n - 2; i >= 0; i--)
{
if (arr[i] >= arr[max])
max = i;
else
greater[i] = max;
}
// Find the triplet
for (int i = 0; i < n; i++)
{
if (smaller[i] != -1 && greater[i] != -1)
return {arr[smaller[i]], arr[i], arr[greater[i]]};
}
// If no such triplet is found, return an empty vector
return {};
}
// Driver code
int main()
{
vector<int> arr = {12, 11, 10, 5, 6, 2, 30};
vector<int> res = find3Numbers(arr);
for (int x : result)
cout << x << " ";
return 0;
}
C
// C program to find a sorted
// sub-sequence of size 3
#include <stdio.h>
// A function to fund a sorted
// sub-sequence of size 3
void find3Numbers(int arr[], int n)
{
// Index of maximum element
// from right side
int max = n - 1;
// Index of minimum element
// from left side
int min = 0;
int i;
// Create an array that will store
// index of a smaller element on left side.
// If there is no smaller element on left side,
// then smaller[i] will be -1.
int* smaller = new int[n];
// first entry will always be -1
smaller[0] = -1;
for (i = 1; i < n; i++) {
if (arr[i] <= arr[min]) {
min = i;
smaller[i] = -1;
}
else
smaller[i] = min;
}
// Create another array that will
// store index of a greater element
// on right side. If there is no greater
// element on right side, then
// greater[i] will be -1.
int* greater = new int[n];
// last entry will always be -1
greater[n - 1] = -1;
for (i = n - 2; i >= 0; i--) {
if (arr[i] >= arr[max]) {
max = i;
greater[i] = -1;
}
else
greater[i] = max;
}
// Now find a number which has
// both a greater number on right
// side and smaller number on left side
for (i = 0; i < n; i++) {
if (smaller[i] != -1 && greater[i] != -1) {
printf("%d %d %d", arr[smaller[i]],
arr[i], arr[greater[i]]);
return;
}
}
// If we reach number, then
// there are no such 3 numbers
printf("No such triplet found");
// Free the dynamically allocated memory
// to avoid memory leak
delete[] smaller;
delete[] greater;
return;
}
// Driver program to test above function
int main()
{
int arr[] = { 12, 11, 10, 5, 6, 2, 30 };
int n = sizeof(arr) / sizeof(arr[0]);
find3Numbers(arr, n);
return 0;
}
Java
// Java program to find a sorted
// sub-sequence of size 3
import java.io.*;
class SortedSubsequence {
// A function to find a sorted
// sub-sequence of size 3
static void find3Numbers(int arr[])
{
int n = arr.length;
// Index of maximum element
// from right side
int max = n - 1;
// Index of minimum element
// from left side
int min = 0;
int i;
// Create an array that will store
// index of a smaller element on left side.
// If there is no smaller element on left
// side, then smaller[i] will be -1.
int[] smaller = new int[n];
// first entry will always be -1
smaller[0] = -1;
for (i = 1; i < n; i++) {
if (arr[i] <= arr[min]) {
min = i;
smaller[i] = -1;
}
else
smaller[i] = min;
}
// Create another array that will
// store index of a greater element
// on right side. If there is no greater
// element on right side, then greater[i]
// will be -1.
int[] greater = new int[n];
// last entry will always be -1
greater[n - 1] = -1;
for (i = n - 2; i >= 0; i--) {
if (arr[i] >= arr[max]) {
max = i;
greater[i] = -1;
}
else
greater[i] = max;
}
// Now find a number which has
// both greater number on right
// side and smaller number on left side
for (i = 0; i < n; i++) {
if (
smaller[i] != -1 && greater[i] != -1) {
System.out.print(
arr[smaller[i]] + " " + arr[i]
+ " " + arr[greater[i]]);
return;
}
}
// If we reach number, then there
// are no such 3 numbers
System.out.println("No such triplet found");
return;
}
public static void main(String[] args)
{
int arr[] = { 12, 11, 10, 5, 6, 2, 30 };
find3Numbers(arr);
}
}
/* This code is contributed by Devesh Agrawal*/
Python
# Python program to fund a sorted
# sub-sequence of size 3
def find3numbers(arr):
n = len(arr)
# Index of maximum element from right side
max = n-1
# Index of minimum element from left side
min = 0
# Create an array that will store
# index of a smaller element on left side.
# If there is no smaller element on left side,
# then smaller[i] will be -1.
smaller = [0]*10000
smaller[0] = -1
for i in range(1, n):
if (arr[i] <= arr[min]):
min = i
smaller[i] = -1
else:
smaller[i] = min
# Create another array that will
# store index of a greater element
# on right side. If there is no greater
# element on right side, then greater[i]
# will be -1.
greater = [0]*10000
greater[n-1] = -1
for i in range(n-2, -1, -1):
if (arr[i] >= arr[max]):
max = i
greater[i] = -1
else:
greater[i] = max
# Now find a number which has
# both a greater number on right
# side and smaller number on left side
for i in range(0, n):
if smaller[i] != -1 and greater[i] != -1:
print arr[smaller[i]], arr[i], arr[greater[i]]
return
# If we reach here, then there are no such 3 numbers
print "No triplet found"
return
# Driver function to test above function
arr = [12, 11, 10, 5, 6, 2, 30]
find3numbers(arr)
# This code is contributed by Devesh Agrawal
C#
// C# program to find a sorted
// subsequence of size 3
using System;
class SortedSubsequence {
// A function to find a sorted
// subsequence of size 3
static void find3Numbers(int[] arr)
{
int n = arr.Length;
// Index of maximum element from right side
int max = n - 1;
// Index of minimum element from left side
int min = 0;
int i;
// Create an array that will store index
// of a smaller element on left side.
// If there is no smaller element
// on left side, then smaller[i] will be -1.
int[] smaller = new int[n];
// first entry will always be -1
smaller[0] = -1;
for (i = 1; i < n; i++) {
if (arr[i] <= arr[min]) {
min = i;
smaller[i] = -1;
}
else
smaller[i] = min;
}
// Create another array that will store
// index of a greater element on right side.
// If there is no greater element on
// right side, then greater[i] will be -1.
int[] greater = new int[n];
// last entry will always be -1
greater[n - 1] = -1;
for (i = n - 2; i >= 0; i--) {
if (arr[i] >= arr[max]) {
max = i;
greater[i] = -1;
}
else
greater[i] = max;
}
// Now find a number which has
// both a greater number on right side
// and smaller number on left side
for (i = 0; i < n; i++) {
if (smaller[i] != -1 && greater[i] != -1) {
Console.Write(
arr[smaller[i]] + " " + arr[i]
+ " " + arr[greater[i]]);
return;
}
}
// If we reach number, then there
// are no such 3 numbers
Console.Write("No such triplet found");
return;
}
// Driver code
public static void Main()
{
int[] arr = { 12, 11, 10, 5, 6, 2, 30 };
find3Numbers(arr);
}
}
/* This code is contributed by vt_m*/
JavaScript
<script>
// Javascript program to find a sorted
// sub-sequence of size 3
// A function to find a sorted
// sub-sequence of size 3
function find3Numbers(arr)
{
let n = arr.length;
// Index of maximum element
// from right side
let max = n - 1;
// Index of minimum element
// from left side
let min = 0;
let i;
// Create an array that will store
// index of a smaller element on left side.
// If there is no smaller element on left
// side, then smaller[i] will be -1.
let smaller = new Array(n);
for(i=0;i<n;i++)
{
smaller[i]=0;
}
// first entry will always be -1
smaller[0] = -1;
for (i = 1; i < n; i++) {
if (arr[i] <= arr[min]) {
min = i;
smaller[i] = -1;
}
else
smaller[i] = min;
}
// Create another array that will
// store index of a greater element
// on right side. If there is no greater
// element on right side, then greater[i]
// will be -1.
let greater = new Array(n);
for(i=0;i<n;i++)
{
greater[i]=0;
}
// last entry will always be -1
greater[n - 1] = -1;
for (i = n - 2; i >= 0; i--) {
if (arr[i] >= arr[max]) {
max = i;
greater[i] = -1;
}
else
greater[i] = max;
}
// Now find a number which has
// both greater number on right
// side and smaller number on left side
for (i = 0; i < n; i++) {
if (
smaller[i] != -1 && greater[i] != -1) {
document.write(
arr[smaller[i]] + " " + arr[i]
+ " " + arr[greater[i]]);
return;
}
}
// If we reach number, then there
// are no such 3 numbers
document.write("No such triplet found <br>");
return;
}
let arr=[12, 11, 10, 5, 6, 2, 30 ]
find3Numbers(arr);
// This code is contributed by avanitrachhadiya2155
</script>
PHP
<?php
// PHP program to find a sorted
// subsequence of size 3
// A function to fund a sorted
// subsequence of size 3
function find3Numbers(&$arr, $n)
{
// Index of maximum element from right side
$max = $n - 1;
// Index of minimum element from left side
$min = 0;
// Create an array that will store
// index of a smaller element on
// left side. If there is no smaller
// element on left side, then
// smaller[i] will be -1.
$smaller = array_fill(0, $n, NULL);
$smaller[0] = -1; // first entry will
// always be -1
for ($i = 1; $i < $n; $i++)
{
if ($arr[$i] <= $arr[$min])
{
$min = $i;
$smaller[$i] = -1;
}
else
$smaller[$i] = $min;
}
// Create another array that will
// store index of a greater element
// on right side. If there is no
// greater element on right side,
// then greater[i] will be -1.
$greater = array_fill(0, $n, NULL);
// last entry will always be -1
$greater[$n - 1] = -1;
for ($i = $n - 2; $i >= 0; $i--)
{
if ($arr[$i] >= $arr[$max])
{
$max = $i;
$greater[$i] = -1;
}
else
$greater[$i] = $max;
}
// Now find a number which has both
// a greater number on right side
// and smaller number on left side
for ($i = 0; $i < $n; $i++)
{
if ($smaller[$i] != -1 &&
$greater[$i] != -1)
{
echo $arr[$smaller[$i]]." ".
$arr[$i] . " " .
$arr[$greater[$i]];
return;
}
}
// If we reach number, then there
// are no such 3 numbers
printf("No such triplet found");
return;
}
// Driver Code
$arr = array(12, 11, 10, 5, 6, 2, 30);
$n = sizeof($arr);
find3Numbers($arr, $n);
// This code is contributed
// by ChitraNayal
?>
Expected Approach - O(n) Time and O(1) Space
First find two elements arr[i] & arr[j] such that arr[i] < arr[j]. Then find a third element arr[k] greater than arr[j]. We can think of the problem in three simple terms.
- First we only need to find two elements arr[i] < arr[j] and i < j. This can be done in linear time with just 1 loop over the range of the array. For instance, while keeping track of the min element, its easy to find any subsequent element that is greater than it. Thus we have our arr[i] & arr[j].
- Secondly, consider this sequence - {3, 4, -1, 0, 2}. Initially min is 3, arr[i] is 3 and arr[j] is 4. While iterating over the array we can easily keep track of min and eventually update it to -1. And we can also update arr[i] & arr[j] to lower values i.e. -1 & 0 respectively.
- Thirdly, as soon as we have arr[i] & arr[j] values, we can immediately start monitoring the subsequent elements in the same loop for an arr[k] > arr[j]. Thus we can find all three values arr[i] < arr[j] < arr[k] in a single pass over the array.
Iterate over the length of the array. Keep track of the min. As soon as the next iteration has an element greater than min, we have found our arr[j] and the min will be saved as arr[i]. Continue iterating until we find an element arr[k] which is greater than arr[j]. In case the next elements are of lower value, then we update min, arr[i] and arr[j] to these lower values, so as to give us the best chance to find arr[k].
C++
#include <bits/stdc++.h>
using namespace std;
vector<int> find3Numbers(const vector<int>& arr)
{
// If number of elements < 3, no triplets are possible
if (arr.size() < 3) {
return {};
}
// track best sequence length (not current sequence length)
int seq = 1;
// min number in the array
int min_num = arr[0];
// least max number in best sequence
int max_seq = INT_MAX;
// save arr[i]
int store_min = min_num;
// Iterate from 1 to arr.size()
for (int i = 1; i < arr.size(); i++)
{
if (arr[i] == min_num)
continue;
else if (arr[i] < min_num)
{
min_num = arr[i];
continue;
}
// this condition is only hit
// when current sequence size is 2
else if (arr[i] < max_seq) {
max_seq = arr[i];
store_min = min_num;
}
// Increase best sequence length &
// save next number in triplet
else if (arr[i] > max_seq) {
return {store_min, max_seq, arr[i]};
}
}
// No triplet found
return {};
}
// Driver code
int main() {
vector<int> arr = {1, 2, -1, 7, 5};
vector<int> res = find3Numbers(arr);
for (int x : res) {
cout << x << " ";
}
return 0;
}
Java
// Java Program for above approach
import java.io.*;
public class Main
{
// Function to find the triplet
public static void find3Numbers(int[] nums)
{
// If number of elements < 3
// then no triplets are possible
if (nums.length < 3){
System.out.print("No such triplet found");
return;
}
// track best sequence length
// (not current sequence length)
int seq = 1;
// min number in array
int min_num = nums[0];
// least max number in best sequence
// i.e. track arr[j] (e.g. in
// array {1, 5, 3} our best sequence
// would be {1, 3} with arr[j] = 3)
int max_seq = Integer.MIN_VALUE;
// save arr[i]
int store_min = min_num;
// Iterate from 1 to nums.size()
for (int i = 1; i < nums.length; i++)
{
if (nums[i] == min_num)
continue;
else if (nums[i] < min_num)
{
min_num = nums[i];
continue;
}
// this condition is only hit
// when current sequence size is 2
else if (nums[i] < max_seq) {
// update best sequence max number
// to a smaller value
// (i.e. we've found a
// smaller value for arr[j])
max_seq = nums[i];
// store best sequence start value
// i.e. arr[i]
store_min = min_num;
}
// Increase best sequence length &
// save next number in our triplet
else if (nums[i] > max_seq)
{
seq++;
// We've found our arr[k]!
// Print the output
if (seq == 3)
{
System.out.println("Triplet: " + store_min +
", " + max_seq + ", " + nums[i]);
return;
}
max_seq = nums[i];
}
}
// No triplet found
System.out.print("No such triplet found");
}
// Driver program
public static void main(String[] args)
{
int[] nums = {1,2,-1,7,5};
// Function Call
find3Numbers(nums);
}
}
// This code is contributed by divyesh072019
Python
# Python3 Program for above approach
import sys
# Function to find the triplet
def find3Numbers(nums):
# If number of elements < 3
# then no triplets are possible
if (len(nums) < 3):
print("No such triplet found", end = '')
return
# Track best sequence length
# (not current sequence length)
seq = 1
# min number in array
min_num = nums[0]
# Least max number in best sequence
# i.e. track arr[j] (e.g. in
# array {1, 5, 3} our best sequence
# would be {1, 3} with arr[j] = 3)
max_seq = -sys.maxsize - 1
# Save arr[i]
store_min = min_num
# Iterate from 1 to nums.size()
for i in range(1, len(nums)):
if (nums[i] == min_num):
continue
elif (nums[i] < min_num):
min_num = nums[i]
continue
# This condition is only hit
# when current sequence size is 2
elif (nums[i] < max_seq):
# Update best sequence max number
# to a smaller value
# (i.e. we've found a
# smaller value for arr[j])
max_seq = nums[i]
# Store best sequence start value
# i.e. arr[i]
store_min = min_num
# Increase best sequence length &
# save next number in our triplet
elif (nums[i] > max_seq):
if seq == 1:
store_min = min_num
seq += 1
# We've found our arr[k]!
# Print the output
if (seq == 3):
print("Triplet: " + str(store_min) +
", " + str(max_seq) + ", " +
str(nums[i]))
return
max_seq = nums[i]
# No triplet found
print("No such triplet found", end = '')
# Driver Code
if __name__=='__main__':
nums = [ 1, 2, -1, 7, 5 ]
# Function Call
find3Numbers(nums)
# This code is contributed by rutvik_56
C#
// C# Program for above approach
using System;
class GFG {
// Function to find the triplet
static void find3Numbers(int[] nums)
{
// If number of elements < 3
// then no triplets are possible
if (nums.Length < 3){
Console.Write("No such triplet found");
return;
}
// track best sequence length
// (not current sequence length)
int seq = 1;
// min number in array
int min_num = nums[0];
// least max number in best sequence
// i.e. track arr[j] (e.g. in
// array {1, 5, 3} our best sequence
// would be {1, 3} with arr[j] = 3)
int max_seq = Int32.MinValue;
// save arr[i]
int store_min = min_num;
// Iterate from 1 to nums.size()
for (int i = 1; i < nums.Length; i++)
{
if (nums[i] == min_num)
continue;
else if (nums[i] < min_num)
{
min_num = nums[i];
continue;
}
// this condition is only hit
// when current sequence size is 2
else if (nums[i] < max_seq) {
// update best sequence max number
// to a smaller value
// (i.e. we've found a
// smaller value for arr[j])
max_seq = nums[i];
// store best sequence start value
// i.e. arr[i]
store_min = min_num;
}
// Increase best sequence length &
// save next number in our triplet
else if (nums[i] > max_seq)
{
seq++;
// We've found our arr[k]!
// Print the output
if (seq == 3)
{
Console.WriteLine("Triplet: " + store_min +
", " + max_seq + ", " + nums[i]);
return;
}
max_seq = nums[i];
}
}
// No triplet found
Console.Write("No such triplet found");
}
static void Main() {
int[] nums = {1,2,-1,7,5};
// Function Call
find3Numbers(nums);
}
}
// This code is contributed by divyeshrabadiya07
JavaScript
<script>
// Javascript program for above approach
// Function to find the triplet
function find3Numbers(nums)
{
// If number of elements < 3
// then no triplets are possible
if (nums.length < 3)
{
document.write("No such triplet found");
return;
}
// Track best sequence length
// (not current sequence length)
let seq = 1;
// min number in array
let min_num = nums[0];
// Least max number in best sequence
// i.e. track arr[j] (e.g. in
// array {1, 5, 3} our best sequence
// would be {1, 3} with arr[j] = 3)
let max_seq = Number.MIN_VALUE;
// Save arr[i]
let store_min = min_num;
// Iterate from 1 to nums.size()
for(let i = 1; i < nums.length; i++)
{
if (nums[i] == min_num)
continue;
else if (nums[i] < min_num)
{
min_num = nums[i];
continue;
}
// This condition is only hit
// when current sequence size is 2
else if (nums[i] < max_seq)
{
// Update best sequence max number
// to a smaller value
// (i.e. we've found a
// smaller value for arr[j])
max_seq = nums[i];
// Store best sequence start value
// i.e. arr[i]
store_min = min_num;
}
// Increase best sequence length &
// save next number in our triplet
else if (nums[i] > max_seq)
{
seq++;
// We've found our arr[k]!
// Print the output
if (seq == 3)
{
document.write("Triplet: " + store_min +
", " + max_seq + ", " +
nums[i]);
return;
}
max_seq = nums[i];
}
}
// No triplet found
document.write("No such triplet found");
}
// Driver code
let nums = [1, 2, -1, 7, 5];
// Function Call
find3Numbers(nums);
// This code is contributed by suresh07
</script>
Exercise:
- Find a sub-sequence of size 3 such that arr[i] < arr[j] > arr[k].
- Find a sorted sub-sequence of size 4 in linear time
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