code stringlengths 195 7.9k | space_complexity stringclasses 6
values | time_complexity stringclasses 7
values |
|---|---|---|
// CPP program to make a permutation of numbers
// from 1 to n using minimum changes.
#include <bits/stdc++.h>
using namespace std;
void makePermutation(int a[], int n)
{
// Store counts of all elements.
unordered_map<int, int> count;
for (int i = 0; i < n; i++)
count[a[i]]++;
int next_missi... | linear | nlogn |
// C++ implementation of simple method to find count of
// pairs with given sum.
#include <bits/stdc++.h>
using namespace std;
// Returns number of pairs in arr[0..n-1] with sum equal
// to 'sum'
int getPairsCount(int arr[], int n, int sum)
{
int count = 0; // Initialize result
// Consider all possible pair... | constant | quadratic |
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the count of pairs
int getPairsCount(int arr[], int n, int k)
{
sort(arr, arr + n);
int x = 0, c = 0, y, z;
for (int i = 0; i < n - 1; i++) {
x = k - arr[i];
// Lower bound from i+1
... | constant | nlogn |
// C++ implementation of simple method to find count of
// pairs with given sum.
#include <bits/stdc++.h>
using namespace std;
// Returns number of pairs in arr[0..n-1] with sum equal
// to 'sum'
int getPairsCount(int arr[], int n, int sum)
{
unordered_map<int, int> m;
// Store counts of all elements in map... | linear | linear |
// C++ implementation of simple method to
// find count of pairs with given sum.
#include <bits/stdc++.h>
using namespace std;
// Returns number of pairs in arr[0..n-1] with sum equal
// to 'sum'
int getPairsCount(int arr[], int n, int k)
{
unordered_map<int, int> m;
int count = 0;
for (int i = 0; i < n;... | linear | linear |
// C++ implementation to count pairs from both linked
// lists whose sum is equal to a given value
#include <bits/stdc++.h>
using namespace std;
/* A Linked list node */
struct Node
{
int data;
struct Node* next;
};
// function to insert a node at the
// beginning of the linked list
void push(struct Node** ... | constant | quadratic |
// C++ implementation to count pairs from both linked
// lists whose sum is equal to a given value
#include <bits/stdc++.h>
using namespace std;
/* A Linked list node */
struct Node
{
int data;
struct Node* next;
};
// function to insert a node at the
// beginning of the linked list
void push(struct Node** ... | constant | nlogn |
// C++ implementation to count pairs from both linked
// lists whose sum is equal to a given value
#include <bits/stdc++.h>
using namespace std;
/* A Linked list node */
struct Node
{
int data;
struct Node* next;
};
// function to insert a node at the
// beginning of the linked list
void push(struct Node** ... | linear | linear |
// C++ implementation to count quadruples from four sorted arrays
// whose sum is equal to a given value x
#include <bits/stdc++.h>
using namespace std;
// function to count all quadruples from
// four sorted arrays whose sum is equal
// to a given value x
int countQuadruples(int arr1[], int arr2[],
... | constant | np |
// C++ implementation to count quadruples from
// four sorted arrays whose sum is equal to a
// given value x
#include <bits/stdc++.h>
using namespace std;
// find the 'value' in the given array 'arr[]'
// binary search technique is applied
bool isPresent(int arr[], int low, int high, int value)
{
while (low <=... | constant | cubic |
// C++ implementation to count quadruples from
// four sorted arrays whose sum is equal to a
// given value x
#include <bits/stdc++.h>
using namespace std;
// count pairs from the two sorted array whose sum
// is equal to the given 'value'
int countPairs(int arr1[], int arr2[], int n, int value)
{
int count = 0... | constant | cubic |
// C++ implementation to count quadruples from
// four sorted arrays whose sum is equal to a
// given value x
#include <bits/stdc++.h>
using namespace std;
// function to count all quadruples from four sorted
// arrays whose sum is equal to a given value x
int countQuadruples(int arr1[], int arr2[], int arr3[],
... | quadratic | quadratic |
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {10, 2, -2, -20, 10};
int k = -10;
int n = sizeof(arr) / sizeof(arr[0]);
int res = 0;
// Calculate all subarrays
for (int i = 0; i < n; i++)
{
int sum = 0;
for (int j = i; j < n; j++)
... | constant | quadratic |
// C++ program to find number of subarrays with sum exactly
// equal to k.
#include <bits/stdc++.h>
using namespace std;
// Function to find number of subarrays with sum exactly
// equal to k.
int findSubarraySum(int arr[], int n, int sum)
{
// STL map to store number of subarrays starting from
// index zero ... | linear | linear |
// C++ program to find all pairs in both arrays
// whose sum is equal to given value x
#include <bits/stdc++.h>
using namespace std;
// Function to print all pairs in both arrays
// whose sum is equal to given value x
void findPairs(int arr1[], int arr2[], int n, int m, int x)
{
for (int i = 0; i < n; i++)
... | constant | quadratic |
// C++ program to find all pair in both arrays
// whose sum is equal to given value x
#include <bits/stdc++.h>
using namespace std;
// Function to find all pairs in both arrays
// whose sum is equal to given value x
void findPairs(int arr1[], int arr2[], int n,
int m, int x)
{
// Insert all element... | linear | linear |
#include <bits/stdc++.h>
using namespace std;
// Function to print the cumulative frequency according to
// the order given
void countFreq(int a[], int n)
{
// Declaring a map so values get inserted in a sorted
// manner
map<int, int> m;
// Inserting values into the map
for (int i = 0; i <... | linear | nlogn |
// C++ program to print the cumulative frequency
// according to the order given
#include <bits/stdc++.h>
using namespace std;
// Function to print the cumulative frequency
// according to the order given
void countFreq(int a[], int n)
{
// Insert elements and their
// frequencies in hash map.
unordered_m... | linear | linear |
// CPP program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Used for sorting by frequency. And if frequency is same,
// then by appearance
bool sortByVal(const pair<int, int>& a,
const pair<int, int>& b)
{
// If frequency is same then sort by index
if (a.se... | linear | linear |
// A simple C++ program to find pair whose sum
// already exists in array
#include <bits/stdc++.h>
using namespace std;
// Function to find pair whose sum exists in arr[]
void findPair(int arr[], int n)
{
bool found = false;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
f... | constant | cubic |
// C++ program to find pair whose sum already
// exists in array
#include <bits/stdc++.h>
using namespace std;
// Function to find pair whose sum exists in arr[]
void findPair(int arr[], int n)
{
// Hash to store all element of array
unordered_set<int> s;
for (int i = 0; i < n; i++)
s.insert(arr[i... | quadratic | quadratic |
// C++ implementation to find such pairs
#include <bits/stdc++.h>
using namespace std;
// Function to find pair such that (a % b = k)
bool printPairs(int arr[], int n, int k)
{
bool isPairFound = true;
// Consider each and every pair
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
... | constant | quadratic |
// C++ program to find all pairs such that
// a % b = k.
#include <bits/stdc++.h>
using namespace std;
// Utility function to find the divisors of
// n and store in vector v[]
vector<int> findDivisors(int n)
{
vector<int> v;
// Vector is used to store the divisors
for (int i = 1; i <= sqrt(n); i++) {
... | linear | linear |
// C++ program to convert an array in reduced
// form
#include <bits/stdc++.h>
using namespace std;
void convert(int arr[], int n)
{
// Create a temp array and copy contents
// of arr[] to temp
int temp[n];
memcpy(temp, arr, n*sizeof(int));
// Sort temp array
sort(temp, temp + n);
// C... | linear | nlogn |
// C++ program to output the maximum occurring character
// in a string
#include <bits/stdc++.h>
#define ASCII_SIZE 256
using namespace std;
char getMaxOccurringChar(char* str)
{
// Create array to keep the count of individual
// characters and initialize the array as 0
int count[ASCII_SIZE] = { 0 };
... | constant | linear |
// C++ program to print all words that have
// the same unique character set
#include<bits/stdc++.h>
using namespace std;
#define MAX_CHAR 26
// Generates a key from given string. The key
// contains all unique characters of given string in
// sorted order consisting of only distinct elements.
string getKey(string &s... | linear | linear |
// C++ program to find out the second
// most repeated word
#include <bits/stdc++.h>
using namespace std;
// Function to find the word
string secMostRepeated(vector<string> seq)
{
// Store all the words with its occurrence
unordered_map<string, int> occ;
for (int i = 0; i < seq.size(); i++)
occ[... | linear | linear |
// C++ program to find the smallest element
// with frequency exactly k.
#include <bits/stdc++.h>
using namespace std;
int smallestKFreq(int a[], int n, int k)
{
unordered_map<int, int> m;
// Map is used to store the count of
// elements present in the array
for (int i = 0; i < n; i++)
m[a[i... | linear | linear |
// C++ code to find number
// occurring prime number
// of times with frequency >= k
#include <bits/stdc++.h>
using namespace std;
// Check if the number of
// occurrences are primes
// or not
bool isPrime(int n)
{
// Corner case
if (n <= 1) return false;
// Check from 2 to n-1
for (int i = 2; i < n... | linear | 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... | logn | 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++ program to find first repeating element in arr[] */
#include <bits/stdc++.h>
using namespace std;
// This function prints the first repeating element in arr[]
void printFirstRepeating(int arr[], int n)
{
// Initialize index of first repeating element
int min = -1;
// Creates an empty hashset
... | linear | linear |
/* C++ program to find first
repeating element in arr[] */
#include <bits/stdc++.h>
using namespace std;
// This function prints the
// first repeating element in arr[]
void printFirstRepeating(int arr[], int n)
{
// This will set k=1, if any
// repeating element found
int k = 0;
// max = maximum ... | linear | linear |
// Simple CPP program to find first non-
// repeating element.
#include <bits/stdc++.h>
using namespace std;
int firstNonRepeating(int arr[], int n)
{
// Loop for checking each element
for (int i = 0; i < n; i++) {
int j;
// Checking if ith element is present in array
for (j = 0; j < n... | constant | quadratic |
// Efficient CPP program to find first non-
// repeating element.
#include <bits/stdc++.h>
using namespace std;
int firstNonRepeating(int arr[], int n)
{
// Insert all array elements in hash
// table
unordered_map<int, int> mp;
for (int i = 0; i < n; i++)
mp[arr[i]]++;
// Traverse array ... | linear | linear |
// C++ program to print k-th distinct
// element in a given array
#include <bits/stdc++.h>
using namespace std;
// Returns k-th distinct
// element in arr.
int printKDistinct(int arr[], int n,
int k)
{
int dist_count = 0;
for (int i = 0; i < n; i++)
{
// Check if curr... | constant | quadratic |
// C++ program to print k-th
// distinct element in a
// given array
#include <bits/stdc++.h>
using namespace std;
// Returns k-th distinct
// element in arr
int printKDistinct(int arr[],
int n, int k)
{
// Traverse input array and
// store counts if individual
// elements.
unordere... | linear | linear |
// 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 |
// Simple CPP program to find pairs of positive
// and negative values present in an array.
#include <bits/stdc++.h>
using namespace std;
// Print pair with negative and positive value
void printPairs(int arr[], int n)
{
vector<int> v;
// For each element of array.
for (int i = 0; i < n; i++)
... | linear | quadratic |
// CPP program to find pairs of
// positive and negative values present in
// an array.
#include <bits/stdc++.h>
using namespace std;
// Print pair with negative and positive value
void printPairs(int arr[], int n)
{
vector<int> v;
unordered_map<int, bool> cnt;
// For each element of array.
for (int... | linear | linear |
// CPP program to find pairs of
// positive and negative values present in
// an array.
#include <bits/stdc++.h>
using namespace std;
// Print pair with negative and positive value
void printPairs(int arr[], int n)
{
unordered_set<int> hs;
vector<int> ans;
for (int i = 0; i < n; i++) {
if (hs.find... | linear | linear |
// CPP program to count divisible pairs.
#include <bits/stdc++.h>
using namespace std;
int countDivisibles(int arr[], int n)
{
int res = 0;
// Iterate through all pairs
for (int i=0; i<n; i++)
for (int j=i+1; j<n; j++)
// Increment count if one divides
// other
... | constant | quadratic |
// C++ code to implement above approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the total
// count of pairs such that
// arr[i]%arr[j]==0
int total_count(int arr[], int n)
{
int count = 0;
// Storing the occurrence of
// every element in array in
// unordered_map
... | constant | quadratic |
// A C++ program to check if arr[0..n-1]
// can be divided in pairs such that
// every pair is divisible by k
#include <bits/stdc++.h>
using namespace std;
bool canPairs(int nums[], int n, int k)
{
// Array with odd length
// cannot be divided
if (n % 2 == 1)
return false;
// Initialize co... | linear | quadratic |
// A C++ program to check if arr[0..n-1] can be divided
// in pairs such that every pair is divisible by k.
#include <bits/stdc++.h>
using namespace std;
// Returns true if arr[0..n-1] can be divided into pairs
// with sum divisible by k.
bool canPairs(int arr[], int n, int k)
{
// An odd length array cannot be d... | linear | linear |
// C++ implementation to find the longest subarray
// with sum divisible by k
#include <bits/stdc++.h>
using namespace std;
// function to find the longest subarray
// with sum divisible by k
int longestSubarrWthSumDivByK(int arr[], int n, int k)
{
// unordered map 'um' implemented as
// hash table
un... | linear | linear |
#include <bits/stdc++.h>
using namespace std;
// function to find the longest subarray
// with sum divisible by k
int longestSubarrWthSumDivByK(int arr[], int n, int k)
{
// unordered map 'um' implemented as
// hash table
unordered_map<int, int> um;
int max_len = 0;
int curr_sum = 0;
for... | linear | linear |
// CPP code to find the subarray with
// no pair sum divisible by K
#include<bits/stdc++.h>
using namespace std;
// function to find the subarray with
// no pair sum divisible by k
void subarrayDivisibleByK(int arr[], int n, int k)
{
// hash table to store the remainders
// obtained on dividing by K
map<i... | linear | nlogn |
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find special numbers
void divisibilityCheck(int arr[], int n)
{
// Storing all array elements in a hash
// and finding maximum element in the array
unordered_set<int> s;
int max_ele = INT_MIN;
for ... | linear | quadratic |
// C++ program to find three element
// from different three arrays such
// that a + b + c is equal to
// given sum
#include<bits/stdc++.h>
using namespace std;
// Function to check if there is
// an element from each array such
// that sum of the three elements
// is equal to given sum.
bool findTriplet(int a1[], in... | constant | cubic |
// C++ program to find three element
// from different three arrays such
// that a + b + c is equal to
// given sum
#include<bits/stdc++.h>
using namespace std;
// Function to check if there is
// an element from each array such
// that sum of the three elements is
// equal to given sum.
bool findTriplet(int a1[], in... | linear | quadratic |
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Returns length of the largest
// subarray with 0 sum
int maxLen(int arr[], int N)
{
// Initialize result
int max_len = 0;
// Pick a starting point
for (int i = 0; i < N; i++) {
// Initialize currr_sum fo... | constant | quadratic |
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Returns Length of the required subarray
int maxLen(int arr[], int N)
{
// Map to store the previous sums
unordered_map<int, int> presum;
int sum = 0; // Initialize the sum of elements
int max_len = 0; // Initial... | linear | linear |
// CPP program to find length of the
// longest increasing subsequence
// whose adjacent element differ by 1
#include <bits/stdc++.h>
using namespace std;
// function that returns the length of the
// longest increasing subsequence
// whose adjacent element differ by 1
void longestSubsequence(int a[], int n)
{
//... | linear | linear |
// CPP program to find length of the
// longest increasing subsequence
// whose adjacent element differ by 1
#include <bits/stdc++.h>
using namespace std;
// function that returns the length of the
// longest increasing subsequence
// whose adjacent element differ by 1
int longestSubsequence(int a[], int n)
{
// ... | linear | linear |
// C++ implementation to find longest subsequence
// such that difference between adjacents is one
#include <bits/stdc++.h>
using namespace std;
// function to find longest subsequence such
// that difference between adjacents is one
int longLenSub(int arr[], int n)
{
// hash table to map the array element with... | linear | linear |
// C++ implementation of longest continuous increasing
// subsequence
#include <bits/stdc++.h>
using namespace std;
// Function for LIS
int findLIS(int A[], int n)
{
unordered_map<int, int> hash;
// Initialize result
int LIS_size = 1;
int LIS_index = 0;
hash[A[0]] = 1;
// iterate through... | linear | linear |
// C++ implementation to count subsets having
// even numbers only and all are distinct
#include <bits/stdc++.h>
using namespace std;
// function to count the
// required subsets
int countSubsets(int arr[], int n)
{
unordered_set<int> us;
int even_count = 0;
// inserting even numbers in the set ... | linear | linear |
// C++ program to count distinct
// elements in every window of size K
#include <bits/stdc++.h>
using namespace std;
// Counts distinct elements in window of size K
int countWindowDistinct(int win[], int K)
{
int dist_count = 0;
// Traverse the window
for (int i = 0; i < K; i++) {
// Check ... | constant | quadratic |
// C++ program for the above approach
#include <iostream>
#include <unordered_map>
using namespace std;
void countDistinct(int arr[], int K, int N)
{
// Creates an empty hashmap hm
unordered_map<int, int> hm;
// initialize distinct element count for current window
int dist_count = 0;
// ... | linear | linear |
// C++ program to find the maximum
// possible sum of a window in one
// array such that elements in same
// window of other array are unique.
#include <bits/stdc++.h>
using namespace std;
// Function to return maximum sum of window
// in B[] according to given constraints.
int returnMaxSum(int A[], int B[], int n)
{... | linear | linear |
// C++ program to find if
// there is a zero sum subarray
#include <bits/stdc++.h>
using namespace std;
bool subArrayExists(int arr[], int N)
{
unordered_set<int> sumSet;
// Traverse through array
// and store prefix sums
int sum = 0;
for (int i = 0; i < N; i++) {
sum += arr[i];
... | linear | linear |
// C++ program to print all subarrays
// in the array which has sum 0
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > findSubArrays(int arr[], int n)
{
// Array to store all the start and end
// indices of subarrays with 0 sum
vector<pair<int, int> > out;
for (int i = 0; i < n;... | constant | quadratic |
// C++ program to print all subarrays
// in the array which has sum 0
#include <bits/stdc++.h>
using namespace std;
// Function to print all subarrays in the array which
// has sum 0
vector< pair<int, int> > findSubArrays(int arr[], int n)
{
// create an empty map
unordered_map<int, vector<int> > map;
... | 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 */
int subArraySum(int arr[], int n, int sum)
{
int curr_sum, i, j;
// Pic... | constant | quadratic |
// C++ program to print subarray with sum as given sum
#include <bits/stdc++.h>
using namespace std;
// Function to print subarray with sum as given sum
void subArraySum(int arr[], int n, int sum)
{
// create an empty map
unordered_map<int, int> map;
// Maintains sum of elements so far
int curr_sum ... | linear | linear |
// CPP program to find minimum number
// of insertions to make a string
// palindrome
#include <bits/stdc++.h>
using namespace std;
// Function will return number of
// characters to be added
int minInsertion(string str)
{
// To store string length
int n = str.length();
// To store number of characters
... | constant | linear |
// C++ implementation to find maximum length
// subsequence with difference between adjacent
// elements as either 0 or 1
#include <bits/stdc++.h>
using namespace std;
// function to find maximum length subsequence
// with difference between adjacent elements as
// either 0 or 1
int maxLenSub(int arr[], int n)
{
... | linear | linear |
// C++ program to find maximum difference
// between frequency of any two element
// such that element with greater frequency
// is also greater in value.
#include<bits/stdc++.h>
using namespace std;
// Return the maximum difference between
// frequencies of any two elements such that
// element with greater frequenc... | linear | quadratic |
// Efficient C++ program to find maximum
// difference between frequency of any two
// elements such that element with greater
// frequency is also greater in value.
#include<bits/stdc++.h>
using namespace std;
// Return the maximum difference between
// frequencies of any two elements such that
// element with great... | linear | nlogn |
// CPP code to find the difference between highest
// and least frequencies
#include <bits/stdc++.h>
using namespace std;
int findDiff(int arr[], int n)
{
// sort the array
sort(arr, arr + n);
int count = 0, max_count = 0, min_count = n;
for (int i = 0; i < (n - 1); i++) {
// checking cons... | constant | nlogn |
// CPP code to find the difference between highest
// and least frequencies
#include <bits/stdc++.h>
using namespace std;
int findDiff(int arr[], int n)
{
// Put all elements in a hash map
unordered_map<int, int> hm;
for (int i = 0; i < n; i++)
hm[arr[i]]++;
// Find counts of maximum and min... | linear | linear |
// C++ program to find minimum range that
// contains exactly k distinct numbers.
#include <bits/stdc++.h>
using namespace std;
// Prints the minimum range that contains exactly
// k distinct numbers.
void minRange(int arr[], int n, int k)
{
// Starting and ending index of resultant subarray
int start = 0, e... | linear | quadratic |
// C++ program to find minimum range that
// contains exactly k distinct numbers.
#include <bits/stdc++.h>
using namespace std;
// prints the minimum range that contains exactly
// k distinct numbers.
void minRange(int arr[], int n, int k)
{
/*
start = starting index of resultant subarray
end = e... | linear | linear |
// CPP program to find longest subarray with
// k or less distinct elements.
#include <bits/stdc++.h>
using namespace std;
// function to print the longest sub-array
void longest(int a[], int n, int k)
{
unordered_map<int, int> freq;
int start = 0, end = 0, now = 0, l = 0;
for (int i = 0; i < n; i++) {
... | linear | linear |
// C++ program to find number of pairs in an array such that
// their XOR is 0
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the count
int calculate(int a[], int n)
{
// Sorting the list using built in function
sort(a, a + n);
int count = 1;
int answer = 0;
// Traversing through t... | constant | nlogn |
// C++ program to find number of pairs
// in an array such that their XOR is 0
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the answer
int calculate(int a[], int n){
// Finding the maximum of the array
int *maximum = max_element(a, a + n);
// Creating frequency array
... | linear | linear |
// C++ implementation to count subarrays with
// equal number of 1's and 0's
#include <bits/stdc++.h>
using namespace std;
// function to count subarrays with
// equal number of 1's and 0's
int countSubarrWithEqualZeroAndOne(int arr[], int n)
{
// 'um' implemented as hash table to store
// frequency of valu... | linear | linear |
#include <bits/stdc++.h>
using namespace std;
int countSubarrWithEqualZeroAndOne(int arr[], int n)
{
unordered_map<int, int> mp;
int sum = 0;
int count = 0;
for (int i = 0; i < n; i++) {
// Replacing 0's in array with -1
if (arr[i] == 0)
arr[i] = -1;
sum += arr[... | linear | linear |
// C++ implementation to find the length of
// longest subarray having count of 1's one
// more than count of 0's
#include <bits/stdc++.h>
using namespace std;
// function to find the length of longest
// subarray having count of 1's one more
// than count of 0's
int lenOfLongSubarr(int arr[], int n)
{
// unorder... | linear | linear |
// C++ program to print all triplets in given
// array that form Arithmetic Progression
// C++ program to print all triplets in given
// array that form Arithmetic Progression
#include <bits/stdc++.h>
using namespace std;
// Function to print all triplets in
// given sorted array that forms AP
void printAllAPTriplet... | linear | quadratic |
// C++ program to print all triplets in given
// array that form Arithmetic Progression
#include <bits/stdc++.h>
using namespace std;
// Function to print all triplets in
// given sorted array that forms AP
void printAllAPTriplets(int arr[], int n)
{
for (int i = 1; i < n - 1; i++)
{
// Search oth... | constant | quadratic |
// C++ program to find unique triplets
// that sum up to a given value.
#include <bits/stdc++.h>
using namespace std;
// Structure to define a triplet.
struct triplet
{
int first, second, third;
};
// Function to find unique triplets that
// sum up to a given value.
int findTriplets(int nums[], int n, int sum)
... | linear | quadratic |
// 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 count triplets with given
// product m
#include <iostream>
using namespace std;
// Function to count such triplets
int countTriplets(int arr[], int n, int m)
{
int count = 0;
// Consider all triplets and count if
// their product is equal to m
for (int i = 0; i < n - 2; i++)
... | constant | cubic |
// C++ program to count triplets with given
// product m
#include <bits/stdc++.h>
using namespace std;
// Function to count such triplets
int countTriplets(int arr[], int n, int m)
{
// Store all the elements in a set
unordered_map<int, int> occ;
for (int i = 0; i < n; i++)
occ[arr[i]] = i;
... | linear | quadratic |
// C++ program to count of pairs with equal
// elements in an array.
#include<bits/stdc++.h>
using namespace std;
// Return the number of pairs with equal
// values.
int countPairs(int arr[], int n)
{
int ans = 0;
// for each index i and j
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++... | constant | quadratic |
// C++ program to count of index pairs with
// equal elements in an array.
#include<bits/stdc++.h>
using namespace std;
// Return the number of pairs with equal
// values.
int countPairs(int arr[], int n)
{
unordered_map<int, int> mp;
// Finding frequency of each number.
for (int i = 0; i < n; i++)
... | linear | linear |
/* A C++ program to answer queries to check whether
the substrings are palindrome or not efficiently */
#include <bits/stdc++.h>
using namespace std;
#define p 101
#define MOD 1000000007
// Structure to represent a query. A query consists
// of (L, R) and we have to answer whether the substring
// from index-L to R... | linear | quadratic |
// C++ program to finds out smallest range that includes
// elements from each of the given sorted lists.
#include <bits/stdc++.h>
using namespace std;
// array for storing the current index of list i
int ptr[501];
// This function takes an k sorted lists in the form of
// 2D array as an argument. It finds out sm... | linear | linear |
// C++ program to finds out smallest range that includes
// elements from each of the given sorted lists.
#include <bits/stdc++.h>
using namespace std;
#define N 5
// A min heap node
struct MinHeapNode {
// The element to be stored
int element;
// index of the list from which the element is taken
... | linear | linear |
// 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 for above implementation
#include <bits/stdc++.h>
using namespace std;
// Function to count numbers to be added
int countNum(int arr[], int n)
{
int count = 0;
// Sort the array
sort(arr, arr + n);
// Check if elements are consecutive
// or not. If not, update count
for (in... | constant | nlogn |
// C++ program for above implementation
#include <bits/stdc++.h>
using namespace std;
// Function to count numbers to be added
int countNum(int arr[], int n)
{
unordered_set<int> s;
int count = 0, maxm = INT_MIN, minm = INT_MAX;
// Make a hash of elements
// and store minimum and maximum element
... | constant | linear |
// C++ program to calculate sum of lengths of subarrays
// of distinct elements.
#include<bits/stdc++.h>
using namespace std;
// Returns sum of lengths of all subarrays with distinct
// elements.
int sumoflength(int arr[], int n)
{
// For maintaining distinct elements.
unordered_set<int> s;
// Initializ... | linear | linear |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.