This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Article Link: https://takeuforward.org/data-structure/longest-consecutive-sequence-in-an-array/ | |
// Problem Link: https://leetcode.com/problems/longest-consecutive-sequence/ | |
// Brute Force Approch | |
// TC -> O(n * n * n) | |
// SC -> O(1) | |
// Thought Process | |
// 1. We will try to find one greater element for every element using binary search. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Article Link: https://takeuforward.org/data-structure/leaders-in-an-array/ | |
// Problem Link: https://www.geeksforgeeks.org/problems/leaders-in-an-array-1587115620/1 | |
// Brute Force Approch | |
// TC -> O(n * n) | |
// SC -> O(n) <- (Used to return the result, not the solve the problem) | |
// Thought Process | |
// We will check every element if it is greater than all the elements on the right side. | |
// We will consider them as a leader. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem link: https://leetcode.com/problems/next-permutation/description/ | |
// Article Link: https://takeuforward.org/data-structure/next_permutation-find-next-lexicographically-greater-permutation/ | |
// Optimal Approch | |
// TC -> O(n + n + n/2 - 2) | |
// SC -> O(1) | |
func nextPermutation(_ arr: inout [Int]) { | |
let n = arr.count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem Link: https://www.geeksforgeeks.org/problems/max-sum-in-sub-arrays0824/0 | |
// Brute Force Approch | |
// TC -> O(n * n) | |
// SC -> O(1) | |
// Thought Process | |
// 1. I will generate all the sub-arrays. | |
// 2. Whenever a new element is inserted into the range, check with smallest and second smallest number. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem Link: https://www.geeksforgeeks.org/problems/union-of-two-sorted-arrays-1587115621/1 | |
// Brute Force Approch | |
// TC -> O(nlogk) + O(mlogk) + O(n+m) | |
// where k is size of set. | |
// SC -> O(n+m) + O(n + m) | |
// 1. O(n+m) -> For solve the problem | |
// 2. O(n+m) -> For return the answer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem Link: https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/description/ | |
// Better Solution -> O(2n) | |
// SC -> O(1) | |
func check(_ nums: [Int]) -> Bool { | |
var n = nums.count | |
if n == 1 { | |
return true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Question Link: https://leetcode.com/problems/rearrange-array-elements-by-sign/description/ | |
// Article Link: https://takeuforward.org/arrays/rearrange-array-elements-by-sign/ | |
// Better approch | |
// TC -> O(n) | |
// SC -> O(n) | |
func rearrangeArray(_ nums: [Int]) -> [Int] { | |
var newArr = Array<Int>(repeating: 0, count: nums.count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Article Link: https://takeuforward.org/data-structure/count-frequency-of-each-element-in-the-array/ | |
// TC -> O(n * n) | |
// SC -> O(n) | |
func printFrequency(arr: [Int]) { | |
var n = arr.count | |
var visitedArray = Array<Bool>(repeating: false, count: n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//LeetCode: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ | |
// Article Link: https://takeuforward.org/data-structure/stock-buy-and-sell/ | |
// Brute Force Approch | |
// TC -> O(n*n) | |
// SC -> O(1) | |
// Thought Proces: | |
// 1. I will buy stock every day and will sell stocks every day. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Question Link: https://www.geeksforgeeks.org/problems/frequency-of-array-elements-1587115620/0 | |
//Video Reference: https://www.youtube.com/watch?v=B2hI-QPoisk | |
//Optimal Approch | |
//TC -> O(2N) | |
//SC -> O(1) | |
public static void frequencyCount(int arr[], int N, int P) | |
{ | |
int i = 0; |
NewerOlder