code
stringlengths
195
7.9k
space_complexity
stringclasses
6 values
time_complexity
stringclasses
7 values
// C++ program Count total number of sub-arrays // having total distinct elements same as that // original array. #include<bits/stdc++.h> using namespace std; // Function to calculate distinct sub-array int countDistictSubarray(int arr[], int n) { // Count distinct elements in whole array unordered_map<int, i...
linear
linear
// Make a set of maximum elements from two // arrays A[] and B[] #include <bits/stdc++.h> using namespace std; void maximizeTheFirstArray(int A[], int B[], int n) { // Create copies of A[] and B[] and sort // the copies in descending order. vector<int> temp1(A, A+n); ...
linear
nlogn
// C++ implementation to find the maximum number // of chocolates to be distributed equally among // k students #include <bits/stdc++.h> using namespace std; // function to find the maximum number of chocolates // to be distributed equally among k students int maxNumOfChocolates(int arr[], int n, int k) { // unor...
linear
linear
// C++ program to print n-th number in Recaman's // sequence #include <bits/stdc++.h> using namespace std; // Prints first n terms of Recaman sequence int recaman(int n) { // Create an array to store terms int arr[n]; // First term of the sequence is always 0 arr[0] = 0; printf("%d, ", arr[0]); ...
linear
quadratic
// C++ program to print n-th number in Recaman's // sequence #include <bits/stdc++.h> using namespace std; // Prints first n terms of Recaman sequence void recaman(int n) { if (n <= 0) return; // Print first term and store it in a hash printf("%d, ", 0); unordered_set<int> s; s.insert(0); ...
linear
linear
#include <bits/stdc++.h> using namespace std; void findFibSubset(int arr[], int n) { for (int i = 0; i < n; i++) { int fact1 = 5 * pow(arr[i], 2) + 4; int fact2 = 5 * pow(arr[i], 2) - 4; if ((int)pow((int)pow(fact1, 0.5), 2) == fact1 || (int)pow((int)pow(fact2, 0.5), 2) == fact2)...
constant
linear
// C++ program to find largest Fibonacci subset #include<bits/stdc++.h> using namespace std; // Prints largest subset of an array whose // all elements are fibonacci numbers void findFibSubset(int arr[], int n) { // Find maximum element in arr[] int max = *std::max_element(arr, arr+n); // Generate all F...
linear
linear
// C++ program to find averages of all levels // in a binary tree. #include <bits/stdc++.h> using namespace std; /* A binary tree node has data, pointer to left child and a pointer to right child */ struct Node { int data; struct Node* left, *right; }; string inorder(Node* node, unordered_map<string, int>& ...
quadratic
quadratic
// A brute force approach based CPP program to // find if there is a rectangle with 1 as corners. #include <bits/stdc++.h> using namespace std; // Returns true if there is a rectangle with // 1 as corners. bool isRectangle(const vector<vector<int> >& m) { // finding row and column size int rows = m.size(); ...
constant
quadratic
// An efficient approach based CPP program to // find if there is a rectangle with 1 as // corners. #include <bits/stdc++.h> using namespace std; // Returns true if there is a rectangle with // 1 as corners. bool isRectangle(const vector<vector<int> >& matrix) { // finding row and column size int rows = matri...
quadratic
quadratic
// C++ implementation comes from: // https://github.com/MichaelWehar/FourCornersProblem // Written by Niteesh Kumar and Michael Wehar // References: // [1] F. Mráz, D. Prusa, and M. Wehar. // Two-dimensional Pattern Matching against // Basic Picture Languages. CIAA 2019. // [2] D. Prusa and M. Wehar. Complexit...
quadratic
quadratic
// CPP program for finding maximum area possible // of a rectangle #include <bits/stdc++.h> using namespace std; // function for finding max area int findArea(int arr[], int n) { // sort array in non-increasing order sort(arr, arr + n, greater<int>()); // Initialize two sides of rectangle int dimens...
constant
nlogn
// CPP program for finding maximum area possible // of a rectangle #include <bits/stdc++.h> using namespace std; // function for finding max area int findArea(int arr[], int n) { unordered_set<int> s; // traverse through array int first = 0, second = 0; for (int i = 0; i < n; i++) { // If ...
linear
linear
// C++ implementation to find length of longest // strict bitonic subsequence #include <bits/stdc++.h> using namespace std; // function to find length of longest // strict bitonic subsequence int longLenStrictBitonicSub(int arr[], int n) { // hash table to map the array element with the // length of the lo...
linear
linear
// C++ program to find last seen element in // an array. #include <bits/stdc++.h> using namespace std; // Returns last seen element in arr[] int lastSeenElement(int a[], int n) { // Store last occurrence index of // every element unordered_map<int, int> hash; for (int i = 0; i < n; i++) hash[a...
linear
linear
// A complete working C++ program to // demonstrate all insertion methods // on Linked List #include <bits/stdc++.h> using namespace std; // A linked list node class Node { public: int data; Node *next; }; // Given a reference (pointer to pointer) // to the head of a list and an int, inserts // a new n...
constant
linear
// Alternate method to declare the class // in order to minimize the // memory allocation work #include <bits/stdc++.h> using namespace std; class node { public: int data; node* next; // A constructor is called here node(int value) { // It automatically assigns the // value t...
constant
linear
// A complete working C++ program to delete // a node in a linked list at a given position #include <iostream> using namespace std; // A linked list node class Node { public: int data; Node* next; }; // Given a reference (pointer to pointer) to // the head of a list and an int inserts a // new node on the f...
constant
linear
// C++ program to delete a linked list #include <bits/stdc++.h> using namespace std; /* Link list node */ class Node { public: int data; Node* next; }; /* Function to delete the entire linked list */ void deleteList(Node** head_ref) { /* deref head_ref to get the real head */ Node* current = *head...
constant
linear
// Iterative C++ program to find length // or count of nodes in a linked list #include <bits/stdc++.h> using namespace std; /* Link list node */ class Node { public: int data; Node* next; }; /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list....
constant
linear
// Recursive C++ program to find length // or count of nodes in a linked list #include <bits/stdc++.h> using namespace std; /* Link list node */ class Node { public: int data; Node* next; }; /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list....
linear
linear
// Tail Recursive C++ program to find length // or count of nodes in a linked list #include <bits/stdc++.h> using namespace std; /* Link list node */ class Node { public: int data; Node* next; }; /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the ...
constant
linear
// Iterative C++ program to search // an element in linked list #include <bits/stdc++.h> using namespace std; /* Link list node */ class Node { public: int key; Node* next; }; /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list. */ void push(N...
constant
linear
// Recursive C++ program to search // an element in linked list #include <bits/stdc++.h> using namespace std; /* Link list node */ struct Node { int key; struct Node* next; }; /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list. */ void push(s...
linear
linear
// C++ program to find n'th node in linked list // using recursion #include <bits/stdc++.h> using namespace std; /* Linked list node */ struct Node { int data; struct Node* next; }; /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list....
linear
linear
// C++ program to find N'th node from end #include <bits/stdc++.h> using namespace std; /* Link list node */ struct Node { int data; struct Node* next; }; /* Function to get the Nth node from the last of a linked list*/ void printNthFromLast(struct Node* head, int N) { int len = 0, i; struct Node...
constant
linear
// C++ program to find n-th node // from the end of the linked list. #include <bits/stdc++.h> using namespace std; struct node { int data; node* next; node(int val) { data = val; next = NULL; } }; struct llist { node* head; llist() { head = NULL; } // insert ope...
constant
linear
// C++ program for the above approach #include <iostream> using namespace std; class Node{ public: int data; Node *next; }; class NodeOperation{ public: // Function to add a new node void pushNode(class Node** head_ref,int data_val){ // Allocate node class...
constant
linear
#include <bits/stdc++.h> using namespace std; // Link list node struct node { int data; struct node* next; }; // Function to get the middle of // the linked list void printMiddle(struct node* head) { int count = 0; struct node* mid = head; while (head != NULL) { // Update...
constant
linear
// C++ program to count occurrences in a linked list #include <bits/stdc++.h> using namespace std; /* Link list node */ class Node { public: int data; Node* next; }; /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list. */ void push(Node** head...
constant
linear
// C++ program to count occurrences in a linked list using // recursion #include <bits/stdc++.h> using namespace std; /* Link list node */ struct Node { int data; struct Node* next; }; // global variable for counting frequency of // given element k int frequency = 0; /* Given a reference (pointer to pointer...
linear
linear
// C++ program to count number of nodes // in loop in a linked list if loop is // present #include <bits/stdc++.h> using namespace std; /* Link list node */ struct Node { int data; struct Node* next; }; // Returns count of nodes present in loop. int countNodes(struct Node* n) { int res = 1; struct N...
constant
linear
#include <bits/stdc++.h> using namespace std; class Node { public: int data; Node(int d) { data = d; } Node* ptr; }; // Function to check if the linked list // is palindrome or not bool isPalin(Node* head) { // Temp pointer Node* slow = head; // Declare a stack stack<int> s; //...
linear
linear
// C++ program to check if a linked list is palindrome #include <bits/stdc++.h> using namespace std; // Link list node struct Node { char data; struct Node* next; }; void reverse(struct Node**); bool compareLists(struct Node*, struct Node*); // Function to check if given linked list is // palindrome or no...
constant
linear
// Recursive program to check if a given linked list is // palindrome #include <bits/stdc++.h> using namespace std; /* Link list node */ struct node { char data; struct node* next; }; // Initial parameters to this function are &head and head bool isPalindromeUtil(struct node** left, st...
linear
linear
/* C++ Program to remove duplicates in an unsorted linked list */ #include <bits/stdc++.h> using namespace std; /* A linked list node */ struct Node { int data; struct Node* next; }; // Utility function to create a new Node struct Node* newNode(int data) { Node* temp = new Node; temp->data = data...
constant
quadratic
/* This program swaps the nodes of linked list rather than swapping the field from the nodes.*/ #include <bits/stdc++.h> using namespace std; /* A linked list node */ class Node { public: int data; Node* next; }; /* Function to swap nodes x and y in linked list by changing links */ void swapNodes(Node** hea...
constant
linear
// C++ program to swap two given nodes of a linked list #include <iostream> using namespace std; // A linked list node class class Node { public: int data; class Node* next; // constructor Node(int val, Node* next) : data(val) , next(next) { } // print list from this to las...
constant
linear
/* CPP Program to move last element to front in a given linked list */ #include <bits/stdc++.h> using namespace std; /* A linked list node */ class Node { public: int data; Node* next; }; /* We are using a double pointer head_ref here because we change head of the linked list inside this function.*/ void ...
constant
linear
#include<bits/stdc++.h> using namespace std; /* Link list node */ struct Node { int data; Node* next; }; void push(Node** head_ref, int new_data); /*This solution uses the temporary dummy to build up the result list */ Node* sortedIntersect(Node* a, Node* b) { Node dummy; Node* tail = &dummy ...
linear
linear
// C++ program to implement above approach #include <bits/stdc++.h> /* Link list node */ struct Node { int data; struct Node* next; }; void push(struct Node** head_ref, int new_data); /* This solution uses the local reference */ struct Node* sortedIntersect( struct Node* a, struct Node* ...
linear
linear
#include <bits/stdc++.h> using namespace std; // Link list node struct Node { int data; struct Node* next; }; struct Node* sortedIntersect(struct Node* a, struct Node* b) { // base case if (a == NULL || b == NULL) return NULL; /* If both lists are non...
linear
linear
// C++ program to implement above approach #include <bits/stdc++.h> using namespace std; // Link list node struct Node { int data; struct Node* next; }; void printList(struct Node* node) { while (node != NULL) { cout << " " << node->data; node = node->next; } } void append(struct Nod...
linear
linear
// C++ program for Quick Sort on Singly Linked List #include <cstdio> #include <iostream> using namespace std; /* a node of the singly linked list */ struct Node { int data; struct Node* next; }; /* A utility function to insert a node at the beginning of * linked list */ void push(struct Node** head_ref,...
linear
nlogn
// C++ program to segregate even and // odd nodes in a Linked List #include <bits/stdc++.h> using namespace std; /* a node of the singly linked list */ class Node { public: int data; Node *next; }; void segregateEvenOdd(Node **head_ref) { Node *end = *head_ref; Node *prev = NULL; Node *curr...
constant
linear
// CPP program to segregate even and odd nodes in a // Linked List #include <bits/stdc++.h> using namespace std; /* a node of the singly linked list */ struct Node { int data; struct Node* next; }; // Function to segregate even and odd nodes. void segregateEvenOdd(struct Node** head_ref) { // Starting n...
constant
linear
// Iterative C++ program to reverse a linked list #include <bits/stdc++.h> using namespace std; /* Link list node */ struct Node { int data; struct Node* next; Node(int data) { this->data = data; next = NULL; } }; struct LinkedList { Node* head; LinkedList() { head = NULL...
constant
linear
// Recursive C++ program to reverse // a linked list #include <bits/stdc++.h> using namespace std; /* Link list node */ struct Node { int data; struct Node* next; Node(int data) { this->data = data; next = NULL; } }; struct LinkedList { Node* head; LinkedList() { head = N...
linear
linear
// A simple and tail recursive C++ program to reverse // a linked list #include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node* next; }; void reverseUtil(Node* curr, Node* prev, Node** head); // This function mainly calls reverseUtil() // with prev as NULL void reverse(Node** hea...
linear
linear
// C++ program for above approach #include <bits/stdc++.h> #include <iostream> using namespace std; // Create a class Node to enter values and address in the // list class Node { public: int data; Node* next; }; // Function to reverse the linked list void reverseLL(Node** head) { // Create a stack "s" o...
linear
linear
// C++ program to delete a given key from // linked list. #include <bits/stdc++.h> using namespace std; // Structure for a node class Node { public: int data; Node* next; }; // Function to insert a node at the // beginning of a Circular linked list void push(Node** head_ref, int data) { // Create a ne...
constant
linear
// C++ program to check if linked list is circular #include<bits/stdc++.h> using namespace std; /* Link list Node */ struct Node { int data; struct Node* next; }; /* This function returns true if given linked list is circular, else false. */ bool isCircular(struct Node *head) { // An empty linked l...
constant
linear
// C++ Program to convert a Binary Tree // to a Circular Doubly Linked List #include <iostream> using namespace std; // To represents a node of a Binary Tree struct Node { struct Node *left, *right; int data; }; // A function that appends rightList at the end // of leftList. Node* concatenate(Node* leftList...
logn
linear
// A C++ program for in-place conversion of Binary Tree to // CDLL #include <iostream> using namespace std; /* A binary tree node has - data , left and right pointers */ struct Node { int data; Node* left; Node* right; }; // A utility function that converts given binary tree to // a doubly linked list //...
logn
linear
// C++ program to count number of nodes in a circular // linked list. #include <bits/stdc++.h> using namespace std; /*structure for a node*/ struct Node { int data; Node* next; Node(int x) { data = x; next = NULL; } }; /* Function to insert a node at the beginning of a Circula...
constant
linear
// C++ program to delete a given key from // linked list. #include <bits/stdc++.h> using namespace std; // Structure for a node class Node { public: int data; Node* next; }; // Function to insert a node at the // beginning of a Circular linked list void push(Node** head_ref, int data) { // Create a ne...
constant
linear
// C++ implementation of De-queue using circular // array #include <iostream> using namespace std; // Maximum size of array or Dequeue #define MAX 100 // A structure to represent a Deque class Deque { int arr[MAX]; int front; int rear; int size; public: Deque(int size) { front = -1...
linear
linear
// CPP program to exchange first and // last node in circular linked list #include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node* next; }; struct Node* addToEmpty(struct Node* head, int data) { // This function is only for empty list if (head != NULL) return head; ...
constant
linear
// A complete working C++ program to // demonstrate all insertion methods #include <bits/stdc++.h> using namespace std; // A linked list node class Node { public: int data; Node* next; Node* prev; }; /* Given a reference (pointer to pointer) to the head of a list and an int, inserts a new node on the fr...
constant
linear
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; class node { public: node* prev; int data; node* next; node(int value) { // A constructor is called here prev = NULL; // By default previous pointer is // pointed to NULL da...
constant
linear
// C++ program to delete a node from // Doubly Linked List #include <bits/stdc++.h> using namespace std; /* a node of the doubly linked list */ class Node { public: int data; Node* next; Node* prev; }; /* Function to delete a node in a Doubly Linked List. head_ref --> pointer to head node pointer. d...
constant
constant
/* C++ program to reverse a doubly linked list */ #include <bits/stdc++.h> using namespace std; /* Node of the doubly linked list */ class Node { public: int data; Node* next; Node* prev; }; /* Function to reverse a Doubly Linked List */ void reverse(Node** head_ref) { Node* temp = NULL; Node*...
constant
linear
// C++ program to reverse a doubly linked list #include <bits/stdc++.h> using namespace std; struct LinkedList { struct Node { int data; Node *next, *prev; Node(int d) { data = d; next = prev = NULL; } }; Node* head = NULL; /* Function to...
linear
linear
// A C++ program to swap Kth node from beginning with kth // node from end #include <bits/stdc++.h> using namespace std; // A Linked List node typedef struct Node { int data; struct Node* next; } Node; // Utility function to insert a node at the beginning void push(Node** head_ref, int new_data) { Node*...
constant
linear
// C++ program for merge sort on doubly linked list #include <bits/stdc++.h> using namespace std; class Node { public: int data; Node *next, *prev; }; Node *split(Node *head); // Function to merge two linked lists Node *merge(Node *first, Node *second) { // If first linked list is empty ...
constant
nlogn
// C++ program to create a doubly linked list out // of given a ternary tree. #include <bits/stdc++.h> using namespace std; /* A ternary tree */ struct Node { int data; struct Node *left, *middle, *right; }; /* Helper function that allocates a new node with the given data and assign NULL to left, middle ...
linear
linear
// C++ program to find a pair with given sum x. #include<bits/stdc++.h> using namespace std; // structure of node of doubly linked list struct Node { int data; struct Node *next, *prev; }; // Function to find pair whose sum equal to given value x. void pairSum(struct Node *head, int x) { // Set two poin...
constant
linear
// C++ implementation to insert value in sorted way // in a sorted doubly linked list #include <bits/stdc++.h> using namespace std; // Node of a doubly linked list struct Node { int data; struct Node* prev, *next; }; // function to create and return a new node // of a doubly linked list struct Node* getNo...
linear
linear
// C++ implementation to count triplets in a sorted doubly linked list // whose sum is equal to a given value 'x' #include <bits/stdc++.h> using namespace std; // structure of node of doubly linked list struct Node { int data; struct Node* next, *prev; }; // function to count triplets in a sorted doubl...
constant
cubic
// C++ implementation to count triplets in a sorted doubly linked list // whose sum is equal to a given value 'x' #include <bits/stdc++.h> using namespace std; // structure of node of doubly linked list struct Node { int data; struct Node* next, *prev; }; // function to count triplets in a sorted doubl...
linear
quadratic
// C++ implementation to count triplets in a sorted doubly linked list // whose sum is equal to a given value 'x' #include <bits/stdc++.h> using namespace std; // structure of node of doubly linked list struct Node { int data; struct Node* next, *prev; }; // function to count pairs whose sum equal to g...
constant
quadratic
/* C++ implementation to remove duplicates from a sorted doubly linked list */ #include <bits/stdc++.h> using namespace std; /* a node of the doubly linked list */ struct Node { int data; struct Node* next; struct Node* prev; }; /* Function to delete a node in a Doubly Linked List. head_ref --> ...
constant
linear
/* C++ implementation to delete all occurrences of a given key in a doubly linked list */ #include <bits/stdc++.h> using namespace std; /* a node of the doubly linked list */ struct Node { int data; struct Node* next; struct Node* prev; }; /* Function to delete a node in a Doubly Linked List. he...
constant
linear
// C++ implementation to remove duplicates from an // unsorted doubly linked list #include <bits/stdc++.h> using namespace std; // a node of the doubly linked list struct Node { int data; struct Node* next; struct Node* prev; }; // Function to delete a node in a Doubly Linked List. // head_ref --> poi...
constant
quadratic
// C++ implementation to remove duplicates from an // unsorted doubly linked list #include <bits/stdc++.h> using namespace std; // a node of the doubly linked list struct Node { int data; struct Node* next; struct Node* prev; }; // Function to delete a node in a Doubly Linked List. // head_ref --> poi...
linear
linear
// C++ implementation to sort a k sorted doubly // linked list #include<bits/stdc++.h> using namespace std; // a node of the doubly linked list struct Node { int data; struct Node* next; struct Node* prev; }; // function to sort a k sorted doubly linked list struct Node* sortAKSortedDLL(struct Node* h...
constant
quadratic
// C++ implementation to sort a k sorted doubly // linked list #include <bits/stdc++.h> using namespace std; // a node of the doubly linked list struct Node { int data; struct Node* next; struct Node* prev; }; // 'compare' function used to build up the // priority queue struct compare { bool operato...
constant
nlogn
// C++ program to convert a given Binary Tree to Doubly Linked List #include <bits/stdc++.h> // Structure for tree and linked list struct Node { int data; Node *left, *right; }; // Utility function for allocating node for Binary // Tree. Node* newNode(int data) { Node* node = new Node; node->data ...
linear
linear
/* C++ program to insetail nodes in doubly linked list such that list remains in ascending order on printing from left to right */ #include <bits/stdc++.h> using namespace std; // A linked list node class Node { public: Node *prev; int info; Node *next; }; // Function to insetail new node void nodeI...
constant
linear
#include<iostream> using namespace std; class Node { public: char data; Node* next; Node* pre; Node(int data) { this->data=data; pre=NULL; next=NULL; } }; void insertAtHead(Node* &head, int data) { Node* n = new Node(data); if(head==NULL) {...
constant
linear
// C++ implementation to reverse a doubly linked list // in groups of given size without recursion // Iterative Method #include <iostream> using namespace std; // Represents a node of doubly linked list struct Node { int data; Node *next, *prev; }; // function to get a new node Node* getNode(int data) { ...
constant
linear
// C++ program to illustrate inserting a Node in // a Circular Doubly Linked list in begging, end // and middle #include <bits/stdc++.h> using namespace std; // Structure of a Node struct Node { int data; struct Node* next; struct Node* prev; }; // Function to insert at the end void insertEnd(struct N...
constant
linear
// C++ program to implement Stack // using linked list so that reverse // can be done with O(1) extra space. #include<bits/stdc++.h> using namespace std; class StackNode { public: int data; StackNode *next; StackNode(int data) { this->data = data; this->next = NULL; } }; ...
constant
linear
// C++ program to implement unrolled linked list // and traversing it. #include <bits/stdc++.h> using namespace std; #define maxElements 4 // Unrolled Linked List Node class Node { public: int numElements; int array[maxElements]; Node *next; }; /* Function to traverse an unrolled linked list and pri...
linear
linear
// C++ program to construct the maximum sum linked // list out of two given sorted lists #include<bits/stdc++.h> using namespace std; //A linked list node struct Node { int data; //data belong to that node Node *next; //next pointer }; // Push the data to the head of the linked list void push(Node **head, i...
constant
linear
// C++ program to find fractional node in a linked list #include <bits/stdc++.h> /* Linked list node */ struct Node { int data; Node* next; }; /* Function to create a new node with given data */ Node* newNode(int data) { Node* new_node = new Node; new_node->data = data; new_node->next = NULL; ...
constant
linear
// C++ program to find modular node in a linked list #include <bits/stdc++.h> /* Linked list node */ struct Node { int data; Node* next; }; /* Function to create a new node with given data */ Node* newNode(int data) { Node* new_node = new Node; new_node->data = data; new_node->next = NULL; r...
constant
linear
// C++ Program to find smallest and largest // elements in singly linked list. #include <bits/stdc++.h> using namespace std; /* Linked list node */ struct Node { int data; struct Node* next; }; // Function that returns the largest element // from the linked list. int largestElement(struct Node* head) { ...
constant
linear
/* C++ program to arrange consonants and vowels nodes in a linked list */ #include<bits/stdc++.h> using namespace std; /* A linked list node */ struct Node { char data; struct Node *next; }; /* Function to add new node to the List */ Node *newNode(char key) { Node *temp = new Node; temp->data = k...
constant
linear
/* C++ program to arrange consonants and vowels nodes in a linked list */ #include <bits/stdc++.h> using namespace std; /* A linked list node */ struct Node { char data; struct Node* next; Node(int x) { data = x; next = NULL; } }; /* Function to add new node to the List */ void a...
linear
linear
// C++ program to partition a linked list around a // given value. #include<bits/stdc++.h> using namespace std; /* Link list Node */ struct Node { int data; struct Node* next; }; // A utility function to create a new node Node *newNode(int data) { struct Node* new_node = new Node; new_node->data = ...
constant
linear
// C++ implementation to modify the contents of // the linked list #include <bits/stdc++.h> using namespace std; /* Linked list node */ struct Node { int data; struct Node* next; }; /* function prototype for printing the list */ void printList(struct Node*); /* Function to insert a node at the beginning o...
constant
linear
// C++ implementation to modify the // contents of the linked list #include <bits/stdc++.h> using namespace std; // Linked list node struct Node { int data; struct Node* next; }; // function prototype for printing the list void printList(struct Node*); // Function to insert a node at the // beginning of t...
linear
linear
// C++ program to rotate a matrix // by 90 degrees #include <bits/stdc++.h> #define N 4 using namespace std; // An Inplace function to // rotate a N x N matrix // by 90 degrees in // anti-clockwise direction void rotateMatrix(int mat[][N]) { // Consider all squares one by one for (int x = 0; x < N / 2; x++) {...
constant
quadratic
// C++ program to rotate a matrix // by 90 degrees #include <bits/stdc++.h> using namespace std; #define N 4 // An Inplace function to // rotate a N x N matrix // by 90 degrees in // anti-clockwise direction void rotateMatrix(int mat[][N]) { // REVERSE every row for (int i = 0; i < N; i++) reverse(mat[i],...
constant
quadratic
#include <bits/stdc++.h> using namespace std; //Function to rotate matrix anticlockwise by 90 degrees. void rotateby90(vector<vector<int> >& matrix) { int n=matrix.size(); int mid; if(n%2==0) mid=n/2-1; else mid=n/2; for(int i=0,j=n-1;i<=mid;i++,j--) { for(int...
constant
quadratic
// C++ program to rotate a matrix by 180 degrees #include <bits/stdc++.h> #define N 3 using namespace std; // Function to Rotate the matrix by 180 degree void rotateMatrix(int mat[][N]) { // Simply print from last cell to first cell. for (int i = N - 1; i >= 0; i--) { for (int j = N - 1; j >= 0; j--) ...
constant
quadratic
// C++ program for left rotation of matrix by 180 #include <bits/stdc++.h> using namespace std; #define R 4 #define C 4 // Function to rotate the matrix by 180 degree void reverseColumns(int arr[R][C]) { for (int i = 0; i < C; i++) for (int j = 0, k = C - 1; j < k; j++, k--) swap(arr[j][i], ...
constant
quadratic
#include <bits/stdc++.h> using namespace std; /** * Reverse Row at specified index in the matrix * @param data matrix * @param index row index */ void reverseRow(vector<vector<int>>& data, int index) { int cols = data[index].size(); for(int i = 0; i < cols / 2; i++) { int temp ...
constant
quadratic
// C++ program to rotate individual rings by k in // spiral order traversal. #include<bits/stdc++.h> #define MAX 100 using namespace std; // Fills temp array into mat[][] using spiral order // traversal. void fillSpiral(int mat[][MAX], int m, int n, int temp[]) { int i, k = 0, l = 0; /* k - starting row in...
quadratic
quadratic