Adding two polynomials using Linked List
Last Updated :
13 Sep, 2024
Given two polynomial numbers represented by a linked list. The task is to add these lists meaning the coefficients with the same variable powers will be added.
Note: Given polynomials are sorted in decreasing order of power.
Example:
Input:
head1: [[5, 2], [4, 1], [2, 0]]
head2: [[5, 1], [5, 0]]
Output: [[5, 2], [9, 1], [7, 0]]
Explanation:
Input:
head1: [[5, 3], [4, 2], [2, 0]]
head2: [[5, 1], [-5, 0]]
Output: [[5, 3], [4, 2], [5, 1], [-3, 0]]
Explanation: head1 can be represented as 5x^3 + 4x^2 + 2 , head2 can be represented as 5x - 5, add both the polynomials to get 5x^3 + 4x^2 + 5x - 3
[Expected Approach - 1] Using Recursion - O(m+n) Time and O(max(m,n)) Space:
The idea is to recursively check the heads of both lists. If one of the heads is NULL, then return the other head. Otherwise, compare the power of both nodes. If the power of one list is greater than the other, then recursively find the next node of the greater power list. Otherwise, store the sum of coefficients in one list, and return its head.
Below is the implementation of the above approach:
C++
// C++ program to add two polynomials
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int coeff;
int pow;
Node* next;
Node(int c, int p) {
coeff = c;
pow = p;
next = nullptr;
}
};
Node* addPolynomial(Node* head1, Node* head2) {
// if any list is empty, then return
// the other list.
if (head1 == nullptr) return head2;
if (head2 == nullptr) return head1;
// If head1.pow is greater, then recursively find
// its next node, and return head1.
if (head1->pow > head2->pow) {
Node* nextPtr = addPolynomial(head1->next, head2);
head1->next = nextPtr;
return head1;
}
// If head2.pow is greater, then recusrively find its
// next node, and return head2.
else if (head1->pow < head2->pow) {
Node* nextPtr = addPolynomial(head1, head2->next);
head2->next = nextPtr;
return head2;
}
// else store the sum of head1.coeff and head2.coeff in
// head1->coeff, then find its next node and return head1.
Node* nextPtr = addPolynomial(head1->next, head2->next);
head1->coeff += head2->coeff;
head1->next = nextPtr;
return head1;
}
void printList(Node* head) {
Node* curr = head;
while (curr != nullptr) {
cout << curr->coeff << "," << curr->pow <<" ";
curr = curr->next;
}
cout<<endl;
}
int main() {
// 1st polynomial: 5x^2+4x^1+2x^0
Node* head1 = new Node(5,2);
head1->next = new Node(4,1);
head1->next->next = new Node(2,0);
// 2nd polynomial: -5x^1-5x^0
Node* head2 = new Node(-5,1);
head2->next = new Node(-5,0);
Node* head = addPolynomial(head1, head2);
printList(head);
}
C
// C program to add two polynomials
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coeff;
int pow;
struct Node* next;
};
struct Node* createNode(int c, int p);
struct Node* addPolynomial(struct Node* head1, struct Node* head2) {
// if any list is empty, then return
// the other list.
if (head1 == NULL) return head2;
if (head2 == NULL) return head1;
// If head1.pow is greater, then recursively find
// its next node, and return head1.
if (head1->pow > head2->pow) {
struct Node* nextPtr =
addPolynomial(head1->next, head2);
head1->next = nextPtr;
return head1;
}
// If head2.pow is greater, then recursively find its
// next node, and return head2.
else if (head1->pow < head2->pow) {
struct Node* nextPtr =
addPolynomial(head1, head2->next);
head2->next = nextPtr;
return head2;
}
// else store the sum of head1.coeff and head2.coeff in
// head1->coeff, then find its next node and return head1.
struct Node* nextPtr =
addPolynomial(head1->next, head2->next);
head1->coeff += head2->coeff;
head1->next = nextPtr;
return head1;
}
void printList(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
printf("%d,%d ", curr->coeff, curr->pow);
curr = curr->next;
}
printf("\n");
}
struct Node* createNode(int c, int p) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->coeff = c;
newNode->pow = p;
newNode->next = NULL;
return newNode;
}
int main() {
// 1st polynomial: 5x^2+4x^1+2x^0
struct Node* head1 = createNode(5, 2);
head1->next = createNode(4, 1);
head1->next->next = createNode(2, 0);
// 2nd polynomial: -5x^1-5x^0
struct Node* head2 = createNode(-5, 1);
head2->next = createNode(-5, 0);
struct Node* head = addPolynomial(head1, head2);
printList(head);
return 0;
}
Java
// Java program to add two polynomials
class Node {
int coeff;
int pow;
Node next;
Node(int c, int p) {
coeff = c;
pow = p;
next = null;
}
}
class Main {
static Node addPolynomial(Node head1, Node head2) {
// if any list is empty, then return
// the other list.
if (head1 == null) return head2;
if (head2 == null) return head1;
// If head1.pow is greater, then recursively find
// its next node, and return head1.
if (head1.pow > head2.pow) {
Node nextPtr = addPolynomial(head1.next, head2);
head1.next = nextPtr;
return head1;
}
// If head2.pow is greater, then recursively find its
// next node, and return head2.
else if (head1.pow < head2.pow) {
Node nextPtr = addPolynomial(head1, head2.next);
head2.next = nextPtr;
return head2;
}
// else store the sum of head1.coeff and head2.coeff in
// head1.coeff, then find its next node and return head1.
Node nextPtr = addPolynomial(head1.next, head2.next);
head1.coeff += head2.coeff;
head1.next = nextPtr;
return head1;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.coeff + "," + curr.pow + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// 1st polynomial: 5x^2+4x^1+2x^0
Node head1 = new Node(5, 2);
head1.next = new Node(4, 1);
head1.next.next = new Node(2, 0);
// 2nd polynomial: -5x^1-5x^0
Node head2 = new Node(-5, 1);
head2.next = new Node(-5, 0);
Node head = addPolynomial(head1, head2);
printList(head);
}
}
Python
# Python program to add two polynomials
class Node:
def __init__(self, c, p):
self.coeff = c
self.pow = p
self.next = None
def add_polynomial(head1, head2):
# if any list is empty, then return
# the other list.
if head1 is None:
return head2
if head2 is None:
return head1
# If head1.pow is greater, then recursively find
# its next node, and return head1.
if head1.pow > head2.pow:
next_ptr = add_polynomial(head1.next, head2)
head1.next = next_ptr
return head1
# If head2.pow is greater, then recursively find its
# next node, and return head2.
elif head1.pow < head2.pow:
next_ptr = add_polynomial(head1, head2.next)
head2.next = next_ptr
return head2
# else store the sum of head1.coeff and head2.coeff in
# head1.coeff, then find its next node and return head1.
next_ptr = add_polynomial(head1.next, head2.next)
head1.coeff += head2.coeff
head1.next = next_ptr
return head1
def print_list(head):
curr = head
while curr is not None:
print(f"{curr.coeff},{curr.pow}", end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# 1st polynomial: 5x^2+4x^1+2x^0
head1 = Node(5, 2)
head1.next = Node(4, 1)
head1.next.next = Node(2, 0)
# 2nd polynomial: -5x^1-5x^0
head2 = Node(-5, 1)
head2.next = Node(-5, 0)
head = add_polynomial(head1, head2)
print_list(head)
C#
// C# program to add two polynomials
class Node {
public int coeff;
public int pow;
public Node next;
public Node(int c, int p) {
coeff = c;
pow = p;
next = null;
}
}
class GfG {
static Node AddPolynomial(Node head1, Node head2) {
// if any list is empty, then return
// the other list.
if (head1 == null) return head2;
if (head2 == null) return head1;
Node nextPtr;
// If head1.pow is greater, then recursively find
// its next node, and return head1.
if (head1.pow > head2.pow) {
nextPtr = AddPolynomial(head1.next, head2);
head1.next = nextPtr;
return head1;
}
// If head2.pow is greater, then recursively find its
// next node, and return head2.
else if (head1.pow < head2.pow) {
nextPtr = AddPolynomial(head1, head2.next);
head2.next = nextPtr;
return head2;
}
// else store the sum of head1.coeff and head2.coeff in
// head1.coeff, then find its next node and return head1.
nextPtr = AddPolynomial(head1.next, head2.next);
head1.coeff += head2.coeff;
head1.next = nextPtr;
return head1;
}
static void PrintList(Node head) {
Node curr = head;
while (curr != null) {
System.Console.Write(curr.coeff + "," + curr.pow + " ");
curr = curr.next;
}
System.Console.WriteLine();
}
static void Main(string[] args) {
// 1st polynomial: 5x^2+4x^1+2x^0
Node head1 = new Node(5, 2);
head1.next = new Node(4, 1);
head1.next.next = new Node(2, 0);
// 2nd polynomial: -5x^1-5x^0
Node head2 = new Node(-5, 1);
head2.next = new Node(-5, 0);
Node head = AddPolynomial(head1, head2);
PrintList(head);
}
}
JavaScript
// JavaScript program to add two polynomials
class Node {
constructor(c, p) {
this.coeff = c;
this.pow = p;
this.next = null;
}
}
function addPolynomial(head1, head2) {
// if any list is empty, then return
// the other list.
if (head1 === null) return head2;
if (head2 === null) return head1;
// If head1.pow is greater, then recursively find
// its next node, and return head1.
if (head1.pow > head2.pow) {
let nextPtr = addPolynomial(head1.next, head2);
head1.next = nextPtr;
return head1;
}
// If head2.pow is greater, then recursively find its
// next node, and return head2.
else if (head1.pow < head2.pow) {
let nextPtr = addPolynomial(head1, head2.next);
head2.next = nextPtr;
return head2;
}
// else store the sum of head1.coeff and head2.coeff in
// head1.coeff, then find its next node and return head1.
let nextPtr = addPolynomial(head1.next, head2.next);
head1.coeff += head2.coeff;
head1.next = nextPtr;
return head1;
}
function printList(head) {
let curr = head;
while (curr !== null) {
console.log(curr.coeff+","+curr.pow + " ");
curr = curr.next;
}
console.log();
}
// 1st polynomial: 5x^2+4x^1+2x^0
let head1 = new Node(5, 2);
head1.next = new Node(4, 1);
head1.next.next = new Node(2, 0);
// 2nd polynomial: -5x^1-5x^0
let head2 = new Node(-5, 1);
head2.next = new Node(-5, 0);
let head = addPolynomial(head1, head2);
printList(head);
Time Complexity: O(m+n), where m and n are the number of nodes in both the lists.
Auxiliary Space: O(max(m,n))
[Expected Approach] Using Iterative Method - O(m+n) Time and O(1) Space:
The idea is to create a dummy node which will act as the head of resultant list. Start traversing both the lists, if list1->pow if not equal to list2->pow, then Link the node with greater power to the resultant list. Otherwise, add the sum of list1->coeff + list2->coeff to the resultant list.
Below is the implementation of the above approach:
C++
// C++ program to add two polynomials
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int coeff;
int pow;
Node* next;
Node(int c, int p) {
coeff = c;
pow = p;
next = nullptr;
}
};
Node* addPolynomial(Node* head1, Node* head2) {
Node* dummy = new Node(0, 0);
// Node to append other nodes to the end
// of list
Node* prev = dummy;
Node* curr1 = head1, *curr2 = head2;
while (curr1 != nullptr && curr2 != nullptr) {
// if curr2.pow > curr1.pow, then
// append curr2 to list
if (curr1->pow < curr2->pow) {
prev->next = curr2;
prev = curr2;
curr2 = curr2->next;
}
// if curr1.pow > curr2.pow, then
// append curr2 to list
else if (curr1->pow > curr2->pow) {
prev->next = curr1;
prev = curr1;
curr1 = curr1->next;
}
// else, add the sum of curr1->coeff and
// curr2->coeff to curr1->coeff, and append
// curr1 to the list
else {
curr1->coeff = curr1->coeff + curr2->coeff;
prev->next = curr1;
prev = curr1;
curr1 = curr1->next;
curr2 = curr2->next;
}
}
// if curr1 if not null, then append the rest
// to the list
if (curr1 != nullptr) {
prev->next = curr1;
}
// if curr2 if not null, then append the rest
// to the list
if (curr2 != NULL) {
prev->next = curr2;
}
return dummy->next;
}
void printList(Node* head) {
Node* curr = head;
while (curr != nullptr) {
cout << curr->coeff << "," << curr->pow << " ";
curr = curr->next;
}
cout<<endl;
}
int main() {
// 1st polynomial: 5x^2+4x^1+2x^0
Node* head1 = new Node(5,2);
head1->next = new Node(4,1);
head1->next->next = new Node(2,0);
// 2nd polynomial: -5x^1-5x^0
Node* head2 = new Node(-5,1);
head2->next = new Node(-5,0);
Node* head = addPolynomial(head1, head2);
printList(head);
}
C
// C program to add two polynomials
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coeff;
int pow;
struct Node* next;
};
struct Node* createNode(int c, int p);
struct Node* addPolynomial(struct Node* head1, struct Node* head2) {
struct Node* dummy = createNode(0, 0);
// Node to append other nodes to the end
// of list
struct Node* prev = dummy;
struct Node *curr1 = head1, *curr2 = head2;
while (curr1 != NULL && curr2 != NULL) {
// if curr2.pow > curr1.pow, then
// append curr2 to list
if (curr1->pow < curr2->pow) {
prev->next = curr2;
prev = curr2;
curr2 = curr2->next;
}
// if curr1.pow > curr2.pow, then
// append curr2 to list
else if (curr1->pow > curr2->pow) {
prev->next = curr1;
prev = curr1;
curr1 = curr1->next;
}
// else, add the sum of curr1->coeff and
// curr2->coeff to curr1->coeff, and append
// curr1 to the list
else {
curr1->coeff = curr1->coeff + curr2->coeff;
prev->next = curr1;
prev = curr1;
curr1 = curr1->next;
curr2 = curr2->next;
}
}
// if curr1 is not null, then append the rest
// to the list
if (curr1 != NULL) {
prev->next = curr1;
}
// if curr2 is not null, then append the rest
// to the list
if (curr2 != NULL) {
prev->next = curr2;
}
return dummy->next;
}
void printList(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
printf("%d,%d ", curr->coeff, curr->pow);
curr = curr->next;
}
printf("\n");
}
struct Node* createNode(int c, int p) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->coeff = c;
newNode->pow = p;
newNode->next = NULL;
return newNode;
}
int main() {
// 1st polynomial: 5x^2+4x^1+2x^0
struct Node* head1 = createNode(5, 2);
head1->next = createNode(4, 1);
head1->next->next = createNode(2, 0);
// 2nd polynomial: -5x^1-5x^0
struct Node* head2 = createNode(-5, 1);
head2->next = createNode(-5, 0);
struct Node* head = addPolynomial(head1, head2);
printList(head);
return 0;
}
Java
// Java program to add two polynomials
class Node {
int coeff;
int pow;
Node next;
Node(int c, int p) {
coeff = c;
pow = p;
next = null;
}
}
class GfG {
static Node addPolynomial(Node head1, Node head2) {
Node dummy = new Node(0, 0);
// Node to append other nodes to the end
// of list
Node prev = dummy;
Node curr1 = head1, curr2 = head2;
while (curr1 != null && curr2 != null) {
// if curr2.pow > curr1.pow, then
// append curr2 to list
if (curr1.pow < curr2.pow) {
prev.next = curr2;
prev = curr2;
curr2 = curr2.next;
}
// if curr1.pow > curr2.pow, then
// append curr2 to list
else if (curr1.pow > curr2.pow) {
prev.next = curr1;
prev = curr1;
curr1 = curr1.next;
}
// else, add the sum of curr1.coeff and
// curr2.coeff to curr1.coeff, and append
// curr1 to the list
else {
curr1.coeff = curr1.coeff + curr2.coeff;
prev.next = curr1;
prev = curr1;
curr1 = curr1.next;
curr2 = curr2.next;
}
}
// if curr1 if not null, then append the rest
// to the list
if (curr1 != null) {
prev.next = curr1;
}
// if curr2 if not null, then append the rest
// to the list
if (curr2 != null) {
prev.next = curr2;
}
return dummy.next;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.coeff + "," + curr.pow + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// 1st polynomial: 5x^2+4x^1+2x^0
Node head1 = new Node(5, 2);
head1.next = new Node(4, 1);
head1.next.next = new Node(2, 0);
// 2nd polynomial: -5x^1-5x^0
Node head2 = new Node(-5, 1);
head2.next = new Node(-5, 0);
Node head = addPolynomial(head1, head2);
printList(head);
}
}
Python
# Python program to add two polynomials
class Node:
def __init__(self, c, p):
self.coeff = c
self.pow = p
self.next = None
def add_polynomial(head1, head2):
dummy = Node(0, 0)
# Node to append other nodes to the end
# of list
prev = dummy
curr1, curr2 = head1, head2
while curr1 is not None and curr2 is not None:
# if curr2.pow > curr1.pow, then
# append curr2 to list
if curr1.pow < curr2.pow:
prev.next = curr2
prev = curr2
curr2 = curr2.next
# if curr1.pow > curr2.pow, then
# append curr1 to list
elif curr1.pow > curr2.pow:
prev.next = curr1
prev = curr1
curr1 = curr1.next
# else, add the sum of curr1.coeff and
# curr2.coeff to curr1.coeff, and append
# curr1 to the list
else:
curr1.coeff = curr1.coeff + curr2.coeff
prev.next = curr1
prev = curr1
curr1 = curr1.next
curr2 = curr2.next
# if curr1 if not None, then append the rest
# to the list
if curr1 is not None:
prev.next = curr1
# if curr2 if not None, then append the rest
# to the list
if curr2 is not None:
prev.next = curr2
return dummy.next
def print_list(head):
curr = head
while curr is not None:
print(f"{curr.coeff},{curr.pow}", end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# 1st polynomial: 5x^2+4x^1+2x^0
head1 = Node(5, 2)
head1.next = Node(4, 1)
head1.next.next = Node(2, 0)
# 2nd polynomial: -5x^1-5x^0
head2 = Node(-5, 1)
head2.next = Node(-5, 0)
head = add_polynomial(head1, head2)
print_list(head)
C#
// C# program to add two polynomials
class Node {
public int coeff;
public int pow;
public Node next;
public Node(int c, int p) {
coeff = c;
pow = p;
next = null;
}
}
class GfG {
static Node AddPolynomial(Node head1, Node head2) {
Node dummy = new Node(0, 0);
// Node to append other nodes to the end
// of list
Node prev = dummy;
Node curr1 = head1, curr2 = head2;
while (curr1 != null && curr2 != null) {
// if curr2.pow > curr1.pow, then
// append curr2 to list
if (curr1.pow < curr2.pow) {
prev.next = curr2;
prev = curr2;
curr2 = curr2.next;
}
// if curr1.pow > curr2.pow, then
// append curr1 to list
else if (curr1.pow > curr2.pow) {
prev.next = curr1;
prev = curr1;
curr1 = curr1.next;
}
// else, add the sum of curr1.coeff and
// curr2.coeff to curr1.coeff, and append
// curr1 to the list
else {
curr1.coeff = curr1.coeff + curr2.coeff;
prev.next = curr1;
prev = curr1;
curr1 = curr1.next;
curr2 = curr2.next;
}
}
// if curr1 if not null, then append the rest
// to the list
if (curr1 != null) {
prev.next = curr1;
}
// if curr2 if not null, then append the rest
// to the list
if (curr2 != null) {
prev.next = curr2;
}
return dummy.next;
}
static void PrintList(Node head) {
Node curr = head;
while (curr != null) {
System.Console.Write(curr.coeff + "," + curr.pow + " ");
curr = curr.next;
}
System.Console.WriteLine();
}
static void Main(string[] args) {
// 1st polynomial: 5x^2+4x^1+2x^0
Node head1 = new Node(5, 2);
head1.next = new Node(4, 1);
head1.next.next = new Node(2, 0);
// 2nd polynomial: -5x^1-5x^0
Node head2 = new Node(-5, 1);
head2.next = new Node(-5, 0);
Node head = AddPolynomial(head1, head2);
PrintList(head);
}
}
JavaScript
// JavaScript program to add two polynomials
class Node {
constructor(c, p) {
this.coeff = c;
this.pow = p;
this.next = null;
}
}
function addPolynomial(head1, head2) {
let dummy = new Node(0, 0);
// Node to append other nodes to the end
// of list
let prev = dummy;
let curr1 = head1, curr2 = head2;
while (curr1 !== null && curr2 !== null) {
// if curr2.pow > curr1.pow, then
// append curr2 to list
if (curr1.pow < curr2.pow) {
prev.next = curr2;
prev = curr2;
curr2 = curr2.next;
}
// if curr1.pow > curr2.pow, then
// append curr1 to list
else if (curr1.pow > curr2.pow) {
prev.next = curr1;
prev = curr1;
curr1 = curr1.next;
}
// else, add the sum of curr1.coeff and
// curr2.coeff to curr1.coeff, and append
// curr1 to the list
else {
curr1.coeff = curr1.coeff + curr2.coeff;
prev.next = curr1;
prev = curr1;
curr1 = curr1.next;
curr2 = curr2.next;
}
}
// if curr1 if not null, then append the rest
// to the list
if (curr1 !== null) {
prev.next = curr1;
}
// if curr2 if not null, then append the rest
// to the list
if (curr2 !== null) {
prev.next = curr2;
}
return dummy.next;
}
function printList(head) {
let curr = head;
while (curr !== null) {
console.log(curr.coeff+","+curr.pow + " ");
curr = curr.next;
}
console.log();
}
// 1st polynomial: 5x^2+4x^1+2x^0
let head1 = new Node(5, 2);
head1.next = new Node(4, 1);
head1.next.next = new Node(2, 0);
// 2nd polynomial: -5x^1-5x^0
let head2 = new Node(-5, 1);
head2.next = new Node(-5, 0);
let head = addPolynomial(head1, head2);
printList(head);
Time Complexity: O(m + n) where m and n are number of nodes in first and second lists respectively.
Auxiliary Space: O(1)
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