code
stringlengths
195
7.9k
space_complexity
stringclasses
6 values
time_complexity
stringclasses
7 values
// LCM of given range queries using Segment Tree #include <bits/stdc++.h> using namespace std; #define MAX 1000 // allocate space for tree int tree[4 * MAX]; // declaring the array globally int arr[MAX]; // Function to return gcd of a and b int gcd(int a, int b) { if (a == 0) return b; return gc...
constant
linear
// C++ program to find minimum and maximum using segment // tree #include <bits/stdc++.h> using namespace std; // Node for storing minimum and maximum value of given range struct node { int minimum; int maximum; }; // A utility function to get the middle index from corner // indexes. int getMid(int s, int e...
linear
logn
/* C++ Program to find LCA of u and v by reducing the problem to RMQ */ #include<bits/stdc++.h> #define V 9 // number of nodes in input tree int euler[2*V - 1]; // For Euler tour sequence int level[2*V - 1]; // Level of nodes in tour sequence int firstOccurrence[V+1]; // First occurrences of...
linear
linear
// A Divide and Conquer Program to find maximum rectangular area in a histogram #include <bits/stdc++.h> using namespace std; // A utility function to find minimum of three integers int max(int x, int y, int z) { return max(max(x, y), z); } // A utility function to get minimum of two numbers in hist[] int minVal...
linear
nlogn
// C++ program to print all words in the CamelCase // dictionary that matches with a given pattern #include <bits/stdc++.h> using namespace std; // Alphabet size (# of upper-Case characters) #define ALPHABET_SIZE 26 // A Trie node struct TrieNode { TrieNode* children[ALPHABET_SIZE]; // isLeaf is true if t...
linear
quadratic
// C++ program to construct an n x n // matrix such that every row and every // column has distinct values. #include <iostream> #include <bits/stdc++.h> using namespace std; const int MAX = 100; int mat[MAX][MAX]; // Fills non-one entries in column j // Given that there is a "1" at // position mat[i][j], this fun...
constant
quadratic
// Given a binary matrix of M X N of integers, // you need to return only unique rows of binary array #include <bits/stdc++.h> using namespace std; #define ROW 4 #define COL 5 // The main function that prints // all unique rows in a given matrix. void findUniqueRows(int M[ROW][COL]) { //Traverse through the matri...
constant
cubic
// Given a binary matrix of M X N of integers, // you need to return only unique rows of binary array #include <bits/stdc++.h> using namespace std; #define ROW 4 #define COL 5 class BST { int data; BST *left, *right; public: // Default constructor. BST(); // Parameterized constru...
linear
quadratic
// Given a binary matrix of M X N of integers, // you need to return only unique rows of binary array #include <bits/stdc++.h> using namespace std; #define ROW 4 #define COL 5 // A Trie node class Node { public: bool isEndOfCol; Node *child[2]; // Only two children needed for 0 and 1 } ; // A utility ...
quadratic
quadratic
// C++ code to print unique row in a // given binary matrix #include<bits/stdc++.h> using namespace std; void printArray(int arr[][5], int row, int col) { unordered_set<string> uset; for(int i = 0; i < row; i++) { string s = ""; for(int j = 0; ...
linear
quadratic
// A C++ program to find the count of distinct substring // of a string using trie data structure #include <bits/stdc++.h> #define MAX_CHAR 26 using namespace std; // A Suffix Trie (A Trie of all suffixes) Node class SuffixTrieNode { public: SuffixTrieNode *children[MAX_CHAR]; SuffixTrieNode() // Constructor ...
constant
linear
#include <bits/stdc++.h> using namespace std; // Function to find minimum XOR pair int minXOR(int arr[], int n) { // Sort given array sort(arr, arr + n); int minXor = INT_MAX; int val = 0; // calculate min xor of consecutive pairs for (int i = 0; i < n - 1; i++) { val = arr[i] ^ ar...
constant
nlogn
// A simple C++ program to find max subarray XOR #include<bits/stdc++.h> using namespace std; int maxSubarrayXOR(int arr[], int n) { int ans = INT_MIN; // Initialize result // Pick starting points of subarrays for (int i=0; i<n; i++) { int curr_xor = 0; // to store xor of current subarra...
constant
quadratic
// C++ program for a Trie based O(n) solution to find max // subarray XOR #include<bits/stdc++.h> using namespace std; // Assumed int size #define INT_SIZE 32 // A Trie Node struct TrieNode { int value; // Only used in leaf nodes TrieNode *arr[2]; }; // Utility function to create a Trie node TrieNode *ne...
linear
linear
// C++ code to demonstrate operations of Binary Index Tree #include <iostream> using namespace std; /* n --> No. of elements present in input array. BITree[0..n] --> Array that represents Binary Indexed Tree. arr[0..n-1] --> Input array for which prefix sum is evaluated. */ // Returns sum of arr[0...
linear
nlogn
/* C++ program to implement 2D Binary Indexed Tree 2D BIT is basically a BIT where each element is another BIT. Updating by adding v on (x, y) means it's effect will be found throughout the rectangle [(x, y), (max_x, max_y)], and query for (x, y) gives you the result of the rectangle [(0, 0), (x, y)], assuming the to...
quadratic
nlogn
// A Simple C++ O(n^3) program to count inversions of size 3 #include<bits/stdc++.h> using namespace std; // Returns counts of inversions of size three int getInvCount(int arr[],int n) { int invcount = 0; // Initialize result for (int i=0; i<n-2; i++) { for (int j=i+1; j<n-1; j++) { ...
constant
cubic
// A O(n^2) C++ program to count inversions of size 3 #include<bits/stdc++.h> using namespace std; // Returns count of inversions of size 3 int getInvCount(int arr[], int n) { int invcount = 0; // Initialize result for (int i=1; i<n-1; i++) { // Count all smaller elements on right of arr[i] ...
constant
quadratic
// C++ program to count the number of inversion // pairs in a 2D matrix #include <bits/stdc++.h> using namespace std; // for simplicity, we are taking N as 4 #define N 4 // Function to update a 2D BIT. It updates the // value of bit[l][r] by adding val to bit[l][r] void update(int l, int r, int val, int bit[][N + 1...
quadratic
logn
// Naive algorithm for building suffix array of a given text #include <iostream> #include <cstring> #include <algorithm> using namespace std; // Structure to store information of a suffix struct suffix { int index; char *suff; }; // A comparison function used by sort() to compare two suffixes int cmp(stru...
linear
nlogn
// C++ program to insert a node in AVL tree #include<bits/stdc++.h> using namespace std; // An AVL tree node class Node { public: int key; Node *left; Node *right; int height; }; // A utility function to get the // height of the tree int height(Node *N) { if (N == NULL) return 0; ...
constant
nlogn
// C++ program to find N'th element in a set formed // by sum of two arrays #include<bits/stdc++.h> using namespace std; //Function to calculate the set of sums int calculateSetOfSum(int arr1[], int size1, int arr2[], int size2, int N) { // Insert each pair sum into set. Note that a set ...
linear
quadratic
#include <iostream> using namespace std; void constructLowerArray(int arr[], int* countSmaller, int n) { int i, j; // Initialize all the counts in // countSmaller array as 0 for (i = 0; i < n; i++) countSmaller[i] = 0; for (i = 0; i < n; i++) { for (j =...
constant
quadratic
#include <bits/stdc++.h> using namespace std; void merge(vector<pair<int, int> >& v, vector<int>& ans, int l, int mid, int h) { vector<pair<int, int> > t; // temporary array for merging both halves int i = l; int j = mid + 1; while (i < mid + 1 && j <= h) { // v[i].fir...
linear
nlogn
#include <iostream> using namespace std; #include <stdio.h> #include <stdlib.h> // An AVL tree node struct node { int key; struct node* left; struct node* right; int height; // size of the tree rooted // with this node int size; }; // A utility function to get // maximum of two integ...
linear
nlogn
#include <bits/stdc++.h> using namespace std; // BST node structure class Node { public: int val; int count; Node* left; Node* right; // Constructor Node(int num1, int num2) { this->val = num1; this->count = num2; this->left = this->right = NULL; } }; // F...
linear
quadratic
// C++ program to sort an array according absolute // difference with x. #include <bits/stdc++.h> using namespace std; // Function to sort an array according absolute // difference with x. void rearrange(int arr[], int n, int x) { multimap<int, int> m; multimap<int, int>::iterator it; // Store values in...
linear
nlogn
// CPP program for the above approach #include <bits/stdc++.h> using namespace std; void rearrange(int arr[], int n, int x) { /* We can send the value x into lambda expression as follows: [capture]() { //statements //capture value can be used inside ...
constant
nlogn
// C++ program to find maximum product of an increasing // subsequence of size 3 #include <bits/stdc++.h> using namespace std; // Returns maximum product of an increasing subsequence of // size 3 in arr[0..n-1]. If no such subsequence exists, // then it returns INT_MIN long long int maxProduct(int arr[], int n) { ...
constant
cubic
// C++ program to find maximum product of an increasing // subsequence of size 3 #include <bits/stdc++.h> using namespace std; // Returns maximum product of an increasing subsequence of // size 3 in arr[0..n-1]. If no such subsequence exists, // then it returns INT_MIN long long int maxProduct(int arr[], int n) { ...
linear
nlogn
// C++ Code for the above approach #include <bits/stdc++.h> using namespace std; /* A binary tree node has data, a pointer to left child and a pointer to right child */ class Node { public: int data; Node* left; Node* right; }; // Function to return a new Node Node* newNode(int data) { Node* node ...
constant
linear
/* CPP program to check if a tree is height-balanced or not */ #include <bits/stdc++.h> using namespace std; /* A binary tree node has data, pointer to left child and a pointer to right child */ class Node { public: int data; Node* left; Node* right; Node(int d) { int data = d; l...
linear
quadratic
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Structure of a tree node struct Node { int key; struct Node* left; struct Node* right; Node(int k) { key = k; left = right = NULL; } }; // Function to check if tree is height balanced int...
linear
linear
// C++ program to find number of islands // using Disjoint Set data structure. #include <bits/stdc++.h> using namespace std; // Class to represent // Disjoint Set Data structure class DisjointUnionSets { vector<int> rank, parent; int n; public: DisjointUnionSets(int n) { ra...
quadratic
quadratic
// C++ program to find the height of the generic // tree(n-ary tree) if parent array is given #include <bits/stdc++.h> using namespace std; // function to fill the height vector int rec(int i, int parent[], vector<int> height) { // if we have reached root node the // return 1 as height of root node if (pa...
linear
linear
// C++ program to find number // of children of given node #include <bits/stdc++.h> using namespace std; // Represents a node of an n-ary tree class Node { public: int key; vector<Node*> child; Node(int data) { key = data; } }; // Function to calculate number // of children of given ...
linear
linear
// C++ program to find sum of all // elements in generic tree #include <bits/stdc++.h> using namespace std; // Represents a node of an n-ary tree struct Node { int key; vector<Node*> child; }; // Utility function to create a new tree node Node* newNode(int key) { Node* temp = new Node; temp->key = k...
linear
linear
// C++ program to create a tree with left child // right sibling representation. #include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node* next; struct Node* child; }; // Creating new Node Node* newNode(int data) { Node* newNode = new Node; newNode->next = newNode->child...
linear
linear
// C++ program to count number of nodes // which has more children than its parent #include<bits/stdc++.h> using namespace std; // function to count number of nodes // which has more children than its parent int countNodes(vector<int> adj[], int root) { int count = 0; // queue for applying BFS queu...
linear
linear
// C++ program to generate short url from integer id and // integer id back from short url. #include<iostream> #include<algorithm> #include<string> using namespace std; // Function to generate a short url from integer ID string idToShortURL(long int n) { // Map to store 62 possible characters char map[] = "ab...
constant
linear
// A C++ program to implement Cartesian Tree sort // Note that in this program we will build a min-heap // Cartesian Tree and not max-heap. #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; Node *le...
linear
nlogn
// C++ Program to search an element // in a sorted and pivoted array #include <bits/stdc++.h> using namespace std; // Standard Binary Search function int binarySearch(int arr[], int low, int high, int key) { if (high < low) return -1; int mid = (low + high) / 2; if (key == arr[mid]) re...
constant
logn
// Search an element in sorted and rotated // array using single pass of Binary Search #include <bits/stdc++.h> using namespace std; // Returns index of key in arr[l..h] if // key is present, otherwise returns -1 int search(int arr[], int l, int h, int key) { if (l > h) return -1; int mid = (l + h) ...
constant
logn
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // This function returns true if arr[0..n-1] // has a pair with sum equals to x. bool pairInSortedRotated(int arr[], int n, int x) { // Find the pivot element int i; for (i = 0; i < n - 1; i++) if (arr[i] > arr...
constant
linear
// C++ program to find max value of i*arr[i] #include <iostream> using namespace std; // Returns max possible value of i*arr[i] int maxSum(int arr[], int n) { // Find array sum and i*arr[i] with no rotation int arrSum = 0; // Stores sum of arr[i] int currVal = 0; // Stores sum of i*arr[i] for (int i =...
constant
linear
// A Naive C++ program to find maximum sum rotation #include<bits/stdc++.h> using namespace std; // Returns maximum value of i*arr[i] int maxSum(int arr[], int n) { // Initialize result int res = INT_MIN; // Consider rotation beginning with i // for all possible values of i. for (int i=0; i<n; i++)...
constant
quadratic
// An efficient C++ program to compute // maximum sum of i*arr[i] #include<bits/stdc++.h> using namespace std; int maxSum(int arr[], int n) { // Compute sum of all array elements int cum_sum = 0; for (int i=0; i<n; i++) cum_sum += arr[i]; // Compute sum of i*arr[i] for initial // confi...
constant
linear
// C++ program to find maximum sum of all // rotation of i*arr[i] using pivot. #include <iostream> using namespace std; // fun declaration int maxSum(int arr[], int n); int findPivot(int arr[], int n); // function definition int maxSum(int arr[], int n) { int sum = 0; int i; int pivot = findPivot(arr, n...
constant
linear
// C++ program for the above approach #include <iostream> using namespace std; int* rotateArray(int A[], int start, int end) { while (start < end) { int temp = A[start]; A[start] = A[end]; A[end] = temp; start++; end--; } return A; } void leftRotate(int A[], int a, in...
linear
linear
// CPP implementation of left rotation of // an array K number of times #include <bits/stdc++.h> using namespace std; // Fills temp[] with two copies of arr[] void preprocess(int arr[], int n, int temp[]) { // Store arr[] elements at i and i + n for (int i = 0; i < n; i++) temp[i] = temp[i + n] = arr[...
linear
linear
// CPP implementation of left rotation of // an array K number of times #include <bits/stdc++.h> using namespace std; // Function to left rotate an array k times void leftRotate(int arr[], int n, int k) { // Print array after k rotations for (int i = k; i < k + n; i++) cout << arr[i % n] << " "; } /...
constant
linear
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum value int findMin(int arr[], int n) { int min_ele = arr[0]; // Traversing over array to // find minimum element for (int i = 0; i < n; i++) { if (arr[i] < min_ele) { ...
constant
linear
// C++ program to find minimum // element in a sorted and rotated array #include <bits/stdc++.h> using namespace std; int findMin(int arr[], int low, int high) { // This condition is needed to // handle the case when array is not // rotated at all if (high < low) return arr[0]; // If t...
constant
logn
// C++ program for right rotation of // an array (Reversal Algorithm) #include <bits/stdc++.h> /*Function to reverse arr[] from index start to end*/ void reverseArray(int arr[], int start, int end) { while (start < end) { std::swap(arr[start], arr[end]); start++; ...
constant
linear
// C++ Program to solve queries on Left and Right // Circular shift on array #include <bits/stdc++.h> using namespace std; // Function to solve query of type 1 x. void querytype1(int* toRotate, int times, int n) { // Decreasing the absolute rotation (*toRotate) = ((*toRotate) - times) % n; } // Function to ...
linear
linear
// C++ implementation of left rotation of // an array K number of times #include <bits/stdc++.h> using namespace std; // Function to leftRotate array multiple times void leftRotate(int arr[], int n, int k) { /* To get the starting point of rotated array */ int mod = k % n; // Prints the rotated array fr...
constant
linear
#include <bits/stdc++.h> using namespace std; int findElement(vector<int> arr, int ranges[][2], int rotations, int index) { // Track of the rotation number int n1 = 1; // Track of the row index for the ranges[][] array int i = 0; // Initialize the left side of the ranges[][] array int ...
constant
quadratic
// CPP code to rotate an array // and answer the index query #include <bits/stdc++.h> using namespace std; // Function to compute the element at // given index int findElement(int arr[], int ranges[][2], int rotations, int index) { for (int i = rotations - 1; i >= 0; i--) { // Range[left...
constant
constant
// CPP program to split array and move first // part to end. #include <iostream> using namespace std; void splitArr(int arr[], int n, int k) { for (int i = 0; i < k; i++) { // Rotate array by 1. int x = arr[0]; for (int j = 0; j < n - 1; ++j) arr[j] = arr[j + 1]; arr[...
constant
quadratic
// CPP program to split array and move first // part to end. #include <bits/stdc++.h> using namespace std; // Function to split array and // move first part to end void splitArr(int arr[], int length, int rotation) { int tmp[length * 2] = {0}; for(int i = 0; i < length; i++) { tmp[i] = arr[i]; ...
linear
linear
// C++ program for above approach #include <iostream> using namespace std; // Function to transform the array void fixArray(int ar[], int n) { int i, j, temp; // Iterate over the array for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { // Check is any ar[j] /...
constant
quadratic
// C++ program for rearrange an // array such that arr[i] = i. #include <bits/stdc++.h> using namespace std; // Function to rearrange an array // such that arr[i] = i. void fixArray(int A[], int len) { for (int i = 0; i < len; i++) { if (A[i] != -1 && A[i] != i) { int x = A[i]; ...
constant
linear
#include <iostream> #include <unordered_set> using namespace std; void fixArray(int arr[], int n) { // a set unordered_set<int> s; // Enter each element which is not -1 in set for(int i=0; i<n; i++) { if(arr[i] != -1) s.insert(arr[i]); } // Navigate through array, // and put A[i] =...
linear
linear
// C++ program for rearrange an // array such that arr[i] = i. #include <iostream> using namespace std; void fixArray(int arr[], int n) { int i = 0; while (i < n) { int correct = arr[i]; if (arr[i] != -1 && arr[i] != arr[correct]) { // if array element should be lesser than ...
constant
linear
// C++ program to rearrange the array as per the given // condition #include <bits/stdc++.h> using namespace std; // function to rearrange the array void rearrangeArr(int arr[], int n) { // total even positions int evenPos = n / 2; // total odd positions int oddPos = n - evenPos; int tempArr[n];...
linear
nlogn
#include <bits/stdc++.h> using namespace std; int main(){ int n,i,p,q; int a[]= {1, 2, 1, 4, 5, 6, 8, 8}; n=sizeof(a)/sizeof(a[0]); int b[n]; for(i=0;i<n;i++) b[i]=a[i]; sort(b,b+n); p=0;q=n-1; for(i=n-1;i>=0;i--){ if(i%2!=0){ a[i]=b[q]; q-...
linear
nlogn
/* C++ program to rearrange positive and negative integers in alternate fashion while keeping the order of positive and negative numbers. */ #include <assert.h> #include <iostream> using namespace std; // Utility function to right rotate all elements between // [outofplace, cur] void rightrotate(int arr[], ...
constant
quadratic
// C++ Program to move all zeros to the end #include <bits/stdc++.h> using namespace std; int main() { int A[] = { 5, 6, 0, 4, 6, 0, 9, 0, 8 }; int n = sizeof(A) / sizeof(A[0]); int j = 0; for (int i = 0; i < n; i++) { if (A[i] != 0) { swap(A[j], A[i]); // Partitioning the array ...
constant
linear
# C++ program to shift all zeros # to right most side of array # without affecting order of non-zero # elements # Given list arr = [5, 6, 0, 4, 6, 0, 9, 0, 8] # Storing all non zero values nonZeroValues = [x for x in arr if x != 0] # Storing all zeroes zeroes = [j for j in arr if j == 0] # Updating the answer a...
constant
linear
// C++ implementation to move all zeroes at the end of array #include <iostream> using namespace std; // function to move all zeroes at the end of array void moveZerosToEnd(int arr[], int n) { // Count of non-zero elements int count = 0; // Traverse the array. If arr[i] is non-zero, then // update t...
constant
linear
// C++ program to find minimum swaps required // to club all elements less than or equals // to k together #include <iostream> using namespace std; // Utility function to find minimum swaps // required to club all elements less than // or equals to k together int minSwap(int *arr, int n, int k) { // Find co...
constant
linear
#include <bits/stdc++.h> using namespace std; // Function for finding the minimum number of swaps // required to bring all the numbers less // than or equal to k together. int minSwap(int arr[], int n, int k) { // Initially snowBallsize is 0 int snowBallSize = 0; for (int i = 0; i < n; i++) { ...
constant
linear
// C++ implementation of // the above approach #include <iostream> void printArray(int array[], int length) { std::cout << "["; for(int i = 0; i < length; i++) { std::cout << array[i]; if(i < (length - 1)) std::cout << ", "; else std::cout <<...
quadratic
quadratic
// C++ program to print the array in given order #include <bits/stdc++.h> using namespace std; // Function which arrange the array. void rearrangeArray(int arr[], int n) { // Sorting the array elements sort(arr, arr + n); int tempArr[n]; // To store modified array // Adding numbers from sorted arr...
linear
nlogn
// C++ implementation to rearrange the array elements after // modification #include <bits/stdc++.h> using namespace std; // function which pushes all zeros to end of an array. void pushZerosToEnd(int arr[], int n) { // Count of non-zero elements int count = 0; // Traverse the array. If element encounte...
constant
linear
// C++ program to Rearrange positive and negative // numbers in a array #include <bits/stdc++.h> using namespace std; // A utility function to print an array of size n void printArray(int arr[], int n) { for (int i = 0; i < n; i++) cout<<arr[i]<<" "; } void rotateSubArray(int arr[], int l, int r) { int ...
constant
quadratic
// C++ program to Rearrange positive and negative // numbers in a array #include <stdio.h> // A utility function to print an array of size n void printArray(int arr[], int n) { for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); } // Function to Rearrange positive and negative // number...
constant
quadratic
// C++ program to Rearrange positive and negative // numbers in a array #include <iostream> using namespace std; /* Function to print an array */ void printArray(int A[], int size) { for (int i = 0; i < size; i++) cout << A[i] << " "; cout << endl; } // Merges two subarrays of arr[]. // First subarr...
linear
nlogn
// C++ program to Rearrange positive and negative // numbers in a array #include <bits/stdc++.h> using namespace std; /* Function to print an array */ void printArray(int A[], int size) { for (int i = 0; i < size; i++) cout << A[i] << " "; cout << endl; } /* Function to reverse an array. An array ca...
logn
nlogn
#include <bits/stdc++.h> using namespace std; void Rearrange(int arr[], int n) { stable_partition(arr,arr+n,[](int x){return x<0;}); } int main() { int n=4; int arr[n]={-3, 3, -2, 2}; Rearrange( arr, n); for (int i = 0; i < n; i++) cout << arr[i] << " "; ...
linear
quadratic
#include <iostream> using namespace std; void rearrangePosNegWithOrder(int *arr, int size) { int i = 0, j = 0; while (j < size) { if (arr[j] >= 0) { j++; } else { for (int k = j; k > i; k--) { int temp = arr[k]; arr[k] = arr[k - 1]; ...
constant
linear
// C++ program to rearrange an array in minimum // maximum form #include <bits/stdc++.h> using namespace std; // Prints max at first position, min at second position // second max at third position, second min at fourth // position and so on. void rearrange(int arr[], int n) { // Auxiliary array to hold modified ...
linear
linear
#include <iostream> #include<vector> #include<algorithm> using namespace std; void move(vector<int>& arr){ sort(arr.begin(),arr.end()); } int main() { vector<int> arr = { -1, 2, -3, 4, 5, 6, -7, 8, 9 }; move(arr); for (int e : arr) cout<<e << " "; return 0; } // This code is contributed b...
linear
nlogn
// A C++ program to put all negative // numbers before positive numbers #include <bits/stdc++.h> using namespace std; void rearrange(int arr[], int n) { int j = 0; for (int i = 0; i < n; i++) { if (arr[i] < 0) { if (i != j) swap(arr[i], arr[j]); j++; } ...
constant
linear
// C++ program of the above // approach #include <iostream> using namespace std; // Function to shift all the // negative elements on left side void shiftall(int arr[], int left, int right) { // Loop to iterate over the // array from left to the right while (left<=right) { // Condition...
constant
linear
#include <iostream> using namespace std; // Swap Function. void swap(int &a,int &b){ int temp =a; a=b; b=temp; } // Using Dutch National Flag Algorithm. void reArrange(int arr[],int n){ int low =0,high = n-1; while(low<high){ if(arr[low]<0){ low++; }else if(arr[high]>0){ ...
constant
linear
// C++ program to Move All -ve Element At End // Without changing order Of Array Element #include<bits/stdc++.h> using namespace std; // Moves all -ve element to end of array in // same order. void segregateElements(int arr[], int n) { // Create an empty array to store result int temp[n]; // Traversal a...
linear
linear
// CPP code to rearrange an array such that // even index elements are smaller and odd // index elements are greater than their // next. #include <iostream> using namespace std; void rearrange(int* arr, int n) { for (int i = 0; i < n - 1; i++) { if (i % 2 == 0 && arr[i] > arr[i + 1]) swap(arr[...
constant
linear
// C++ program to rearrange positive and negative // numbers #include <bits/stdc++.h> using namespace std; void rearrange(int a[], int size) { int positive = 0, negative = 1; while (true) { /* Move forward the positive pointer till negative number number not encountered */ whi...
constant
quadratic
// C++ program to rearrange positive // and negative numbers #include<iostream> using namespace std; // Swap function void swap(int* a, int i , int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; return ; } // Print array function void printArray(int* a, int n) { for(int i = 0; i < n; i++) ...
constant
quadratic
// C++ program to update every array element with // multiplication of previous and next numbers in array #include<iostream> using namespace std; void modify(int arr[], int n) { // Nothing to do when array size is 1 if (n <= 1) return; // store current value of arr[0] and update it int prev = ...
constant
linear
// C++ Program to shuffle a given array #include<bits/stdc++.h> #include <stdlib.h> #include <time.h> using namespace std; // A utility function to swap to integers void swap (int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // A utility function to print an array void printArray (int arr[], int n...
constant
linear
// C++ Implementation of the above approach #include <iostream> using namespace std; void arrayEvenAndOdd(int arr[], int n) { int a[n], index = 0; for (int i = 0; i < n; i++) { if (arr[i] % 2 == 0) { a[index] = arr[i]; index++; } } for (int i = 0; i < ...
linear
linear
// CPP code to segregate even odd // numbers in an array #include <bits/stdc++.h> using namespace std; // Function to segregate even odd numbers void arrayEvenAndOdd(int arr[], int n) { int i = -1, j = 0; int t; while (j != n) { if (arr[j] % 2 == 0) { i++; // Swapping e...
constant
linear
// C++ program of above implementation #include <bits/stdc++.h> using namespace std; // Standard partition process of QuickSort(). // It considers the last element as pivot and // oves all smaller element to left of it // and greater elements to right int partition(int* arr, int l, int r) { int x = arr[r], i = ...
constant
nlogn
// STL based C++ program to find k-th smallest // element. #include <bits/stdc++.h> using namespace std; int kthSmallest(int arr[], int n, int k) { // Insert all elements into the set set<int> s; for (int i = 0; i < n; i++) s.insert(arr[i]); // Traverse set and print k-th element auto it = s.begin(); for (i...
linear
nlogn
#include <iostream> using namespace std; // Swap function to interchange // the value of variables x and y int swap(int& x, int& y) { int temp = x; x = y; y = temp; } // Min Heap Class // arr holds reference to an integer // array size indicate the number of // elements in Min Heap class MinHeap { ...
linear
nlogn
#include <bits/stdc++.h> using namespace std; // picks up last element between start and end int findPivot(int a[], int start, int end) { // Selecting the pivot element int pivot = a[end]; // Initially partition-index will be at starting int pIndex = start; for (int i = start; i < end; i++) { ...
constant
nlogn
// C++ code for k largest/ smallest elements in an array #include <bits/stdc++.h> using namespace std; // Function to find k largest array element void kLargest(vector<int>& v, int N, int K) { // Implementation using // a Priority Queue priority_queue<int, vector<int>, greater<int> >pq; for (int i ...
linear
nlogn
#include <bits/stdc++.h> using namespace std; struct Node{ int data; struct Node *left; struct Node *right; }; class Tree{ public: Node *root = NULL; void addNode(int data){ Node *newNode = new Node(); newNode->data = data; if (!root){ ...
linear
nlogn