code stringlengths 195 7.9k | space_complexity stringclasses 6
values | time_complexity stringclasses 7
values |
|---|---|---|
// C++ program to find maximum
// in arr[] of size n
#include <bits/stdc++.h>
using namespace std;
int largest(int arr[], int n)
{
int i;
// Initialize maximum element
int max = arr[0];
// Traverse array elements
// from second and compare
// every element with current max
fo... | constant | linear |
// C++ program to find maximum
// in arr[] of size n
#include <bits/stdc++.h>
using namespace std;
int largest(int arr[], int n, int i)
{
// last index
// return the element
if (i == n - 1) {
return arr[i];
}
// find the maximum from rest of the array
int recMax = largest(arr, n, i... | linear | linear |
// C++ program to find maximum in arr[] of size n
#include <bits/stdc++.h>
using namespace std;
// returns maximum in arr[] of size n
int largest(int arr[], int n)
{
return *max_element(arr, arr+n);
}
int main()
{
int arr[] = {10, 324, 45, 90, 9808};
int n = sizeof(arr)/sizeof(arr[0]);
cout << la... | constant | linear |
// C++ program for find the largest
// three elements in an array
#include <bits/stdc++.h>
using namespace std;
// Function to print three largest elements
void print3largest(int arr[], int arr_size)
{
int first, second, third;
// There should be atleast three elements
if (arr_size < 3)
{
co... | constant | linear |
// C++ code to find largest three elements in an array
#include <bits/stdc++.h>
using namespace std;
void find3largest(int arr[], int n)
{
sort(arr, arr + n); // It uses Tuned Quicksort with
// avg. case Time complexity = O(nLogn)
int check = 0, count = 1;
for (int i = 1; i <= n; i++) {
if (... | constant | nlogn |
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> V = { 11, 65, 193, 36, 209, 664, 32 };
partial_sort(V.begin(), V.begin() + 3, V.end(), greater<int>());
cout << "first = " << V[0] << "\n";
cout << "second = " << V[1] << "\n";
cout << "third = " << V[2] << "\n";
return 0;
... | constant | nlogn |
// Simple C++ program to find
// all elements in array which
// have at-least two greater
// elements itself.
#include<bits/stdc++.h>
using namespace std;
void findElements(int arr[], int n)
{
// Pick elements one by one and
// count greater elements. If
// count is more than 2, print
// that element.... | constant | quadratic |
// Sorting based C++ program to
// find all elements in array
// which have atleast two greater
// elements itself.
#include<bits/stdc++.h>
using namespace std;
void findElements(int arr[], int n)
{
sort(arr, arr + n);
for (int i = 0; i < n - 2; i++)
cout << arr[i] << " ";
}
// Driver Code
int main()
... | constant | nlogn |
// C++ program to find all elements
// in array which have atleast two
// greater elements itself.
#include<bits/stdc++.h>
using namespace std;
void findElements(int arr[], int n)
{
int first = INT_MIN,
second = INT_MIN;
for (int i = 0; i < n; i++)
{
/* If current element is smaller
... | constant | linear |
// CPP program to find mean and median of
// an array
#include <bits/stdc++.h>
using namespace std;
// Function for calculating mean
double findMean(int a[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];
return (double)sum / (double)n;
}
// Function for calculating median
doub... | constant | nlogn |
// C++ program to find med in
// stream of running integers
#include<bits/stdc++.h>
using namespace std;
// function to calculate med of stream
void printMedians(double arr[], int n)
{
// max heap to store the smaller half elements
priority_queue<double> s;
// min heap to store the greater half elemen... | linear | nlogn |
// CPP program to find minimum product of
// k elements in an array
#include <bits/stdc++.h>
using namespace std;
int minProduct(int arr[], int n, int k)
{
priority_queue<int, vector<int>, greater<int> > pq;
for (int i = 0; i < n; i++)
pq.push(arr[i]);
int count = 0, ans = 1;
// One by one... | linear | nlogn |
// A simple C++ program to find N maximum
// combinations from two arrays,
#include <bits/stdc++.h>
using namespace std;
// function to display first N maximum sum
// combinations
void KMaxCombinations(int A[], int B[],
int N, int K)
{
// max heap.
priority_queue<int> pq;
// insert... | quadratic | quadratic |
// An efficient C++ program to find top K elements
// from two arrays.
#include <bits/stdc++.h>
using namespace std;
// Function prints k maximum possible combinations
void KMaxCombinations(vector<int>& A,
vector<int>& B, int K)
{
// sort both arrays A and B
sort(A.begin(), A.end());
... | linear | nlogn |
// CPP for printing smallest k numbers in order
#include <algorithm>
#include <iostream>
using namespace std;
// Function to print smallest k numbers
// in arr[0..n-1]
void printSmall(int arr[], int n, int k)
{
// For each arr[i] find whether
// it is a part of n-smallest
// with insertion sort concept
... | constant | quadratic |
// C++ program to prints first k pairs with least sum from two
// arrays.
#include<bits/stdc++.h>
using namespace std;
// Function to find k pairs with least sum such
// that one element of a pair is from arr1[] and
// other element is from arr2[]
void kSmallestPair(int arr1[], int n1, int arr2[],
... | linear | linear |
// C++ program to Prints
// first k pairs with
// least sum from two arrays.
#include <bits/stdc++.h>
using namespace std;
// Function to find k pairs
// with least sum such
// that one element of a pair
// is from arr1[] and
// other element is from arr2[]
void kSmallestPair(vector<int> A, vector<int> B, int K)
{
... | linear | nlogn |
// C++ program to find k-th absolute difference
// between two elements
#include<bits/stdc++.h>
using namespace std;
// returns number of pairs with absolute difference
// less than or equal to mid.
int countPairs(int *a, int n, int mid)
{
int res = 0;
for (int i = 0; i < n; ++i)
// Upper bound retu... | constant | nlogn |
// C++ program to find second largest element in an array
#include <bits/stdc++.h>
using namespace std;
/* Function to print the second largest elements */
void print2largest(int arr[], int arr_size)
{
int i, first, second;
/* There should be atleast two elements */
if (arr_size < 2) {
printf(" ... | constant | nlogn |
// C++ program to find the second largest element in the array
#include <iostream>
using namespace std;
int secondLargest(int arr[], int n) {
int largest = 0, secondLargest = -1;
// finding the largest element in the array
for (int i = 1; i < n; i++) {
if (arr[i] > arr[largest])
la... | constant | linear |
// C++ program to find the second largest element
#include <iostream>
using namespace std;
// returns the index of second largest
// if second largest didn't exist return -1
int secondLargest(int arr[], int n) {
int first = 0, second = -1;
for (int i = 1; i < n; i++) {
if (arr[i] > arr[first]) {
... | constant | linear |
// C++ implementation to find k numbers with most
// occurrences in the given array
#include <bits/stdc++.h>
using namespace std;
// Comparison function to sort the 'freq_arr[]'
bool compare(pair<int, int> p1, pair<int, int> p2)
{
// If frequencies of two elements are same
// then the larger number should com... | linear | nlogn |
// C++ program to find k numbers with most
// occurrences in the given array
#include <bits/stdc++.h>
using namespace std;
// Function to print the k numbers with most occurrences
void print_N_mostFrequentNumber(int arr[], int N, int K)
{
// HashMap to store count of the elements
unordered_map<int, int> ele... | linear | linear |
//C++ simple approach to print smallest
//and second smallest element.
#include<bits/stdc++.h>
using namespace std;
int main() {
int arr[]={111, 13, 25, 9, 34, 1};
int n=sizeof(arr)/sizeof(arr[0]);
//sorting the array using
//in-built sort function
sort(arr,arr+n);
//printing the desired element
cout<<"smallest element... | constant | nlogn |
// C++ program to find smallest and
// second smallest elements
#include <bits/stdc++.h>
using namespace std; /* For INT_MAX */
void print2Smallest(int arr[], int arr_size)
{
int i, first, second;
/* There should be atleast two elements */
if (arr_size < 2)
{
cout<<" Invalid Input ";
... | constant | linear |
// C++ program to find the smallest elements
// missing in a sorted array.
#include<bits/stdc++.h>
using namespace std;
int findFirstMissing(int array[],
int start, int end)
{
if (start > end)
return end + 1;
if (start != array[start])
return start;
int mid = (start... | logn | logn |
//C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Program to find missing element
int findFirstMissing(vector<int> arr , int start ,
int end,int first)
{
if (start < end)
{
int mid = (start + end) / 2;
/** Index matches with value
at... | logn | logn |
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum sum
int findMaxSum(vector<int> arr, int N)
{
// Declare dp array
int dp[N][2];
if (N == 1) {
return arr[0];
}
// Initialize the values in dp array
dp[0][0] = 0;
... | linear | linear |
// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to return max sum such that
// no two elements are adjacent
int FindMaxSum(vector<int> arr, int n)
{
int incl = arr[0];
int excl = 0;
int excl_new;
int i;
for (i = 1; i < n; i++) {
// ... | constant | linear |
// C++ program to demonstrate working of Square Root
// Decomposition.
#include "iostream"
#include "math.h"
using namespace std;
#define MAXN 10000
#define SQRSIZE 100
int arr[MAXN]; // original array
int block[SQRSIZE]; // decomposed array
int blk_sz; // block size
/... | linear | linear |
// C++ program to do range minimum query
// using sparse table
#include <bits/stdc++.h>
using namespace std;
#define MAX 500
// lookup[i][j] is going to store minimum
// value in arr[i..j]. Ideally lookup table
// size should not be fixed and should be
// determined using n Log n. It is kept
// constant to keep code ... | nlogn | nlogn |
// C++ program to do range minimum query
// using sparse table
#include <bits/stdc++.h>
using namespace std;
#define MAX 500
// lookup[i][j] is going to store GCD of
// arr[i..j]. Ideally lookup table
// size should not be fixed and should be
// determined using n Log n. It is kept
// constant to keep code simple.
in... | nlogn | nlogn |
// C++ program to find total count of an element
// in a range
#include<bits/stdc++.h>
using namespace std;
// Returns count of element in arr[left-1..right-1]
int findFrequency(int arr[], int n, int left,
int right, int element)
{
int count = 0;
for (int i=left-1; i<=right; ++i)
... | constant | linear |
// C++ program to get updated array after many array range
// add operation
#include <bits/stdc++.h>
using namespace std;
// Utility method to add value val, to range [lo, hi]
void add(int arr[], int N, int lo, int hi, int val)
{
arr[lo] += val;
if (hi != N - 1)
arr[hi + 1] -= val;
}
// Utility me... | constant | linear |
// C++ program for queries of GCD excluding
// given range of elements.
#include<bits/stdc++.h>
using namespace std;
// Filling the prefix and suffix array
void FillPrefixSuffix(int prefix[], int arr[],
int suffix[], int n)
{
// Filling the prefix array following relation
// prefi... | linear | nlogn |
// C++ implementation of finding number
// represented by binary subarray
#include <bits/stdc++.h>
using namespace std;
// Fills pre[]
void precompute(int arr[], int n, int pre[])
{
memset(pre, 0, n * sizeof(int));
pre[n - 1] = arr[n - 1] * pow(2, 0);
for (int i = n - 2; i >= 0; i--)
pre[i] = pre[... | linear | linear |
// CPP program to perform range queries over range
// queries.
#include <bits/stdc++.h>
#define max 10000
using namespace std;
// For prefix sum array
void update(int arr[], int l)
{
arr[l] += arr[l - 1];
}
// This function is used to apply square root
// decomposition in the record array
void record_func(int b... | linear | logn |
// CPP program to count the number of indexes
// in range L R such that Ai = Ai+1
#include <bits/stdc++.h>
using namespace std;
// function that answers every query in O(r-l)
int answer_query(int a[], int n, int l, int r)
{
// traverse from l to r and count
// the required indexes
int count = 0;
for (... | constant | constant |
// CPP program to count the number of indexes
// in range L R such that Ai=Ai+1
#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
// array to store count of index from 0 to
// i that obey condition
int prefixans[N];
// precomputing prefixans[] array
int countIndex(int a[], int n)
{
// traverse t... | linear | linear |
// C++ program to print largest contiguous array sum
#include <bits/stdc++.h>
using namespace std;
int maxSubArraySum(int a[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < size; i++) {
max_ending_here = max_ending_here + a[i];
if (max_so_far < max_ending_her... | constant | linear |
// C++ program to print largest contiguous array sum
#include <climits>
#include <iostream>
using namespace std;
void maxSubArraySum(int a[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0,
start = 0, end = 0, s = 0;
for (int i = 0; i < size; i++) {
max_ending_here += a[i];
... | constant | linear |
// C++ program to find maximum
// possible profit with at most
// two transactions
#include <bits/stdc++.h>
using namespace std;
// Returns maximum profit with
// two transactions on a given
// list of stock prices, price[0..n-1]
int maxProfit(int price[], int n)
{
// Create profit array and
// initialize it ... | linear | linear |
#include <iostream>
#include<climits>
using namespace std;
int maxtwobuysell(int arr[],int size) {
int first_buy = INT_MIN;
int first_sell = 0;
int second_buy = INT_MIN;
int second_sell = 0;
for(int i=0;i<size;i++) {
first_buy = max(first_buy,-arr[i]);//we set p... | constant | linear |
//C++ code of Naive approach to
//find subarray with minimum average
#include<bits/stdc++.h>
using namespace std;
//function to find subarray
void findsubarrayleast(int arr[],int k,int n){
int min=INT_MAX,minindex;
for (int i = 0; i <= n-k; i++)
{
int sum=0;
for (int j = i; j < i+k; j++)
... | constant | quadratic |
// A Simple C++ program to find minimum average subarray
#include <bits/stdc++.h>
using namespace std;
// Prints beginning and ending indexes of subarray
// of size k with minimum average
void findMinAvgSubarray(int arr[], int n, int k)
{
// k must be smaller than or equal to n
if (n < k)
return;
... | constant | linear |
// C++ program to Find the minimum
// distance between two numbers
#include <bits/stdc++.h>
using namespace std;
int minDist(int arr[], int n, int x, int y)
{
int i, j;
int min_dist = INT_MAX;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if ((x == arr[i] && y == arr[j]
... | constant | quadratic |
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
int minDist(int arr[], int n, int x, int y)
{
//previous index and min distance
int p = -1, min_dist = INT_MAX;
for(int i=0 ; i<n ; i++)
{
if(arr[i]==x || arr[i]==y)
{
//we... | constant | linear |
// C++ program to Find the minimum
// distance between two numbers
#include <bits/stdc++.h>
using namespace std;
int minDist(int arr[], int n, int x, int y)
{
//idx1 and idx2 will store indices of
//x or y and min_dist will store the minimum difference
int idx1=-1,idx2=-1,min_dist = INT_MAX;
for(int ... | constant | linear |
// C++ Code for the Approach
#include <bits/stdc++.h>
using namespace std;
// User function Template
int getMinDiff(int arr[], int n, int k)
{
sort(arr, arr + n);
// Maximum possible height difference
int ans = arr[n - 1] - arr[0];
int tempmin, tempmax;
tempmin = arr[0];
tempmax = ar... | constant | nlogn |
// C++ program to find Minimum
// number of jumps to reach end
#include <bits/stdc++.h>
using namespace std;
// Function to return the minimum number
// of jumps to reach arr[h] from arr[l]
int minJumps(int arr[], int n)
{
// Base case: when source and
// destination are same
if (n == 1)
return ... | linear | np |
// C++ program for Minimum number
// of jumps to reach end
#include <bits/stdc++.h>
using namespace std;
int min(int x, int y) { return (x < y) ? x : y; }
// Returns minimum number of jumps
// to reach arr[n-1] from arr[0]
int minJumps(int arr[], int n)
{
// jumps[n-1] will hold the result
int* jumps = new ... | linear | np |
// C++ program to find Minimum number of jumps to reach end
#include <bits/stdc++.h>
using namespace std;
// Returns Minimum number of jumps to reach end
int minJumps(int arr[], int n)
{
// jumps[0] will hold the result
int* jumps = new int[n];
int min;
// Minimum number of jumps needed to reach las... | linear | quadratic |
/* Dynamic Programming implementation
of Maximum Sum Increasing Subsequence
(MSIS) problem */
#include <bits/stdc++.h>
using namespace std;
/* maxSumIS() returns the maximum
sum of increasing subsequence
in arr[] of size n */
int maxSumIS(int arr[], int n)
{
int i, j, max = 0;
int msis[n];
/* Initialize... | linear | quadratic |
# include <iostream>
using namespace std;
// Returns length of smallest subarray with sum greater than x.
// If there is no subarray with given sum, then returns n+1
int smallestSubWithSum(int arr[], int n, int x)
{
// Initialize length of smallest subarray as n+1
int min_len = n + 1;
// Pick every e... | constant | quadratic |
// O(n) solution for finding smallest subarray with sum
// greater than x
#include <iostream>
using namespace std;
// Returns length of smallest subarray with sum greater than
// x. If there is no subarray with given sum, then returns
// n+1
int smallestSubWithSum(int arr[], int n, int x)
{
// Initialize current ... | constant | linear |
// C++ program to find maximum average subarray
// of given length.
#include<bits/stdc++.h>
using namespace std;
// Returns beginning index of maximum average
// subarray of length 'k'
int findMaxAverage(int arr[], int n, int k)
{
// Check if 'k' is valid
if (k > n)
return -1;
// Create and fill... | linear | linear |
// C++ program to find maximum average subarray
// of given length.
#include<bits/stdc++.h>
using namespace std;
// Returns beginning index of maximum average
// subarray of length 'k'
int findMaxAverage(int arr[], int n, int k)
{
// Check if 'k' is valid
if (k > n)
return -1;
// Compute sum of ... | constant | linear |
/* C++ program to count minimum number of operations
to get the given target array */
#include <bits/stdc++.h>
using namespace std;
// Returns count of minimum operations to convert a
// zero array to target array with increment and
// doubling operations.
// This function computes count by doing reverse
// steps,... | constant | linear |
// C++ program to find number of operations
// to make an array palindrome
#include <bits/stdc++.h>
using namespace std;
// Returns minimum number of count operations
// required to make arr[] palindrome
int findMinOps(int arr[], int n)
{
int ans = 0; // Initialize result
// Start from two corners
for (... | constant | linear |
// C++ program to find the smallest positive value that cannot be
// represented as sum of subsets of a given sorted array
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
// Returns the smallest number that cannot be represented as sum
// of subset of elements from set represented by sort... | constant | nlogn |
// C++ program to print length of the largest
// contiguous array sum
#include<bits/stdc++.h>
using namespace std;
int maxSubArraySum(int a[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0,
start =0, end = 0, s=0;
for (int i=0; i< size; i++ )
{
max_ending_here += a[i];
... | constant | linear |
// C++ implementation of simple method to find
// minimum difference between any pair
#include <bits/stdc++.h>
using namespace std;
// Returns minimum difference between any pair
int findMinDiff(int arr[], int n)
{
// Initialize difference as infinite
int diff = INT_MAX;
// Find the min diff by comparin... | constant | quadratic |
// C++ program to find minimum difference between
// any pair in an unsorted array
#include <bits/stdc++.h>
using namespace std;
// Returns minimum difference between any pair
int findMinDiff(int arr[], int n)
{
// Sort array in non-decreasing order
sort(arr, arr + n);
// Initialize difference as infini... | constant | nlogn |
// A Simple C++ program to find longest common
// subarray of two binary arrays with same sum
#include<bits/stdc++.h>
using namespace std;
// Returns length of the longest common subarray
// with same sum
int longestCommonSum(bool arr1[], bool arr2[], int n)
{
// Initialize result
int maxLen = 0;
// One... | constant | quadratic |
// A O(n) and O(n) extra space C++ program to find
// longest common subarray of two binary arrays with
// same sum
#include<bits/stdc++.h>
using namespace std;
// Returns length of the longest common sum in arr1[]
// and arr2[]. Both are of same size n.
int longestCommonSum(bool arr1[], bool arr2[], int n)
{
// ... | linear | linear |
// C++ program to find largest subarray
// with equal number of 0's and 1's.
#include <bits/stdc++.h>
using namespace std;
// Returns largest common subarray with equal
// number of 0s and 1s in both of t
int longestCommonSum(bool arr1[], bool arr2[], int n)
{
// Find difference between the two
int arr[n];
... | linear | linear |
// C++ program to print an array in alternate
// sorted manner.
#include <bits/stdc++.h>
using namespace std;
// Function to print alternate sorted values
void alternateSort(int arr[], int n)
{
// Sorting the array
sort(arr, arr+n);
// Printing the last element of array
// first and then first eleme... | constant | nlogn |
// A STL based C++ program to sort a nearly sorted array.
#include <bits/stdc++.h>
using namespace std;
// Given an array of size n, where every element
// is k away from its target position, sorts the
// array in O(n logk) time.
void sortK(int arr[], int n, int k)
{
// Insert first k+1 items in a priority ... | constant | constant |
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int sort(vector<int>& array, int l, int h, int k)
{
int mid = l + (h - l) / 2; //Choose middle element as pivot
int i = max(l, mid - k), j = i, end = min(mid + k, h); // Set appropriate range
swap(array[mid], array[end]); //Swap middle and ... | logn | nlogn |
// 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 |
// A C++ program to sort an array in wave form using
// a sorting function
#include<iostream>
#include<algorithm>
using namespace std;
// A utility method to swap two numbers.
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
// This function sorts arr[0..n-1] in wave form, i.e.,
// arr[0... | constant | nlogn |
// A O(n) program to sort an input array in wave form
#include<iostream>
using namespace std;
// A utility method to swap two numbers.
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
// This function sorts arr[0..n-1] in wave form, i.e., arr[0] >=
// arr[1] <= arr[2] >= arr[3] <= arr[4]... | constant | linear |
// C++ program to Merge an array of
// size n into another array of size m + n
#include <bits/stdc++.h>
using namespace std;
/* Assuming -1 is filled for the places
where element is not available */
#define NA -1
/* Function to move m elements at
the end of array mPlusN[] */
void moveToEnd(int mPlusN[], int s... | constant | linear |
// CPP program to test whether array
// can be sorted by swapping adjacent
// elements using boolean array
#include <bits/stdc++.h>
using namespace std;
// Return true if array can be
// sorted otherwise false
bool sortedAfterSwap(int A[], bool B[], int n)
{
int i, j;
// Check bool array B and sorts
// ... | constant | quadratic |
// CPP program to test whether array
// can be sorted by swapping adjacent
// elements using boolean array
#include <bits/stdc++.h>
using namespace std;
// Return true if array can be
// sorted otherwise false
bool sortedAfterSwap(int A[], bool B[], int n)
{
for (int i = 0; i < n - 1; i++) {
if (B[i]) {
... | constant | linear |
// CPP program to sort an array with two types
// of values in one traversal.
#include <bits/stdc++.h>
using namespace std;
/* Method for segregation 0 and 1 given
input array */
void segregate0and1(int arr[], int n)
{
int type0 = 0;
int type1 = n - 1;
while (type0 < type1) {
if (arr[type0] =... | constant | linear |
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Used for sorting
struct ele {
int count, index, val;
};
// Used for sorting by value
bool mycomp(struct ele a, struct ele b)
{
return (a.val < b.val);
}
// Used for sorting by frequency. And if frequency is same,
// the... | linear | nlogn |
// CPP program for above approach
#include <bits/stdc++.h>
using namespace std;
// Compare function
bool fcompare(pair<int, pair<int, int> > p,
pair<int, pair<int, int> > p1)
{
if (p.second.second != p1.second.second)
return (p.second.second > p1.second.second);
else
return (p.se... | linear | nlogn |
// CPP program to find shortest subarray which is
// unsorted.
#include <bits/stdc++.h>
using namespace std;
// bool function for checking an array elements
// are in increasing.
bool increasing(int a[], int n)
{
for (int i = 0; i < n - 1; i++)
if (a[i] >= a[i + 1])
return false;
return... | constant | linear |
// C++ program to find
// minimum number of swaps
// required to sort an array
#include<bits/stdc++.h>
using namespace std;
// Function returns the
// minimum number of swaps
// required to sort the array
int minSwaps(int arr[], int n)
{
// Create an array of
// pairs where first
// element is array el... | linear | nlogn |
#include <bits/stdc++.h>
using namespace std;
// Function returns the
// minimum number of swaps
// required to sort the array
int minSwaps(int nums[], int n)
{
int len = n;
map<int, int> map;
for (int i = 0; i < len; i++)
map[nums[i]] = i;
sort(nums, nums + n);
// To keep track of visit... | linear | nlogn |
// C++ program to find minimum number
// of swaps required to sort an array
#include <bits/stdc++.h>
using namespace std;
void swap(vector<int> &arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int indexOf(vector<int> &arr, int ele)
{
for(int i = 0; i < arr.size(); i++)
... | linear | quadratic |
// C++ program to find
// minimum number of swaps
// required to sort an array
#include<bits/stdc++.h>
using namespace std;
void swap(vector<int> &arr,
int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Return the minimum number
// of swaps required to sort
// the array
int minSwa... | linear | nlogn |
// C++ program to sort an array
// with 0, 1 and 2 in a single pass
#include <bits/stdc++.h>
using namespace std;
// Function to sort the input array,
// the array is assumed
// to have values in {0, 1, 2}
void sort012(int a[], int arr_size)
{
int lo = 0;
int hi = arr_size - 1;
int mid = 0;
// Itera... | constant | linear |
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Utility function to print the contents of an array
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// Function to sort the array of 0s, 1s and 2s
void sortArr(int arr[], int n)
{... | constant | linear |
// This code is contributed by Anjali Saxena
#include <bits/stdc++.h>
using namespace std;
// Function to find position to insert current element of
// stream using binary search
int binarySearch(int arr[], int item, int low, int high)
{
if (low >= high) {
return (item > arr[low]) ? (low + 1) : low;
... | constant | quadratic |
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the median of stream of data
void streamMed(int A[], int n)
{
// Declared two max heap
priority_queue<int> g, s;
for (int i = 0; i < n; i++) {
s.push(A[i]);
int temp = s.top();
... | linear | nlogn |
// C++ code to count the number of possible triangles using
// brute force approach
#include <bits/stdc++.h>
using namespace std;
// Function to count all possible triangles with arr[]
// elements
int findNumberOfTriangles(int arr[], int n)
{
// Count of triangles
int count = 0;
// The three loops selec... | constant | cubic |
// C++ program to count number of triangles that can be
// formed from given array
#include <bits/stdc++.h>
using namespace std;
// Function to count all possible triangles with arr[]
// elements
int findNumberOfTriangles(int arr[], int n)
{
// Sort the array elements in non-decreasing order
sort(arr, arr + n... | constant | quadratic |
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
void CountTriangles(vector<int> A)
{
int n = A.size();
sort(A.begin(), A.end());
int count = 0;
for (int i = n - 1; i >= 1; i--) {
int l = 0, r = i - 1;
while (l < r) {
if (A[l... | constant | quadratic |
// C++ program to finds the number of pairs (x, y)
// in an array such that x^y > y^x
#include <bits/stdc++.h>
using namespace std;
// Function to return count of pairs with x as one element
// of the pair. It mainly looks for all values in Y[] where
// x ^ Y[i] > Y[i] ^ x
int count(int x, int Y[], int n, int NoOfY... | constant | nlogn |
/* A simple program to count pairs with difference k*/
#include <iostream>
using namespace std;
int countPairsWithDiffK(int arr[], int n, int k)
{
int count = 0;
// Pick all elements one by one
for (int i = 0; i < n; i++) {
// See if there is a pair of this picked element
for (int j = i ... | constant | quadratic |
/* A sorting based program to count pairs with difference k*/
#include <iostream>
#include <algorithm>
using namespace std;
/* Returns count of pairs with difference k in arr[] of size n. */
int countPairsWithDiffK(int arr[], int n, int k)
{
int count = 0;
sort(arr, arr+n); // Sort array elements
int ... | constant | nlogn |
#include <bits/stdc++.h>
using namespace std;
int BS(int arr[], int X, int low, int N)
{
int high = N - 1;
int ans = N;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] >= X) {
ans = mid;
high = mid - 1;
}
else
low = m... | constant | nlogn |
// C++ program to print all distinct elements in a given array
#include <bits/stdc++.h>
using namespace std;
void printDistinct(int arr[], int n)
{
// Pick all elements one by one
for (int i=0; i<n; i++)
{
// Check if the picked element is already printed
int j;
for (j=0; j<i; j++)... | constant | quadratic |
// C++ program to print all distinct elements in a given array
#include <bits/stdc++.h>
using namespace std;
void printDistinct(int arr[], int n)
{
// First sort the array so that all occurrences become consecutive
sort(arr, arr + n);
// Traverse the sorted array
for (int i=0; i<n; i++)
{
... | constant | nlogn |
/* CPP program to print all distinct elements
of a given array */
#include<bits/stdc++.h>
using namespace std;
// This function prints all distinct elements
void printDistinct(int arr[],int n)
{
// Creates an empty hashset
unordered_set<int> s;
// Traverse the input array
for (int i=0; i<n; i++)
... | linear | linear |
// C++ approach
#include <bits/stdc++.h>
using namespace std;
int main() {
int ar[] = { 10, 5, 3, 4, 3, 5, 6 };
map<int ,int> hm;
for (int i = 0; i < sizeof(ar)/sizeof(ar[0]); i++) { // total = O(n*logn)
hm.insert({ar[i], i}); // time complexity for insert() in map O(logn)
}
cout <<"[";
for (auto c... | linear | nlogn |
// C++ program to merge two sorted arrays with O(1) extra
// space.
#include <bits/stdc++.h>
using namespace std;
// Merge ar1[] and ar2[] with O(1) extra space
void merge(int ar1[], int ar2[], int m, int n)
{
// Iterate through all elements
// of ar2[] starting from the last element
for (int i = n - 1; i... | constant | quadratic |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.