Reverse an array

Read more →

Find the maximum and minimum element in an array

https://www.geeksforgeeks.org/maximum-and-minimum-in-an-array/ Divide and conquer struct Pair { int min; int max; }; struct Pair getMinMax(int arr[], int low, int high) { struct Pair minmax, mml, mmr; int mid; if (low == high) { minmax.max = arr[low]; minmax.min = arr[low]; return minmax; } if (high - low == 1) { if (arr[low] > arr[high]) { minmax.max = arr[low]; minmax.min = arr[high]; } else { minmax.max = arr[high]; minmax.min = arr[low]; } return minmax; } mid = low + (high - low) / 2; mml = getMinMax(arr, low, mid); mmr = getMinMax(arr, mid + 1, high); minmax.
Read more →

Find the Kth max and min element in an array

https://practice.geeksforgeeks.org/problems/kth-smallest-element/0 Make priority queue of size k, insert first k elements from the array For the remaining elements, pop and insert into pq if element is smaller than top class Solution { public: int kthSmallest(int arr[], int l, int r, int k) { priority_queue<int, vector<int>> pq(arr, arr + k); int n = r - l + 1; for (int i = k; i < n; i++) { if (arr[i] < pq.
Read more →

Sort an array of 0s, 1s and 2s

Read more →

Move all the negative elements to one side of the array

Read more →

Find the union and intersection of the two sorted arrays

https://practice.geeksforgeeks.org/problems/union-of-two-arrays/0 Union Does not handle duplicates class Solution { public: int doUnion(int a[], int n, int b[], int m) { int i = 0, j = 0; sort(a, a + n); sort(b, b + m); vector<int> v; while (i < n && j < m) { if (a[i] < b[j]) { v.push_back(a[i]); i++; } else if (a[i] > b[j]) { v.push_back(b[j]); j++; } else { v.push_back(a[i]); // or // v.
Read more →

Cyclically rotate an array by one

Read more →

Minimise the maximum difference between heights

Read more →

Minimum number of jumps to reach end of an array

Read more →

Find the duplicate in an array of N+1 integers

Read more →

Kadane’s algorithm

Read more →

Merge intervals

Read more →

Next permutation

https://leetcode.com/problems/next-permutation/ Find non-increasing sequence from right Find just greater number from right Swap them Reverse from i to end class Solution { public: void nextPermutation(vector<int> &nums) { int i = nums.size() - 2; while (i >= 0 && nums[i + 1] <= nums[i]) { i--; } if (i >= 0) { int j = nums.size() - 1; while (j >= 0 && nums[j] <= nums[i]) { j--; } swap(nums[i], nums[j]); } reverse(nums.
Read more →

Count inversion

Read more →

Best time to buy and sell stock

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ Only 1 transaction is allowed One pass, just need to check the lowest valley/highest peak (and their difference) This problem reduces to maximum difference between two elements when larger element must come after smaller element class Solution { public: int maxProfit(vector<int> &prices) { int n = prices.size(); int minPrice = INT_MAX, maxProfit = 0; for (int i = 0; i < n; i++) { minPrice = min(minPrice, prices[i]); maxProfit = max(maxProfit, prices[i] - minPrice); } return maxProfit; } }; Maximum profit by buying and selling a share at most twice https://leetcode.
Read more →

Find all pairs on integer array whose sum is equal to K

Read more →

Find common elements in 3 sorted arrays

Read more →

Rearrange the array in alternating positive and negative items with O(1) extra space

Read more →

Find if there is any subarray with sum equal to 0

Read more →

Find factorial of a large number

Read more →

Find maximum product subarray

Read more →

Find longest consecutive subsequence

Read more →

Given an array of size N and a number K, find all elements that appear more than N/K times

Read more →

Find whether an array is a subset of another array

https://practice.geeksforgeeks.org/problems/array-subset-of-another-array/0 Can also insert into set twice, and check if size is the same. string isSubset(int arr1[], int arr2[], int m, int n) { int i = 0, j = 0; if (m < n) return "No"; sort(arr1, arr1 + m); sort(arr2, arr2 + n); while (i < n && j < m) { if (arr1[j] < arr2[i]) j++; else if (arr1[j] == arr2[i]) { j++; i++; } else if (arr1[j] > arr2[i]) return "No"; } return (i < n) ?
Read more →

Find the triplet that sum to a given value

https://practice.geeksforgeeks.org/problems/triplet-sum-in-array/0 class Solution { public: bool find3Numbers(int A[], int n, int X) { sort(A, A + n); for (int i = 0; i < n - 2; i++) { int l = i + 1, r = n - 1; while (l < r) { int sum = A[i] + A[l] + A[r]; if (sum == X) return true; else if (sum < X) { l++; } else { r--; } } } return false; } }; When X = 0 https://leetcode.
Read more →

Trapping rain water

https://practice.geeksforgeeks.org/problems/trapping-rain-water/0 Find lMax, rMax ans += min(lMax, rMax) - currHeight class Solution { public: int trap(vector<int> &height) { if (height.empty()) return 0; int n = height.size(); vector<pair<int, int>> maxHeights(n); maxHeights[0].first = height[0]; maxHeights[n - 1].second = height[n - 1]; for (int i = 1; i < n; i++) { maxHeights[i].first = max(height[i], maxHeights[i - 1].first); } for (int i = n - 2; i >= 0; i--) { maxHeights[i].second = max(height[i], maxHeights[i + 1].
Read more →

Chocolate distribution

Read more →

Smallest subarray with sum greater than a given value

Read more →

Three way partitioning of an array around a given value

Read more →

Minimum swaps required to bring elements <= K together

Read more →

Minimum number of merge operations required to make an array palindrome

Read more →

Median of 2 sorted arrays of equal size

Read more →

Median of 2 sorted arrays of different size

Read more →

Merge 2 sorted arrays

Read more →