Skip to content

Instantly share code, notes, and snippets.

View avii-7's full-sized avatar
🚣
On a raft.

A V I I avii-7

🚣
On a raft.
View GitHub Profile
@avii-7
avii-7 / A32.swift
Last active June 23, 2024 16:52
Longest Consecutive Sequence in an Array.
// 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.
@avii-7
avii-7 / A31.swift
Created June 22, 2024 11:55
Leaders in an Array (GFG: Array Leaders)
// 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.
@avii-7
avii-7 / A30.swift
Created June 20, 2024 18:12
next_permutation : find next lexicographically greater permutation ( 31. Next Permutation )
// 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
@avii-7
avii-7 / A29.java
Last active June 16, 2024 08:02
Max sum in sub-arrays ( 2 )
// 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.
@avii-7
avii-7 / A11.java
Last active June 4, 2024 15:49
Union of Two Sorted Arrays
// 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.
@avii-7
avii-7 / A28.swift
Created June 1, 2024 07:34
1752. Check if Array Is Sorted and Rotated
// 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
@avii-7
avii-7 / A27.swift
Last active June 29, 2024 10:53
2149. Rearrange Array Elements by Sign | A27.swift
// 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)
@avii-7
avii-7 / A26.swift
Last active June 29, 2024 19:13
Count(Print) frequency of each element in the array | A26.swift
// 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)
@avii-7
avii-7 / A25.swift
Last active June 16, 2024 09:49
121. Best Time to Buy and Sell Stock
//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.
@avii-7
avii-7 / A24.java
Last active June 24, 2024 18:54
Count frequency of each element in the array ( Frequency of limited range elements )
//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;