3 Sum - Pythagorean Triplet in an array
Last Updated :
23 Jul, 2025
Given an array of positive integers, the task is to determine if a Pythagorean triplet exists in the given array. A triplet {a, b, c} is considered a Pythagorean triplet if it satisfies the condition a2 + b2 = c2.
Example:
Input: arr[] = [3, 1, 4, 6, 5]
Output: true
Explanation: The array contains a Pythagorean triplet {3, 4, 5}.
Input: arr[] = [10, 4, 6, 12, 5]
Output: false
Explanation: There is no Pythagorean triplet.
[Naive Approach] Explore all the triplets - O(n^3) Time and O(1) Space
The simplest approach is to explore all the triplets(a, b, c) using three nested loops and if any of them satisfies the condition of Pythagorean Triplet, that is a2 + b2 = c2 or a2 + c2 = b2 or b2 + c2 = a2, return true.
C++
// C++ program to find if a Pythagorean triplet exists
// by exploring all triplets
#include <iostream>
#include <vector>
using namespace std;
bool pythagoreanTriplet(vector<int> &arr) {
int n = arr.size();
// Exploring all possible triplets
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
// Calculate square of array elements
int x = arr[i] * arr[i];
int y = arr[j] * arr[j];
int z = arr[k] * arr[k];
// If these integers form Pythagorean triplet then
// return true
if (x == y + z || y == x + z || z == x + y)
return true;
}
}
}
return false;
}
int main() {
vector<int> arr = {3, 1, 4, 6, 5};
cout << (pythagoreanTriplet(arr) ? "true" : "false");
return 0;
}
C
// C program to find if a Pythagorean triplet exists
// by exploring all triplets
#include <stdio.h>
#include <stdbool.h>
bool pythagoreanTriplet(int arr[], int n) {
// Exploring all possible triplets
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
// Calculate square of array elements
int x = arr[i] * arr[i];
int y = arr[j] * arr[j];
int z = arr[k] * arr[k];
// If these integers form Pythagorean triplet then
// return true
if (x == y + z || y == x + z || z == x + y)
return true;
}
}
}
return false;
}
int main() {
int arr[] = {3, 1, 4, 6, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf(pythagoreanTriplet(arr, n) ? "true" : "false");
return 0;
}
Java
// Java program to find if a Pythagorean triplet exists
// by exploring all triplets
import java.util.*;
class GfG {
static boolean pythagoreanTriplet(int[] arr) {
int n = arr.length;
// Exploring all possible triplets
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
// Calculate square of array elements
int x = arr[i] * arr[i];
int y = arr[j] * arr[j];
int z = arr[k] * arr[k];
// If these integers form Pythagorean triplet then
// return true
if (x == y + z || y == x + z || z == x + y)
return true;
}
}
}
return false;
}
public static void main(String[] args) {
int[] arr = {3, 1, 4, 6, 5};
System.out.println(pythagoreanTriplet(arr));
}
}
Python
# Python program to find if a Pythagorean triplet exists
# by exploring all triplets
def pythagoreanTriplet(arr):
n = len(arr)
# Exploring all possible triplets
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
# Calculate square of array elements
x = arr[i] ** 2
y = arr[j] ** 2
z = arr[k] ** 2
# If these integers form Pythagorean triplet then
# return true
if x == y + z or y == x + z or z == x + y:
return True
return False
if __name__ == "__main__":
arr = [3, 1, 4, 6, 5]
if pythagoreanTriplet(arr):
print("true")
else :
print("false")
C#
// C# program to find if a Pythagorean triplet exists
// by exploring all triplets
using System;
class GfG {
static bool pythagoreanTriplet(int[] arr) {
int n = arr.Length;
// Exploring all possible triplets
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
for (int k = j + 1; k < n; k++) {
// Calculate square of array elements
int x = arr[i] * arr[i];
int y = arr[j] * arr[j];
int z = arr[k] * arr[k];
// If these integers form Pythagorean triplet then
// return true
if (x == y + z || y == x + z || z == x + y)
return true;
}
}
}
return false;
}
static void Main() {
int[] arr = {3, 1, 4, 6, 5};
Console.WriteLine(pythagoreanTriplet(arr)?"true":"false");
}
}
JavaScript
// JavaScript program to find if a Pythagorean triplet exists
// by exploring all triplets
function pythagoreanTriplet(arr) {
let n = arr.length;
// Exploring all possible triplets
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
for (let k = j + 1; k < n; k++) {
// Calculate square of array elements
let x = arr[i] * arr[i];
let y = arr[j] * arr[j];
let z = arr[k] * arr[k];
// If these integers form Pythagorean triplet then
// return true
if (x == y + z || y == x + z || z == x + y)
return true;
}
}
}
return false;
}
let arr = [3, 1, 4, 6, 5];
console.log(pythagoreanTriplet(arr));
[Better Approach-1] Two Pointers Technique - O(n^2) Time and O(1) Space
The idea is to square each element of the array and sort it in ascending order. Then, we will traverse the array in reverse and fix the largest element of the triplet as c2. After that, we will use two-pointers technique to find two elements a2 and b2 in remaining array such that a2 + b2 = c2. We initialize two pointers at both ends of the remaining portion of the array (from index 0 to i−1) and adjust them based on the sum of the elements at both pointers as follows:
- If sum = c2, we have found the Pythagorean triplet.
- If sum < c2, we move the left pointer towards right.
- If sum > c2, we move the right pointer towards left.
C++
// C++ program to find if a Pythagorean triplet
// exists using the two-pointer technique
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool pythagoreanTriplet(vector<int> &arr) {
int n = arr.size();
// Taking Square of each element
for (int i = 0; i < n; i++)
arr[i] = arr[i] * arr[i];
sort(arr.begin(), arr.end());
// fix the largest element of Pythagorean triplet
for (int i = n - 1; i > 1; i--) {
// Two pointer technique to find remaining two
// elements such that a^2 + b^2 = c^2
int l = 0;
int r = i - 1;
while (l < r) {
// A Pythagorean triplet is found
if (arr[l] + arr[r] == arr[i])
return true;
if (arr[l] + arr[r] < arr[i])
l++;
else
r--;
}
}
return false;
}
int main() {
vector<int> arr = {3, 1, 4, 6, 5};
cout << (pythagoreanTriplet(arr) ? "true" : "false");
return 0;
}
C
// C program to find if a Pythagorean triplet
// exists using the two-pointer technique
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
bool pythagoreanTriplet(int arr[], int n) {
// Taking Square of each element
for (int i = 0; i < n; i++)
arr[i] = arr[i] * arr[i];
// Sort the array
qsort(arr, n, sizeof(int), compare);
// Fix the largest element of Pythagorean triplet
for (int i = n - 1; i > 1; i--) {
// Two pointer technique to find remaining two
// elements such that a^2 + b^2 = c^2
int l = 0;
int r = i - 1;
while (l < r) {
// A Pythagorean triplet is found
if (arr[l] + arr[r] == arr[i])
return true;
if (arr[l] + arr[r] < arr[i])
l++;
else
r--;
}
}
return false;
}
int main() {
int arr[] = {3, 1, 4, 6, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf(pythagoreanTriplet(arr, n) ? "true" : "false");
return 0;
}
Java
// Java program to find if a Pythagorean triplet
// exists using the two-pointer technique
import java.util.Arrays;
class GfG {
static boolean pythagoreanTriplet(int[] arr) {
int n = arr.length;
// Taking Square of each element
for (int i = 0; i < n; i++)
arr[i] = arr[i] * arr[i];
Arrays.sort(arr);
// Fix the largest element of Pythagorean triplet
for (int i = n - 1; i > 1; i--) {
// Two pointer technique to find remaining two
// elements Such that a^2 + b^2 = c^2
int l = 0;
int r = i - 1;
while (l < r) {
// A Pythagorean triplet is found
if (arr[l] + arr[r] == arr[i])
return true;
if (arr[l] + arr[r] < arr[i])
l++;
else
r--;
}
}
return false;
}
public static void main(String[] args) {
int[] arr = {3, 1, 4, 6, 5};
System.out.println(pythagoreanTriplet(arr));
}
}
Python
# Python program to find if a Pythagorean triplet
# exists using the two-pointer technique
def pythagoreanTriplet(arr):
n = len(arr)
# Taking Square of each element
for i in range(n):
arr[i] = arr[i] * arr[i]
arr.sort()
# Fix the largest element of Pythagorean triplet
for i in range(n - 1, 1, -1):
# Two pointer technique to find remaining two
# elements such that a^2 + b^2 = c^2
l = 0
r = i - 1
while l < r:
# A Pythagorean triplet is found
if arr[l] + arr[r] == arr[i]:
return True
if arr[l] + arr[r] < arr[i]:
l += 1
else:
r -= 1
return False
if __name__ == "__main__":
arr = [3, 1, 4, 6, 5]
print("true" if pythagoreanTriplet(arr) else "false")
C#
// C# program to find if a Pythagorean triplet
// exists using the two-pointer technique
using System;
class GfG {
static bool pythagoreanTriplet(int[] arr) {
int n = arr.Length;
// Taking Square of each element
for (int i = 0; i < n; i++)
arr[i] = arr[i] * arr[i];
Array.Sort(arr);
// Fix the largest element of Pythagorean triplet
for (int i = n - 1; i > 1; i--) {
// Two pointer technique to find remaining two elements
// such that a^2 + b^2 = c^2
int l = 0;
int r = i - 1;
while (l < r) {
// A Pythagorean triplet is found
if (arr[l] + arr[r] == arr[i])
return true;
if (arr[l] + arr[r] < arr[i])
l++;
else
r--;
}
}
return false;
}
static void Main() {
int[] arr = {3, 1, 4, 6, 5};
Console.WriteLine(pythagoreanTriplet(arr)?"true":"false");
}
}
JavaScript
// JavaScript program to find if a Pythagorean triplet
// exists using the two-pointer technique
function pythagoreanTriplet(arr) {
let n = arr.length;
// Taking Square of each element
for (let i = 0; i < n; i++)
arr[i] = arr[i] * arr[i];
arr.sort((a, b) => a - b);
// Fix the largest element of Pythagorean triplet
for (let i = n - 1; i > 1; i--) {
// Two pointer technique to find remaining two
// elements such that a^2 + b^2 = c^2
let l = 0;
let r = i - 1;
while (l < r) {
// A Pythagorean triplet is found
if (arr[l] + arr[r] === arr[i])
return true;
if (arr[l] + arr[r] < arr[i])
l++;
else
r--;
}
}
return false;
}
// Driver Code
const arr = [3, 1, 4, 6, 5];
console.log(pythagoreanTriplet(arr));
[Better Approach-2] Using Hashing - O(n^2) Time and O(n) Space
First, we will insert all the elements of the given array into a hash set. Then, using two nested loops, we will iterate through all possible combinations of a and b, and check if there exists a third value c that forms a Pythagorean triplet with a and b. If such c exists, we will return true.
C++
// C++ program to find if a Pythagorean triplet exists
// using hashing
#include <iostream>
#include <vector>
#include <unordered_set>
#include <math.h>
using namespace std;
bool pythagoreanTriplet(vector<int> &arr) {
int n = arr.size();
unordered_set<int> st;
for (int i = 0; i < n; i++)
st.insert(arr[i]);
// Iterate through all possible values of a and b
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int a = arr[i];
int b = arr[j];
// calculate required value for
// c to form Pythagorean triplet
int c = sqrt(a * a + b * b);
// First, verify if c^2 is a perfect square (indicating a
// valid c) and check if this c exists in the set.
if (c*c == a*a + b*b && st.find(c) != st.end())
return true;
}
}
// No Pythagorean triplet exists in the array
return false;
}
int main() {
vector<int> arr = {3, 1, 4, 6, 5};
cout << (pythagoreanTriplet(arr) ? "true" : "false");
return 0;
}
Java
// Java program to find if a Pythagorean triplet exists
// using hashing
import java.util.HashSet;
class GfG {
static boolean pythagoreanTriplet(int[] arr) {
int n = arr.length;
HashSet<Integer> st = new HashSet<>();
for (int i = 0; i < n; i++)
st.add(arr[i]);
// Iterate through all possible values of a and b
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int a = arr[i];
int b = arr[j];
// calculate required value for
// c to form Pythagorean triplet
int c = (int)Math.sqrt(a * a + b * b);
// First, verify if c^2 is a perfect square (indicating a
// valid c) and check if this c exists in the set.
if (c*c == a*a + b*b && st.contains(c))
return true;
}
}
// No Pythagorean triplet exists in the array
return false;
}
public static void main(String[] args) {
int[] arr = {3, 1, 4, 6, 5};
System.out.println(pythagoreanTriplet(arr));
}
}
Python
# Python program to find if a Pythagorean triplet exists
# using hashing
def pythagoreanTriplet(arr):
n = len(arr)
st = set()
for i in range(n):
st.add(arr[i])
# Iterate through all possible values of a and b
for i in range(n - 1):
for j in range(i + 1, n):
a = arr[i]
b = arr[j]
# calculate required value for
# c to form Pythagorean triplet
c = (int)((a ** 2 + b ** 2) ** 0.5)
# First, verify if c^2 is a perfect square (indicating a
# valid c) and check if this c exists in the set.
if (c*c == a*a + b*b) and (c in st):
return True
# No Pythagorean triplet exists in the array
return False
if __name__ == "__main__":
arr = [3, 1, 4, 6, 5]
print("true" if pythagoreanTriplet(arr) else "false")
C#
// C# program to find if a Pythagorean triplet exists
// using hashing
using System;
using System.Collections.Generic;
class GfG {
static bool pythagoreanTriplet(int[] arr) {
HashSet<int> st = new HashSet<int>();
int n = arr.Length;
for (int i = 0; i < n; i++)
st.Add(arr[i]);
// Iterate through all possible values of a and b
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int a = arr[i];
int b = arr[j];
// calculate required value for
// c to form Pythagorean triplet
int c = (int)Math.Sqrt(a * a + b * b);
// First, verify if c^2 is a perfect square (indicating a
// valid c) and check if this c exists in the set.
if (c*c == a*a + b*b && st.Contains(c))
return true;
}
}
// No Pythagorean triplet exists in the array
return false;
}
static void Main() {
int[] arr = {3, 1, 4, 6, 5};
Console.WriteLine(pythagoreanTriplet(arr)?"true":"false");
}
}
JavaScript
// JavaScript program to find if a Pythagorean triplet exists
// using hashing
function pythagoreanTriplet(arr) {
const st = new Set();
const n = arr.length;
for (let i = 0; i < n; i++)
st.add(arr[i]);
// Iterate through all possible values of a and b
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
const a = arr[i]
const b = arr[j]
// calculate required value for
// c to form Pythagorean triplet
const c = Math.sqrt(a * a + b * b);
// First, verify if c^2 is a perfect square (indicating a
// valid c) and check if this c exists in the set.
if (c*c === a*a + b*b && st.has(c))
return true;
}
}
// No Pythagorean triplet exists in the array
return false;
}
const arr = [3, 1, 4, 6, 5];
console.log(pythagoreanTriplet(arr) ? "true" : "false");
[Expected Approach] Iterating till max element - O(max(arr)^2) Time and O(max(arr)) Space
The idea is to generate all possible pairs (a, b) within the range of maximum element in the input array using nested loops. For each pair, calculate the value of c required to form a Pythagorean Triplet and check if c exists in the input array. We can check if c exists or not in constant time by marking all the elements of input array in a visited array.
Here, we only need to mark the elements and not store their frequency because for all valid triplets of positive integers (a, b, c), no two elements can be equal, that is a != b, a != c and b != c.
Note: This approach is more efficient than the previous ones, given the constraint (max(arr) < n).
C++
// C++ program to find if a Pythagorean triplet
// exists by iterating till max element
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
bool pythagoreanTriplet(vector<int> &arr) {
int n = arr.size();
int maxEle = 0;
for (int ele: arr)
maxEle = max(maxEle, ele);
// Visited array to mark the elements
vector<bool> vis(maxEle + 1, 0);
// Marking each element of input array
for (int ele: arr)
vis[ele] = true;
// Iterate for all possible a
for (int a = 1; a < maxEle + 1; a++) {
// If a is not there
if (vis[a] == false)
continue;
// Iterate for all possible b
for (int b = 1; b < maxEle + 1; b++) {
// If b is not there
if (vis[b] == false)
continue;
// calculate c value to form a pythagorean triplet
int c = sqrt(a * a + b * b);
// If c^2 is not a perfect square or c exceeds the
// maximum value
if ((c * c) != (a * a + b * b) || c > maxEle)
continue;
// If there exists c in the original array,
// we have found the triplet
if (vis[c] == true) {
return true;
}
}
}
return false;
}
int main() {
vector<int> arr = {3, 1, 4, 6, 5};
cout << (pythagoreanTriplet(arr) ? "true" : "false");
return 0;
}
Java
// Java program to find if a Pythagorean triplet
// exists by iterating till max element
import java.util.Arrays;
class GfG {
static boolean pythagoreanTriplet(int[] arr) {
int n = arr.length;
int maxEle = 0;
for (int ele : arr)
maxEle = Math.max(maxEle, ele);
// Visited array to mark the elements
boolean[] vis = new boolean[maxEle + 1];
// Marking each element of input array
for (int ele : arr)
vis[ele] = true;
// Iterate for all possible a
for (int a = 1; a < maxEle + 1; a++) {
// If a is not there
if (!vis[a])
continue;
// Iterate for all possible b
for (int b = 1; b < maxEle + 1; b++) {
// If b is not there
if (!vis[b])
continue;
// calculate c value to form a pythagorean triplet
int c = (int) Math.sqrt(a * a + b * b);
// If c^2 is not a perfect square or c exceeds the
// maximum value
if ((c * c) != (a * a + b * b) || c > maxEle)
continue;
// If there exists c in the original array,
// we have found the triplet
if (vis[c]) {
return true;
}
}
}
return false;
}
public static void main(String[] args) {
int[] arr = {3, 1, 4, 6, 5};
System.out.println(pythagoreanTriplet(arr));
}
}
Python
# Python program to find if a Pythagorean triplet
# exists by iterating till max element
import math
def pythagoreanTriplet(arr):
n = len(arr)
maxEle = 0
for ele in arr:
maxEle = max(maxEle, ele)
# Visited array to mark the elements
vis = [False] * (maxEle + 1)
# Marking each element of input array
for ele in arr:
vis[ele] = True
# Iterate for all possible a
for a in range(1, maxEle + 1):
# If a is not there
if not vis[a]:
continue
# Iterate for all possible b
for b in range(1, maxEle + 1):
# If b is not there
if not vis[b]:
continue
# calculate c value to form a pythagorean triplet
c = int(math.sqrt(a * a + b * b))
# If c^2 is not a perfect square or c exceeds the
# maximum value
if (c * c) != (a * a + b * b) or c > maxEle:
continue
# If there exists c in the original array,
# we have found the triplet
if vis[c]:
return True
return False
if __name__ == "__main__":
arr = [3, 1, 4, 6, 5]
ans = pythagoreanTriplet(arr)
if ans:
print("true")
else :
print("false")
C#
// C# program to find if a Pythagorean triplet
// exists by iterating till max element
using System;
using System.Collections.Generic;
class GfG {
static bool pythagoreanTriplet(int[] arr) {
int n = arr.Length;
int maxEle = 0;
foreach (int ele in arr)
maxEle = Math.Max(maxEle, ele);
// Visited array to mark the elements
bool[] vis = new bool[maxEle + 1];
// Marking each element of input array
foreach (int ele in arr)
vis[ele] = true;
// Iterate for all possible a
for (int a = 1; a < maxEle + 1; a++) {
// If a is not there
if (vis[a] == false)
continue;
// Iterate for all possible b
for (int b = 1; b < maxEle + 1; b++) {
// If b is not there
if (vis[b] == false)
continue;
// calculate c value to form a pythagorean triplet
int c = (int)Math.Sqrt(a * a + b * b);
// If c^2 is not a perfect square or c exceeds the
// maximum value
if ((c * c) != (a * a + b * b) || c > maxEle)
continue;
// If there exists c in the original array,
// we have found the triplet
if (vis[c] == true)
return true;
}
}
return false;
}
static void Main() {
int[] arr = { 3, 1, 4, 6, 5 };
bool ans = pythagoreanTriplet(arr);
if (ans)
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
JavaScript
// JavaScript program to find if a Pythagorean triplet
// exists by iterating till max element
function pythagoreanTriplet(arr) {
let n = arr.length;
let maxEle = 0;
for (let ele of arr)
maxEle = Math.max(maxEle, ele);
// Visited array to mark the elements
let vis = new Array(maxEle + 1).fill(false);
// Marking each element of input array
for (let ele of arr)
vis[ele] = true;
// Iterate for all possible a
for (let a = 1; a < maxEle + 1; a++) {
// If a is not there
if (vis[a] === false)
continue;
// Iterate for all possible b
for (let b = 1; b < maxEle + 1; b++) {
// If b is not there
if (vis[b] === false)
continue;
// calculate c value to form a pythagorean triplet
let c = Math.floor(Math.sqrt(a * a + b * b));
// If c^2 is not a perfect square or c exceeds the
// maximum value
if ((c * c) !== (a * a + b * b) || c > maxEle)
continue;
// If there exists c in the original array,
// we have found the triplet
if (vis[c] === true) {
return true;
}
}
}
return false;
}
const arr = [3, 1, 4, 6, 5];
console.log(pythagoreanTriplet(arr));
Pythagorean Triplet | DSA Problem
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