code stringlengths 195 7.9k | space_complexity stringclasses 6
values | time_complexity stringclasses 7
values |
|---|---|---|
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void merge(int arr1[], int arr2[], int n, int m) {
int i=0;
// while loop till last element of array 1(sorted) is greater than
// first element of array 2(sorted)
while(arr1[n-1]>arr2[0])
{
if(arr1[i]... | constant | linear |
#include <bits/stdc++.h>
using namespace std;
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
void rotate(int a[], int n, int idx)
{
int i;
for (i = 0; i < idx / 2; i++)
swap(a[i], a[idx - 1 - i]);
for (i = idx; i < (n + idx) / 2; i++)
swap(a[i], a[n - 1 - (i - i... | constant | nlogn |
#include <bits/stdc++.h>
using namespace std;
void merge(int arr1[], int arr2[], int n, int m)
{
// two pointer to iterate
int i = 0;
int j = 0;
while (i < n && j < m) {
// if arr1[i] <= arr2[j] then both array is already
// sorted
if (arr1[i] <= arr2[j]) {
i++;
... | constant | quadratic |
// C++ program to merge two sorted arrays without using extra space
#include <bits/stdc++.h>
using namespace std;
void merge(int arr1[], int arr2[], int n, int m)
{
// three pointers to iterate
int i = 0, j = 0, k = 0;
// for euclid's division lemma
int x = 10e7 + 1;
// in this loop we are rearr... | constant | linear |
// C++ program to calculate the
// product of max element of
// first array and min element
// of second array
#include <bits/stdc++.h>
using namespace std;
// Function to calculate
// the product
int minMaxProduct(int arr1[],
int arr2[],
int n1,
int n2)
{
// ... | constant | nlogn |
// C++ program to find the to
// calculate the product of
// max element of first array
// and min element of second array
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the product
int minMaxProduct(int arr1[], int arr2[],
int n1, int n2)
{
// Initialize max of first arr... | constant | linear |
// C++ program to implement linear
// search in unsorted array
#include <bits/stdc++.h>
using namespace std;
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
// If the key is not f... | constant | linear |
// C++ program to implement insert
// operation in an unsorted array.
#include <iostream>
using namespace std;
// Inserts a key in arr[] of given capacity.
// n is current size of arr[]. This
// function returns n + 1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{
... | constant | constant |
// C Program to Insert an element
// at a specific position in an Array
#include <bits/stdc++.h>
using namespace std;
// Function to insert element
// at a specific position
void insertElement(int arr[], int n, int x, int pos)
{
// shift elements to the right
// which are on the right side of pos
for (i... | constant | linear |
// C++ program to implement delete operation in a
// unsorted array
#include <iostream>
using namespace std;
// To search a key to be deleted
int findElement(int arr[], int n, int key);
// Function to delete an element
int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
... | constant | linear |
// C++ program to implement binary search in sorted array
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2; /*low + (high - low)/2;*/
if (key == arr[mid])
return mid;
if (key > ... | logn | logn |
// C++ program to implement insert operation in
// an sorted array.
#include <bits/stdc++.h>
using namespace std;
// Inserts a key in arr[] of given capacity. n is current
// size of arr[]. This function returns n+1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{
... | constant | linear |
// C++ program to implement delete operation in a
// sorted array
#include <bits/stdc++.h>
using namespace std;
// To search a key to be deleted
int binarySearch(int arr[], int low, int high, int key);
/* Function to delete an element */
int deleteElement(int arr[], int n, int key)
{
// Find position of element... | logn | linear |
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find and print pair
bool chkPair(int A[], int size, int x)
{
for (int i = 0; i < (size - 1); i++) {
for (int j = (i + 1); j < size; j++) {
if (A[i] + A[j] == x) {
return 1;
... | constant | quadratic |
// C++ program to check if given array
// has 2 elements whose sum is equal
// to the given value
#include <bits/stdc++.h>
using namespace std;
// Function to check if array has 2 elements
// whose sum is equal to the given value
bool hasArrayTwoCandidates(int A[], int arr_size, int sum)
{
int l, r;
/* So... | constant | nlogn |
// C++ program to check if given array
// has 2 elements whose sum is equal
// to the given value
#include <bits/stdc++.h>
using namespace std;
bool binarySearch(int A[], int low, int high, int searchKey)
{
while (low <= high) {
int m = low + (high - low) / 2;
// Check if searchKey is presen... | constant | nlogn |
// C++ program to check if given array
// has 2 elements whose sum is equal
// to the given value
#include <bits/stdc++.h>
using namespace std;
void printPairs(int arr[], int arr_size, int sum)
{
unordered_set<int> s;
for (int i = 0; i < arr_size; i++) {
int temp = sum - arr[i];
if (s.find... | linear | linear |
// Code in cpp to tell if there exists a pair in array whose
// sum results in x.
#include <iostream>
using namespace std;
// Function to print pairs
void printPairs(int a[], int n, int x)
{
int i;
int rem[x];
// initializing the rem values with 0's.
for (i = 0; i < x; i++)
rem[i] = 0;
// ... | linear | linear |
// C++ program to search an element in an array
// where difference between adjacent elements is atmost k
#include<bits/stdc++.h>
using namespace std;
// x is the element to be searched in arr[0..n-1]
// such that all elements differ by at-most k.
int search(int arr[], int n, int x, int k)
{
// Traverse the given... | constant | linear |
// C++ program to print common elements in three arrays
#include <bits/stdc++.h>
using namespace std;
// This function prints common elements in ar1
void findCommon(int ar1[], int ar2[], int ar3[], int n1,
int n2, int n3)
{
// Initialize starting indexes for ar1[], ar2[] and
// ar3[]
int i... | constant | linear |
// C++ program to print common elements in three arrays
#include <bits/stdc++.h>
using namespace std;
// This function prints common elements in ar1
void findCommon(int ar1[], int ar2[], int ar3[], int n1, int n2, int n3)
{
// Initialize starting indexes for ar1[], ar2[] and ar3[]
int i = 0, j = 0, k = 0;
... | constant | linear |
#include <bits/stdc++.h>
using namespace std;
void findCommon(int a[], int b[], int c[], int n1, int n2,
int n3)
{
// three sets to maintain frequency of elements
unordered_set<int> uset, uset2, uset3;
for (int i = 0; i < n1; i++) {
uset.insert(a[i]);
}
for (int i = 0; i < ... | linear | linear |
// C++ program to find the only repeating
// element in an array where elements are
// from 1 to N-1.
#include <bits/stdc++.h>
using namespace std;
int findRepeating(int arr[], int N)
{
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
if (arr[i] == arr[j])
return... | constant | quadratic |
// CPP program to find the only repeating
// element in an array where elements are
// from 1 to N-1.
#include <bits/stdc++.h>
using namespace std;
int findRepeating(int arr[], int N)
{
sort(arr, arr + N); // sort array
for (int i = 0; i < N; i++) {
// compare array element with its index
... | constant | nlogn |
// C++ program to find the only repeating
// element in an array where elements are
// from 1 to N-1.
#include <bits/stdc++.h>
using namespace std;
int findRepeating(int arr[], int N)
{
unordered_set<int> s;
for (int i = 0; i < N; i++) {
if (s.find(arr[i]) != s.end())
return arr[i];
... | linear | linear |
// CPP program to find the only repeating
// element in an array where elements are
// from 1 to N-1.
#include <bits/stdc++.h>
using namespace std;
int findRepeating(int arr[], int N)
{
// Find array sum and subtract sum
// first N-1 natural numbers from it
// to find the result.
return accumulate(arr... | constant | linear |
// CPP program to find the only repeating
// element in an array where elements are
// from 1 to N-1.
#include <bits/stdc++.h>
using namespace std;
int findRepeating(int arr[], int N)
{
// res is going to store value of
// 1 ^ 2 ^ 3 .. ^ (N-1) ^ arr[0] ^
// arr[1] ^ .... arr[N-1]
int res = 0;
fo... | constant | linear |
// CPP program to find the only
// repeating element in an array
// where elements are from 1 to N-1.
#include <bits/stdc++.h>
using namespace std;
// Function to find repeated element
int findRepeating(int arr[], int N)
{
int missingElement = 0;
// indexing based
for (int i = 0; i < N; i++) {
... | constant | linear |
#include <bits/stdc++.h>
using namespace std;
int findDuplicate(vector<int>& nums)
{
int slow = nums[0];
int fast = nums[0];
do {
slow = nums[slow];
fast = nums[nums[fast]];
} while (slow != fast);
fast = nums[0];
while (slow != fast) {
slow = nums[slow];
fa... | constant | linear |
// C++ program to find the array element
// that appears only once
#include <iostream>
using namespace std;
// Function to find the
int findSingle(int A[], int ar_size)
{
// Iterate over every element
for (int i = 0; i < ar_size; i++) {
// Initialize count to 0
int count = 0;
f... | constant | quadratic |
// C++ program to find the array element
// that appears only once
#include <iostream>
using namespace std;
int findSingle(int ar[], int ar_size)
{
// Do XOR of all elements and return
int res = ar[0];
for (int i = 1; i < ar_size; i++)
res = res ^ ar[i];
return res;
}
// Driver code
int ma... | constant | linear |
// C++ program to find
// element that appears once
#include <bits/stdc++.h>
using namespace std;
// function which find number
int singleNumber(int nums[],int n)
{
map<int,int> m;
long sum1 = 0,sum2 = 0;
for(int i = 0; i < n; i++)
{
if(m[nums[i]] == 0)
{
sum1 += nums[i... | linear | nlogn |
#include <bits/stdc++.h>
using namespace std;
int singleelement(int arr[], int n)
{
int low = 0, high = n - 2;
int mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == arr[mid ^ 1])
low = mid + 1;
else
high = mid - 1;
}
return arr[low];
}... | constant | nlogn |
// C++ Program to find max subarray
// sum excluding some elements
#include <bits/stdc++.h>
using namespace std;
// Function to check the element
// present in array B
bool isPresent(int B[], int m, int x)
{
for (int i = 0; i < m; i++)
if (B[i] == x)
return true;
return false;
}
// Utili... | constant | quadratic |
// C++ Program to find max subarray
// sum excluding some elements
#include <bits/stdc++.h>
using namespace std;
// Utility function for findMaxSubarraySum()
// with the following parameters
// A => Array A,
// B => Array B,
// n => Number of elements in Array A,
// m => Number of elements in Array B
int findMaxSubar... | constant | nlogn |
// C++ Program implementation of the
// above idea
#include<bits/stdc++.h>
using namespace std;
// Function to calculate the max sum of contiguous
// subarray of B whose elements are not present in A
int findMaxSubarraySum(vector<int> A,vector<int> B)
{
unordered_map<int,int> m;
// mark all the elements... | linear | linear |
// CPP program to find
// maximum equilibrium sum.
#include <bits/stdc++.h>
using namespace std;
// Function to find
// maximum equilibrium sum.
int findMaxSum(int arr[], int n)
{
int res = INT_MIN;
for (int i = 0; i < n; i++)
{
int prefix_sum = arr[i];
for (int j = 0; j < i; j++)
prefix_s... | linear | quadratic |
// CPP program to find
// maximum equilibrium sum.
#include <bits/stdc++.h>
using namespace std;
// Function to find maximum
// equilibrium sum.
int findMaxSum(int arr[], int n)
{
// Array to store prefix sum.
int preSum[n];
// Array to store suffix sum.
int suffSum[n];
// Variable to store ma... | linear | linear |
// CPP program to find
// maximum equilibrium sum.
#include <bits/stdc++.h>
using namespace std;
// Function to find
// maximum equilibrium sum.
int findMaxSum(int arr[], int n)
{
int sum = accumulate(arr, arr + n, 0);
int prefix_sum = 0, res = INT_MIN;
for (int i = 0; i < n; i++)
{
prefix_sum += ... | constant | linear |
// C++ program to find equilibrium
// index of an array
#include <bits/stdc++.h>
using namespace std;
int equilibrium(int arr[], int n)
{
int i, j;
int leftsum, rightsum;
/* Check for indexes one by one until
an equilibrium index is found */
for (i = 0; i < n; ++i) {
/* get left sum */... | constant | quadratic |
// C++ program to find equilibrium
// index of an array
#include <bits/stdc++.h>
using namespace std;
int equilibrium(int arr[], int n)
{
int sum = 0; // initialize sum of whole array
int leftsum = 0; // initialize leftsum
/* Find sum of the whole array */
for (int i = 0; i < n; ++i)
sum += ... | constant | linear |
// C++ program to find equilibrium index of an array
#include <bits/stdc++.h>
using namespace std;
int equilibrium(int a[], int n)
{
if (n == 1)
return (0);
int forward[n] = { 0 };
int rev[n] = { 0 };
// Taking the prefixsum from front end array
for (int i = 0; i < n; i++) {
if (... | linear | linear |
#include<iostream>
using namespace std;
/*C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
int j;
for (j = i+1; j < size; j++)
{
if (arr[i] <=arr[j])
break;
}
if (j == s... | constant | quadratic |
#include <iostream>
using namespace std;
/* C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
int max_from_right = arr[size-1];
/* Rightmost element is always leader */
cout << max_from_right << " ";
for (int i = size-2; i >= 0; i--)
{
if (max_f... | constant | linear |
#include <bits/stdc++.h>
using namespace std;
/* C++ Function to print leaders in an array */
void printLeaders(int arr[], int size)
{
/* create stack to store leaders*/
stack<int> sk;
sk.push(arr[size-1]);
for (int i = size-2; i >= 0; i--)
{
if(arr[i] >= sk.top())
{ ... | linear | linear |
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
/* Function to get index of ceiling of x in arr[low..high] */
int ceilSearch(int arr[], int low, int high, int x)
{
int i;
/* If x is smaller than or equal to first element,
then return the first element ... | constant | linear |
#include <bits/stdc++.h>
using namespace std;
/* Function to get index of
ceiling of x in arr[low..high]*/
int ceilSearch(int arr[], int low, int high, int x)
{
int mid;
/* If x is smaller than
or equal to the first element,
then return the first element */
if (x <= arr[low])
re... | constant | logn |
#include <bits/stdc++.h>
using namespace std;
/* Function to get index of
ceiling of x in arr[low..high]*/
int ceilSearch(int arr[], int low, int high, int x)
{
// base condition if length of arr == 0 then return -1
if (x == 0) {
return -1;
}
int mid;
// this while loop function... | constant | logn |
// Hashing based C++ program to find if there
// is a majority element in input array.
#include <bits/stdc++.h>
using namespace std;
// Returns true if there is a majority element
// in a[]
bool isMajority(int a[], int n)
{
// Insert all elements in a hash table
unordered_map<int, int> mp;
for (int i = 0;... | linear | linear |
// A C++ program to find a peak element
// using divide and conquer
#include <bits/stdc++.h>
using namespace std;
// A binary search based function
// that returns index of a peak element
int findPeakUtil(int arr[], int low,
int high, int n)
{
// Find index of middle element
// low + (high - ... | logn | logn |
// A C++ program to find a peak element
// using divide and conquer
#include <bits/stdc++.h>
using namespace std;
// A binary search based function
// that returns index of a peak element
int findPeak(int arr[], int n)
{
int l = 0;
int r = n-1;
int mid;
while (l <= r) {
// finding ... | constant | logn |
// C++ program to Find the two repeating
// elements in a given array
#include <bits/stdc++.h>
using namespace std;
void printTwoRepeatNumber(int arr[], int size)
{
int i, j;
cout << "Repeating elements are ";
for (i = 0; i < size; i++) {
for (j = i + 1; j < size; j++) {
if (arr[i] == ... | constant | quadratic |
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
void printRepeating(int arr[], int size)
{
int* count = new int[sizeof(int) * (size - 2)];
int i;
cout << " Repeating elements are ";
for (i = 0; i < size; i++) {
if (count[arr[i]] == 1)
cout <<... | linear | linear |
#include <bits/stdc++.h>
using namespace std;
/* function to get factorial of n */
int fact(int n);
void printRepeating(int arr[], int size)
{
int S = 0; /* S is for sum of elements in arr[] */
int P = 1; /* P is for product of elements in arr[] */
int x, y; /* x and y are two repeating elements */
... | constant | linear |
#include <bits/stdc++.h>
using namespace std;
void printRepeating(int arr[], int size)
{
int Xor = arr[0]; /* Will hold Xor of all elements */
int set_bit_no; /* Will have only single set bit of Xor
*/
int i;
int n = size - 2;
int x = 0, y = 0;
/* Get the Xor of all elem... | constant | linear |
#include <bits/stdc++.h>
using namespace std;
void printRepeating(int arr[], int size)
{
int i;
cout << "Repeating elements are ";
for (i = 0; i < size; i++) {
if (arr[abs(arr[i])] > 0)
arr[abs(arr[i])] = -arr[abs(arr[i])];
else
cout << abs(arr[i]) << " ";
}... | constant | linear |
#include <bits/stdc++.h>
using namespace std;
void twoRepeated(int arr[], int N)
{
int m = N - 1;
for (int i = 0; i < N; i++) {
arr[arr[i] % m - 1] += m;
if ((arr[arr[i] % m - 1] / m) == 2)
cout << arr[i] % m << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 4, 2, 4, 5,... | constant | linear |
// C++ program to Find the two repeating
// elements in a given array
#include <bits/stdc++.h>
using namespace std;
void printRepeating(int arr[], int size)
{
unordered_set<int> s;
cout << "Repeating elements are ";
for (int i = 0; i < size; i++) {
if (s.empty() == false && s.find(arr[i]) != s.end... | linear | linear |
/* A simple program to print subarray
with sum as given sum */
#include <bits/stdc++.h>
using namespace std;
/* Returns true if the there is a subarray
of arr[] with sum equal to 'sum' otherwise
returns false. Also, prints the result */
void subArraySum(int arr[], int n, int sum)
{
// Pick a starting point
... | constant | quadratic |
/* An efficient program to print
subarray with sum as given sum */
#include <iostream>
using namespace std;
/* Returns true if the there is a subarray of
arr[] with a sum equal to 'sum' otherwise
returns false. Also, prints the result */
int subArraySum(int arr[], int n, int sum)
{
/* Initialize currentSum as val... | constant | linear |
// C++ implementation of smallest difference triplet
#include <bits/stdc++.h>
using namespace std;
// function to find maximum number
int maximum(int a, int b, int c)
{
return max(max(a, b), c);
}
// function to find minimum number
int minimum(int a, int b, int c)
{
return min(min(a, b), c);
}
// Finds and ... | constant | nlogn |
// A simple C++ program to find three elements
// whose sum is equal to zero
#include <bits/stdc++.h>
using namespace std;
// Prints all triplets in arr[] with 0 sum
void findTriplets(int arr[], int n)
{
bool found = false;
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
... | constant | cubic |
// C++ program to find triplets in a given
// array whose sum is zero
#include <bits/stdc++.h>
using namespace std;
// function to print triplets with 0 sum
void findTriplets(int arr[], int n)
{
bool found = false;
for (int i = 0; i < n - 1; i++) {
// Find all pairs with sum equals to
// "-a... | linear | quadratic |
// C++ program to find triplets in a given
// array whose sum is zero
#include <bits/stdc++.h>
using namespace std;
// function to print triplets with 0 sum
void findTriplets(int arr[], int n)
{
bool found = false;
// sort array elements
sort(arr, arr + n);
for (int i = 0; i < n - 1; i++) {
... | constant | quadratic |
// C++ program to rotate a matrix
#include <bits/stdc++.h>
#define R 4
#define C 4
using namespace std;
// A function to rotate a matrix mat[][] of size R x C.
// Initially, m = R and n = C
void rotatematrix(int m, int n, int mat[R][C])
{
int row = 0, col = 0;
int prev, curr;
/*
row - Starting row... | constant | quadratic |
// 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 |
// C++ program to check if all rows of a matrix
// are rotations of each other
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
// Returns true if all rows of mat[0..n-1][0..n-1]
// are rotations of each other.
bool isPermutedMatrix( int mat[MAX][MAX], int n)
{
// Creating a string that contain... | linear | cubic |
// C++ implementation to sort the given matrix
#include <bits/stdc++.h>
using namespace std;
#define SIZE 10
// function to sort the given matrix
void sortMat(int mat[SIZE][SIZE], int n)
{
// temporary matrix of size n^2
int temp[n * n];
int k = 0;
// copy the elements of matrix one by one
// ... | quadratic | quadratic |
// C++ program to find median of a matrix
// sorted row wise
#include<bits/stdc++.h>
using namespace std;
const int MAX = 100;
// function to find median in the matrix
int binaryMedian(int m[][MAX], int r ,int c)
{
int min = INT_MAX, max = INT_MIN;
for (int i=0; i<r; i++)
{
// Finding the minimu... | constant | nlogn |
// C++ program to print Lower
// triangular and Upper triangular
// matrix of an array
#include<iostream>
using namespace std;
// Function to form
// lower triangular matrix
void lower(int matrix[3][3], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{... | constant | quadratic |
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
vector<int> spiralOrder(vector<vector<int> >& matrix)
{
int m = matrix.size(), n = matrix[0].size();
vector<int> ans;
if (m == 0)
return ans;
vector<vector<bool> > seen(m, vector<bool>(n, false));
int... | linear | linear |
// C++ Program to print a matrix spirally
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
void spiralPrint(int m, int n, int a[R][C])
{
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
... | constant | quadratic |
// C++. program for the above approach
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
// Function for printing matrix in spiral
// form i, j: Start index of matrix, row
// and column respectively m, n: End index
// of matrix row and column respectively
void print(int arr[R][C], int i, int j, ... | constant | quadratic |
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
bool isInBounds(int i, int j)
{
if (i < 0 || i >= R || j < 0 || j >= C)
return false;
return true;
}
// check if the position is blocked
bool isBlocked(int matrix[R][C], int i, int j)
{
... | constant | quadratic |
// C++ program to find unique element in matrix
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
// function that calculate unique element
int unique(int mat[R][C], int n, int m)
{
// declare map for hashing
unordered_map<int, int> mp;
for (int i = 0; i < n; i++)
for (int j ... | quadratic | quadratic |
// CPP Program to swap diagonal of a matrix
#include <bits/stdc++.h>
using namespace std;
// size of square matrix
#define N 3
// Function to swap diagonal of matrix
void swapDiagonal(int matrix[][N]) {
for (int i = 0; i < N; i++)
swap(matrix[i][i], matrix[i][N - i - 1]);
}
// Driver Code
int main() {
int... | constant | quadratic |
// CPP program for finding max path in matrix
#include <bits/stdc++.h>
#define N 4
#define M 6
using namespace std;
// To calculate max path in matrix
int findMaxPath(int mat[][M])
{
for (int i = 1; i < N; i++) {
for (int j = 0; j < M; j++) {
// When all paths are possible
if (... | constant | quadratic |
// C++ code to move matrix elements
// in given direction with add
// element with same value
#include <bits/stdc++.h>
using namespace std;
#define MAX 50
// Function to shift the matrix
// in the given direction
void moveMatrix(char d[], int n,
int a[MAX][MAX])
{
// For right shift move.
... | linear | quadratic |
// C++ program to find number of subarrays
// having product exactly equal to k.
#include <bits/stdc++.h>
using namespace std;
// Function to find number of subarrays
// having product equal to 1.
int countOne(int arr[], int n){
int i = 0;
// To store number of ones in
// current segment of all 1s.
... | constant | linear |
// CPP program to find all those
// elements of arr1[] that are not
// present in arr2[]
#include <iostream>
using namespace std;
void relativeComplement(int arr1[], int arr2[],
int n, int m) {
int i = 0, j = 0;
while (i < n && j < m) {
// If current element in arr2[] is
... | constant | linear |
// Program to make all array equal
#include <bits/stdc++.h>
using namespace std;
// function for calculating min operations
int minOps(int arr[], int n, int k)
{
// max elements of array
int max = *max_element(arr, arr + n);
int res = 0;
// iterate for all elements
for (int i = 0; i < n; i++) {
... | constant | linear |
// C++ code for above approach
#include<bits/stdc++.h>
using namespace std;
int solve(int A[], int B[], int C[], int i, int j, int k)
{
int min_diff, current_diff, max_term;
// calculating min difference from last
// index of lists
min_diff = abs(max(A[i], max(B[j], C[k]))
... | constant | linear |
/* A program to convert Binary Tree to Binary Search Tree */
#include <iostream>
using namespace std;
/* A binary tree node structure */
struct node {
int data;
struct node* left;
struct node* right;
};
/* A helper function that stores inorder
traversal of a tree rooted with node */
void storeInorder... | linear | nlogn |
// C++ program to print BST in given range
#include<bits/stdc++.h>
using namespace std;
/* A Binary Tree node */
class TNode
{
public:
int data;
TNode* left;
TNode* right;
};
TNode* newNode(int data);
/* A function that constructs Balanced
Binary Search Tree from a sorted array */
TNode* sortedArr... | logn | linear |
// C++ program to transform a BST to sum tree
#include<iostream>
using namespace std;
// A BST node
struct Node
{
int data;
struct Node *left, *right;
};
// A utility function to create a new Binary Tree Node
struct Node *newNode(int item)
{
struct Node *temp = new Node;
temp->data = item;
temp... | linear | linear |
//C++ Program to convert a BST into a Min-Heap
// in O(n) time and in-place
#include <bits/stdc++.h>
using namespace std;
// Node for BST/Min-Heap
struct Node
{
int data;
Node *left, *right;
};
// Utility function for allocating node for BST
Node* newNode(int data)
{
Node* node = new Node;
node->dat... | linear | linear |
// C++ implementation to convert the given
// BST to Min Heap
#include <bits/stdc++.h>
using namespace std;
// Structure of a node of BST
struct Node {
int data;
Node *left, *right;
};
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
struct Node... | linear | linear |
// C++ implementation to construct a BST
// from its level order traversal
#include <bits/stdc++.h>
using namespace std;
// node of a BST
struct Node {
int data;
Node *left, *right;
};
// function to get a new node
Node* getNode(int data)
{
// Allocate memory
Node* newNode = (Node*)malloc(sizeof(N... | linear | nlogn |
// C++ implementation to construct a BST
// from its level order traversal
#include <bits/stdc++.h>
using namespace std;
// node of a BST
struct Node {
int data;
Node *left, *right;
Node(int x)
{
data = x;
right = NULL;
left = NULL;
}
};
// Function to construct a BST... | linear | linear |
/* CPP program to convert a Binary tree to BST
using sets as containers. */
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
// function to store the nodes in set while
// doing inorder traversal.
void storeinorderInSet(Node* root, set<int>& s)
{
if ... | 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 |
#include <bits/stdc++.h>
using namespace std;
// Structure of a BST Node
class node
{
public:
int data;
node *left;
node *right;
};
//.................... START OF STACK RELATED STUFF....................
// A stack node
class snode
{
public:
node *t;
snode *next;
};
// Function to add ... | logn | linear |
#include <bits/stdc++.h>
using namespace std;
// Structure of a BST Node
class Node {
public:
int val;
Node* left;
Node* right;
};
/* Utility function to create a new Binary Tree Node */
Node* newNode(int data)
{
Node* temp = new Node;
temp->val = data;
temp->left = nullptr;
temp->right ... | logn | linear |
// C++ program to find minimum value node in binary search
// 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;
struct node* right;
};
/* Helper function that allocates a ne... | linear | linear |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.