Majority Element II - Elements occurring more than ⌊n/3⌋ times
Last Updated :
26 Jul, 2025
Given an array arr[] consisting of n integers, find all the array elements which occurs more than floor(n/3) times.
Note: The returned array of majority elements should be sorted.
Examples:
Input: arr[] = [2, 2, 3, 1, 3, 2, 1, 1]
Output: [1, 2]
Explanation: The frequency of 1 and 2 is 3, which is more than floor n/3 (8/3 = 2).
Input: arr[] = [-5, 3, -5]
Output: [-5]
Explanation: The frequency of -5 is 2, which is more than floor n/3 (3/3 = 1).
Input: arr[] = [3, 2, 2, 4, 1, 4]
Output: [ ]
Explanation: There is no majority element.
[Naive Approach] Using Nested Loops - O(n^2) Time and O(1) Space
The idea is to iterate over all elements and count the frequency of the element in the array. If the frequency of the element is greater than floor(n/3), add it to the result. To avoid adding duplicate elements into the result, we can check if the element is already present in the result. We can stop the iteration if we have already found two majority elements.
C++
#include <iostream>
#include <vector>
using namespace std;
vector<int> findMajority(vector<int> &arr) {
int n = arr.size();
vector<int> res;
for (int i = 0; i < n; i++) {
// Count the frequency of arr[i]
int cnt = 0;
for (int j = i; j < n; j++) {
if (arr[j] == arr[i])
cnt += 1;
}
// Check if arr[i] is a majority element
if (cnt > (n / 3)) {
// Add arr[i] only if it is not already
// present in the result
if (res.size() == 0 || arr[i] != res[0]) {
res.push_back(arr[i]);
}
}
// If we have found two majority elements,
// we can stop our search
if (res.size() == 2) {
if(res[0] > res[1])
swap(res[0], res[1]);
break;
}
}
return res;
}
int main() {
vector<int> arr = {2, 2, 3, 1, 3, 2, 1, 1};
vector<int> res = findMajority(arr);
for (int ele : res)
cout << ele << " ";
return 0;
}
C
#include <stdio.h>
int *findMajority(int *arr, int n, int *resSize) {
int *res = (int *)malloc(2 * sizeof(int));
*resSize = 0;
for (int i = 0; i < n; i++) {
// Count the frequency of arr[i]
int cnt = 0;
for (int j = i; j < n; j++) {
if (arr[j] == arr[i])
cnt += 1;
}
// Check if arr[i] is a majority element
if (cnt > (n / 3)) {
// Add arr[i] only if it is not already
// present in the result
if (*resSize == 0 || arr[i] != res[0]) {
res[*resSize] = arr[i];
(*resSize)++;
}
}
// If we have found two majority elements,
// we can stop our search
if (*resSize == 2) {
if (res[0] > res[1]) {
int temp = res[0];
res[0] = res[1];
res[1] = temp;
}
break;
}
}
return res;
}
int main() {
int arr[] = {2, 2, 3, 1, 3, 2, 1, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int resSize;
int *res = findMajority(arr, n, &resSize);
for (int i = 0; i < resSize; i++)
printf("%d ", res[i]);
return 0;
}
Java
import java.util.ArrayList;
class GfG {
static ArrayList<Integer> findMajority(int[] arr) {
int n = arr.length;
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
// Count the frequency of arr[i]
int cnt = 0;
for (int j = i; j < n; j++) {
if (arr[j] == arr[i])
cnt += 1;
}
// Check if arr[i] is a majority element
if (cnt > (n / 3)) {
// Add arr[i] only if it is not already present
if (res.size() == 0 || arr[i] != res.get(0)) {
res.add(arr[i]);
}
}
// If we have found two majority elements,
// we can stop our search
if (res.size() == 2) {
if (res.get(0) > res.get(1))
java.util.Collections.swap(res, 0, 1);
break;
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {2, 2, 3, 1, 3, 2, 1, 1};
ArrayList<Integer> res = findMajority(arr);
for (int ele : res)
System.out.print(ele + " ");
}
}
Python
def findMajority(arr):
n = len(arr)
res = []
for i in range(n):
# Count the frequency of arr[i]
cnt = 0
for j in range(i, n):
if arr[j] == arr[i]:
cnt += 1
# Check if arr[i] is a majority element
if cnt > (n // 3):
# Add arr[i] only if it is not already
# present in the result
if len(res) == 0 or arr[i] != res[0]:
res.append(arr[i])
# If we have found two majority elements,
# we can stop our search
if len(res) == 2:
if res[0] > res[1]:
res[0], res[1] = res[1], res[0]
break
return res
if __name__ == "__main__":
arr = [2, 2, 3, 1, 3, 2, 1, 1]
res = findMajority(arr)
for ele in res:
print(ele, end=" ")
C#
using System;
using System.Collections.Generic;
class GfG {
static List<int> findMajority(int[] arr) {
int n = arr.Length;
List<int> res = new List<int>();
for (int i = 0; i < n; i++) {
// Count the frequency of arr[i]
int cnt = 0;
for (int j = i; j < n; j++) {
if (arr[j] == arr[i])
cnt += 1;
}
// Check if arr[i] is a majority element
if (cnt > (n / 3)) {
// Add arr[i] only if it is not already
// present in the result
if (res.Count == 0 || arr[i] != res[0]) {
res.Add(arr[i]);
}
}
// If we have found two majority elements,
// we can stop our search
if (res.Count == 2) {
if (res[0] > res[1]) {
int temp = res[0];
res[0] = res[1];
res[1] = temp;
}
break;
}
}
return res;
}
static void Main(string[] args) {
int[] arr = { 2, 2, 3, 1, 3, 2, 1, 1 };
List<int> res = findMajority(arr);
foreach (int ele in res)
Console.Write(ele + " ");
}
}
JavaScript
function findMajority(arr) {
const n = arr.length;
const res = [];
for (let i = 0; i < n; i++) {
// Count the frequency of arr[i]
let cnt = 0;
for (let j = i; j < n; j++) {
if (arr[j] === arr[i]) {
cnt += 1;
}
}
// Check if arr[i] is a majority element
if (cnt > (n / 3)) {
// Add arr[i] only if it is not already
// present in the result
if (res.length === 0 || arr[i] !== res[0]) {
res.push(arr[i]);
}
}
// If we have found two majority elements,
// we can stop our search
if (res.length === 2) {
if (res[0] > res[1]) {
[res[0], res[1]] = [res[1], res[0]];
}
break;
}
}
return res;
}
// Driver Code
const arr = [2, 2, 3, 1, 3, 2, 1, 1];
const res = findMajority(arr);
console.log(res.join(" "));
[Better Approach] Using Hash Map or Dictionary - O(n) Time and O(n) Space
The idea is to use a hash map or dictionary to count the frequency of each element in the array. After counting, iterate over the hash map and if the frequency of any element is greater than (n/3), push it into the result. Finally, the majority elements are returned after sorting.
C++
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
vector<int> findMajority(vector<int> &arr) {
int n = arr.size();
unordered_map<int, int> freq;
vector<int> res;
// find frequency of each number
for (int ele : arr)
freq[ele]++;
// Iterate over each key-value pair
// in the hash map
for (auto it : freq) {
int ele = it.first;
int cnt = it.second;
// Add the element to the result, if its frequency
// if greater than floor(n/3)
if (cnt > n / 3)
res.push_back(ele);
}
if (res.size() == 2 && res[0] > res[1])
swap(res[0], res[1]);
return res;
}
int main() {
vector<int> arr = {2, 2, 3, 1, 3, 2, 1, 1};
vector<int> res = findMajority(arr);
for (auto ele : res) {
cout << ele << " ";
}
return 0;
}
Java
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;
class GfG {
static ArrayList<Integer> findMajority(int[] arr) {
int n = arr.length;
HashMap<Integer, Integer> freq = new HashMap<>();
ArrayList<Integer> res = new ArrayList<>();
// find frequency of each number
for (int ele : arr)
freq.put(ele, freq.getOrDefault(ele, 0) + 1);
// Iterate over each key-value
// pair in the hash map
for (Map.Entry<Integer, Integer> it : freq.entrySet()) {
int ele = it.getKey();
int cnt = it.getValue();
// Add the element to the result if its
// frequency is greater than n / 3
if (cnt > n / 3)
res.add(ele);
}
// Sort result if there are two elements
// and they are out of order
if (res.size() == 2 && res.get(0) > res.get(1)) {
int temp = res.get(0);
res.set(0, res.get(1));
res.set(1, temp);
}
return res;
}
public static void main(String[] args) {
int[] arr = {2, 2, 3, 1, 3, 2, 1, 1};
ArrayList<Integer> res = findMajority(arr);
for (int ele : res) {
System.out.print(ele + " ");
}
}
}
Python
def findMajority(arr):
n = len(arr)
freq = {}
res = []
# find frequency of each number
for ele in arr:
freq[ele] = freq.get(ele, 0) + 1
# Iterate over each key-value
# pair in the hash map
for ele, cnt in freq.items():
# Add the element to the result, if its frequency
# is greater than floor(n/3)
if cnt > n // 3:
res.append(ele)
if len(res) == 2 and res[0] > res[1]:
res[0], res[1] = res[1], res[0]
return res
if __name__ == "__main__":
arr = [2, 2, 3, 1, 3, 2, 1, 1]
res = findMajority(arr)
for ele in res:
print(ele, end=" ")
C#
using System;
using System.Collections.Generic;
class GfG {
static List<int> findMajority(int[] arr) {
int n = arr.Length;
Dictionary<int, int> freq = new Dictionary<int, int>();
List<int> res = new List<int>();
// find frequency of each number
foreach (int ele in arr) {
if (freq.ContainsKey(ele))
freq[ele]++;
else
freq[ele] = 1;
}
// Iterate over each key-value
// pair in the hash map
foreach (var it in freq) {
int ele = it.Key;
int cnt = it.Value;
// Add the element to the result, if its frequency
// is greater than floor(n/3)
if (cnt > n / 3)
res.Add(ele);
}
if (res.Count == 2 && res[0] > res[1]) {
int temp = res[0];
res[0] = res[1];
res[1] = temp;
}
return res;
}
static void Main() {
int[] arr = { 2, 2, 3, 1, 3, 2, 1, 1 };
List<int> res = findMajority(arr);
foreach (int ele in res) {
Console.Write(ele + " ");
}
}
}
JavaScript
function findMajority(arr) {
const n = arr.length;
const freq = {};
const res = [];
// find frequency of each number
for (const ele of arr) {
freq[ele] = (freq[ele] || 0) + 1;
}
// Iterate over each key-value pair in the hash map
for (const it in freq) {
const ele = Number(it);
const cnt = freq[it];
// Add the element to the result, if its frequency
// is greater than floor(n/3)
if (cnt > Math.floor(n / 3)) {
res.push(ele);
}
}
if (res.length === 2 && res[0] > res[1]) {
[res[0], res[1]] = [res[1], res[0]];
}
return res;
}
// Driver Code
const arr = [2, 2, 3, 1, 3, 2, 1, 1];
const res = findMajority(arr);
console.log(res.join(" "));
[Expected Approach] Boyer-Moore’s Voting Algorithm - O(n) Time and O(1) Space
The idea is based on the observation that there can be at most two majority elements, which appear more than n/3 times. so we can use Boyer-Moore’s Voting algorithm. As we iterate the array, We identify potential majority elements by keeping track of two candidates and their respective counts.
Steps:
- Initialize two variables ele1 = -1 and ele2 = -1, for candidates and two variables cnt1 = 0 and cnt2 = 0, for counting.
- In each iteration,
- If an element is equal to any candidate, update that candidate's count.
- If count of a candidate reaches zero then replace that candidate with current element.
- If neither candidate matches and both counts are non zero, decrement the counts.
- After this, in second pass we check if the chosen candidates appear more than n/3 times in the array. If they do then include them in result array.
Since any element than appears more than floor(n/3) times, will dominate over elements that appear less frequently. Whenever we encounter a different element, we decrement the count of both the candidates. This maintains at most two candidates in the array.
C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;
vector<int> findMajority(vector<int> &arr) {
int n = arr.size();
// Initialize two candidates and their counts
int ele1 = -1, ele2 = -1, cnt1 = 0, cnt2 = 0;
for (int ele : arr) {
// Increment count for candidate 1
if (ele1 == ele) {
cnt1++;
}
// Increment count for candidate 2
else if (ele2 == ele) {
cnt2++;
}
// New candidate 1 if count is zero
else if (cnt1 == 0) {
ele1 = ele;
cnt1++;
}
// New candidate 2 if count is zero
else if (cnt2 == 0) {
ele2 = ele;
cnt2++;
}
// Decrease counts if neither candidate
else {
cnt1--;
cnt2--;
}
}
vector<int> res;
cnt1 = 0;
cnt2 = 0;
// Count the occurrences of candidates
for (int ele : arr) {
if (ele1 == ele) cnt1++;
if (ele2 == ele) cnt2++;
}
// Add to result if they are majority elements
if (cnt1 > n / 3) res.push_back(ele1);
if (cnt2 > n / 3 && ele1 != ele2) res.push_back(ele2);
if(res.size() == 2 && res[0] > res[1])
swap(res[0], res[1]);
return res;
}
int main() {
vector<int> arr = {2, 2, 3, 1, 3, 2, 1, 1};
vector<int> res = findMajority(arr);
for (int ele : res) {
cout << ele << " ";
}
return 0;
}
C
#include <stdio.h>
#include <limits.h>
void findMajority(int arr[], int n, int *res, int *resSize) {
// Initialize two candidates and their counts
int ele1 = -1, ele2 = -1, cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) {
int ele = arr[i];
// Increment count for candidate 1
if (ele1 == ele) {
cnt1++;
}
// Increment count for candidate 2
else if (ele2 == ele) {
cnt2++;
}
// New candidate 1 if count is zero
else if (cnt1 == 0) {
ele1 = ele;
cnt1++;
}
// New candidate 2 if count is zero
else if (cnt2 == 0) {
ele2 = ele;
cnt2++;
}
// Decrease counts if neither candidate
else {
cnt1--;
cnt2--;
}
}
cnt1 = 0;
cnt2 = 0;
// Count the occurrences of candidates
for (int i = 0; i < n; i++) {
int ele = arr[i];
if (ele1 == ele) cnt1++;
if (ele2 == ele) cnt2++;
}
// Add to result if they are majority elements
*resSize = 0;
if (cnt1 > n / 3) res[(*resSize)++] = ele1;
if (cnt2 > n / 3 && ele1 != ele2) res[(*resSize)++] = ele2;
// Sort the result if there are two elements
if (*resSize == 2 && res[0] > res[1]) {
int temp = res[0];
res[0] = res[1];
res[1] = temp;
}
}
int main() {
int arr[] = {2, 2, 3, 1, 3, 2, 1, 1};
int n = sizeof(arr) / sizeof(arr[0]);
int res[2];
int resSize;
findMajority(arr, n, res, &resSize);
for (int i = 0; i < resSize; i++) {
printf("%d ", res[i]);
}
return 0;
}
Java
import java.util.ArrayList;
class GfG {
// Function to find Majority element in an array
static ArrayList<Integer> findMajority(int[] arr) {
int n = arr.length;
// Initialize two candidates and their counts
int ele1 = -1, ele2 = -1;
int cnt1 = 0, cnt2 = 0;
for (int ele : arr) {
// Increment count for candidate 1
if (ele1 == ele) {
cnt1++;
}
// Increment count for candidate 2
else if (ele2 == ele) {
cnt2++;
}
// New candidate 1 if count is zero
else if (cnt1 == 0) {
ele1 = ele;
cnt1++;
}
// New candidate 2 if count is zero
else if (cnt2 == 0) {
ele2 = ele;
cnt2++;
}
// Decrease counts if neither candidate
else {
cnt1--;
cnt2--;
}
}
ArrayList<Integer> res = new ArrayList<>();
cnt1 = 0;
cnt2 = 0;
// Count the occurrences of candidates
for (int ele : arr) {
if (ele1 == ele) cnt1++;
if (ele2 == ele) cnt2++;
}
// Add to result if they are majority elements
if (cnt1 > n / 3) res.add(ele1);
if (cnt2 > n / 3 && ele1 != ele2) res.add(ele2);
// Sort the result if needed
if (res.size() == 2 && res.get(0) > res.get(1)) {
int temp = res.get(0);
res.set(0, res.get(1));
res.set(1, temp);
}
return res;
}
public static void main(String[] args) {
int[] arr = {2, 2, 3, 1, 3, 2, 1, 1};
ArrayList<Integer> res = findMajority(arr);
for (int ele : res) {
System.out.print(ele + " ");
}
}
}
Python
def findMajority(arr):
n = len(arr)
# Initialize two candidates and their counts
ele1, ele2 = -1, -1
cnt1, cnt2 = 0, 0
for ele in arr:
# Increment count for candidate 1
if ele1 == ele:
cnt1 += 1
# Increment count for candidate 2
elif ele2 == ele:
cnt2 += 1
# New candidate 1 if count is zero
elif cnt1 == 0:
ele1 = ele
cnt1 += 1
# New candidate 2 if count is zero
elif cnt2 == 0:
ele2 = ele
cnt2 += 1
# Decrease counts if neither candidate
else:
cnt1 -= 1
cnt2 -= 1
res = []
cnt1, cnt2 = 0, 0
# Count the occurrences of candidates
for ele in arr:
if ele1 == ele:
cnt1 += 1
if ele2 == ele:
cnt2 += 1
# Add to result if they are majority elements
if cnt1 > n / 3:
res.append(ele1)
if cnt2 > n / 3 and ele1 != ele2:
res.append(ele2)
if len(res) == 2 and res[0] > res[1]:
res[0], res[1] = res[1], res[0]
return res
if __name__ == "__main__":
arr = [2, 2, 3, 1, 3, 2, 1, 1]
res = findMajority(arr)
for ele in res:
print(ele, end = " ")
C#
using System;
using System.Collections.Generic;
class GfG {
static List<int> findMajority(int[] arr) {
int n = arr.Length;
// Initialize two candidates and their counts
int ele1 = -1, ele2 = -1, cnt1 = 0, cnt2 = 0;
foreach (int ele in arr) {
// Increment count for candidate 1
if (ele1 == ele) {
cnt1++;
}
// Increment count for candidate 2
else if (ele2 == ele) {
cnt2++;
}
// New candidate 1 if count is zero
else if (cnt1 == 0) {
ele1 = ele;
cnt1++;
}
// New candidate 2 if count is zero
else if (cnt2 == 0) {
ele2 = ele;
cnt2++;
}
// Decrease counts if neither candidate
else {
cnt1--;
cnt2--;
}
}
List<int> res = new List<int>();
cnt1 = 0;
cnt2 = 0;
// Count the occurrences of candidates
foreach (int ele in arr) {
if (ele1 == ele) cnt1++;
if (ele2 == ele) cnt2++;
}
// Add to result if they are majority elements
if (cnt1 > n / 3) res.Add(ele1);
if (cnt2 > n / 3 && ele1 != ele2) res.Add(ele2);
if (res.Count == 2 && res[0] > res[1]) {
int temp = res[0];
res[0] = res[1];
res[1] = temp;
}
return res;
}
static void Main() {
int[] arr = { 2, 2, 3, 1, 3, 2, 1, 1 };
List<int> res = findMajority(arr);
foreach (int ele in res) {
Console.Write(ele + " ");
}
}
}
JavaScript
function findMajority(arr) {
const n = arr.length;
// Initialize two candidates and their counts
let ele1 = -1, ele2 = -1;
let cnt1 = 0, cnt2 = 0;
for (let ele of arr) {
// Increment count for candidate 1
if (ele1 === ele) {
cnt1++;
}
// Increment count for candidate 2
else if (ele2 === ele) {
cnt2++;
}
// New candidate 1 if count is zero
else if (cnt1 === 0) {
ele1 = ele;
cnt1++;
}
// New candidate 2 if count is zero
else if (cnt2 === 0) {
ele2 = ele;
cnt2++;
}
// Decrease counts if neither candidate
else {
cnt1--;
cnt2--;
}
}
const res = [];
cnt1 = 0;
cnt2 = 0;
// Count the occurrences of candidates
for (let ele of arr) {
if (ele1 === ele) cnt1++;
if (ele2 === ele) cnt2++;
}
// Add to result if they are majority elements
if (cnt1 > n / 3) res.push(ele1);
if (cnt2 > n / 3 && ele1 != ele2) res.push(ele2);
if (res.length === 2 && res[0] > res[1]) {
[res[0], res[1]] = [res[1], res[0]];
}
return res;
}
// Driver Code
const arr = [2, 2, 3, 1, 3, 2, 1, 1];
const res = findMajority(arr);
console.log(res.join(" "));
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