- 63
- 3 441
Gale Praveen
India
เข้าร่วมเมื่อ 5 พ.ย. 2018
Welcome to My Channel 🎉
I’m Gale Praveen, a software engineer, have been practicing Data Structures and Algorithms for over 2 years. Join me as we dive deep into the world of DSA, exploring concepts, tackling challenges, and honing our problem-solving skills together!
On this channel, you’ll find engaging tutorials, practical applications, and tips to help you excel in coding interviews. Whether you’re a beginner or looking to deepen your knowledge, my goal is to make learning DSA and System Design topics enjoyable and accessible.
Let’s break down complex ideas, share insights, and build a vibrant coding community. Grab your favorite drink, settle in, and let’s conquer DSA and System Design Problems.
Don’t forget to subscribe and be part of our journey!
I’m Gale Praveen, a software engineer, have been practicing Data Structures and Algorithms for over 2 years. Join me as we dive deep into the world of DSA, exploring concepts, tackling challenges, and honing our problem-solving skills together!
On this channel, you’ll find engaging tutorials, practical applications, and tips to help you excel in coding interviews. Whether you’re a beginner or looking to deepen your knowledge, my goal is to make learning DSA and System Design topics enjoyable and accessible.
Let’s break down complex ideas, share insights, and build a vibrant coding community. Grab your favorite drink, settle in, and let’s conquer DSA and System Design Problems.
Don’t forget to subscribe and be part of our journey!
Sorted and Rotated Minimum | EASY | Arrays
Problem Link: www.geeksforgeeks.org/problems/minimum-element-in-a-sorted-and-rotated-array3611/1
Hey Everyone,
In this video, we'll be solving the problem of finding the minimum element in a rotated sorted array, which may contain duplicates. A rotated sorted array is a sorted array that has been rotated at some unknown pivot point.
Problem Breakdown:
Given a sorted array arr[] that may have been rotated at an unknown pivot, we need to find the minimum element efficiently.
The provided solution uses a linear approach to traverse the array and compare adjacent elements, returning the element where the sequence breaks (i.e., where the current element is smaller than the previous one). If no such break occurs, the array is considered not rotated, and the first element is returned as the minimum.
Key Concepts:
Rotated sorted array
Linear search approach for finding the minimum element
Handling arrays with duplicates
Video Highlights:
Explanation of the problem and the solution approach
Walkthrough of the code step by step
Time and space complexity analysis
#Algorithm #Programming #Coding #RotatedArray #LeetCode #DataStructures #TechTutorial #gfg #gfgpotd #gfgsolutions #gfgpotdtoday #coding #helpajobseeker #leetcode #gfg #gfgstreek #cpp #motivation #motivation #developers #codingtools #gfg #potd #linkedlist #gfglearning #explore #leetcodedailychallenge #geek #geeksforgeeks #linkedlist #twopointers #algorithms #logic #logical #array #datastructure #algorithms #cplusplus #binarysearch
Hey Everyone,
In this video, we'll be solving the problem of finding the minimum element in a rotated sorted array, which may contain duplicates. A rotated sorted array is a sorted array that has been rotated at some unknown pivot point.
Problem Breakdown:
Given a sorted array arr[] that may have been rotated at an unknown pivot, we need to find the minimum element efficiently.
The provided solution uses a linear approach to traverse the array and compare adjacent elements, returning the element where the sequence breaks (i.e., where the current element is smaller than the previous one). If no such break occurs, the array is considered not rotated, and the first element is returned as the minimum.
Key Concepts:
Rotated sorted array
Linear search approach for finding the minimum element
Handling arrays with duplicates
Video Highlights:
Explanation of the problem and the solution approach
Walkthrough of the code step by step
Time and space complexity analysis
#Algorithm #Programming #Coding #RotatedArray #LeetCode #DataStructures #TechTutorial #gfg #gfgpotd #gfgsolutions #gfgpotdtoday #coding #helpajobseeker #leetcode #gfg #gfgstreek #cpp #motivation #motivation #developers #codingtools #gfg #potd #linkedlist #gfglearning #explore #leetcodedailychallenge #geek #geeksforgeeks #linkedlist #twopointers #algorithms #logic #logical #array #datastructure #algorithms #cplusplus #binarysearch
มุมมอง: 38
วีดีโอ
Number of occurrence | Binary Search | Easy
มุมมอง 4114 ชั่วโมงที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/number-of-occurrence2259/1 Hey everyone! 👋 In this video, we demonstrate how to efficiently find the number of occurrences of a target element in a sorted array using binary search methods in C . We'll explain the lower_bound and upper_bound functions, which help in identifying the range of the target element in a sorted list. ✨ Key Concepts Covered:...
Merge Without Extra Space | Sorting | EASY
มุมมอง 716 ชั่วโมงที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/merge-two-sorted-arrays-1587115620/1 Hey everyone! 👋 In this video, we explain how to merge two sorted arrays a[] and b[] in non-decreasing order without using any extra space. This approach modifies the input arrays in place, ensuring that array a[] contains the first n elements, and array b[] contains the last m elements, while keeping both arrays ...
Non-overlapping Intervals | Sorting | EASY
มุมมอง 1116 ชั่วโมงที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/non-overlapping-intervals/1 Link to merge intervals: th-cam.com/video/qA3ehDpHbQ0/w-d-xo.html Hey everyone! 👋 In this video, we'll walk you through a C solution for the problem of removing the minimum number of intervals to make the rest non-overlapping. This is a common problem involving sorting and greedy algorithms. Problem Explanation: Given a 2D...
Insert Interval | Sorting | MEDIUM
มุมมอง 1621 ชั่วโมงที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/insert-interval-1666733333/1 Link to merge intervals: th-cam.com/video/qA3ehDpHbQ0/w-d-xo.html Hey everyone! 👋 In this video, we walk through a C solution to a common problem: inserting a new interval into a sorted list of non-overlapping intervals and merging any overlapping intervals if necessary. The given intervals are sorted by their start time,...
Overlapping Intervals | Sorting | MEDIUM
มุมมอง 20วันที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/overlapping-intervals 170633/1 Hey everyone! 👋 In this video, we walk through the solution to the problem of merging overlapping intervals. Given an array of intervals, where each interval is represented by a pair of start and end values, the task is to merge all overlapping intervals into a single one. We will cover the following key points: Sorting...
Find H-Index | Arrays | MEDIUM
มุมมอง 10วันที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/find-h-index 165609/1 Hey everyone! 👋 Description: In this video, we dive into the concept of the H-Index and how to efficiently calculate it using a C solution. The H-Index is a metric used to measure the productivity and citation impact of a researcher's work. It is defined as the largest number H such that the researcher has at least H papers with...
Sort 0s, 1s and 2s | Arrays | EASY
มุมมอง 24วันที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s4231/1 Hey everyone! 👋 In this video, we demonstrate an efficient approach to sorting an array containing only 0s, 1s, and 2s using the Dutch National Flag Algorithm. This algorithm allows us to sort the array in linear time, O(n), without needing extra space. We will walk through a C implementation that sorts the array i...
Strings Rotations of Each Other | Strings | EASY
มุมมอง 3314 วันที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/check-if-strings-are-rotations-of-each-other-or-not-1587115620/1 Link to KMP algorithm: th-cam.com/video/WYMp9w_VO5Y/w-d-xo.htmlsi=DfqdMsgojiCAuvJ0 Hey everyone! 👋 In this video, we will walk you through an efficient way to determine if one string is a rotated version of another using C . The algorithm utilizes the concept of pattern matching combine...
Search Pattern (KMP-Algorithm) | Strings | HARD
มุมมอง 1514 วันที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/search-pattern0205/1 Hey everyone! 👋 Welcome to this deep dive into the Knuth-Morris-Pratt (KMP) Algorithm, one of the most efficient algorithms for string matching. In this video, we break down the key concepts behind KMP, exploring how it improves pattern matching performance by reducing unnecessary comparisons. We’ll cover: What is the KMP Algorit...
Min Chars to Add for Palindrome | Strings | HARD
มุมมอง 4914 วันที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/minimum-characters-to-be-added-at-front-to-make-string-palindrome/1 Link to KMP Algorithm Video: th-cam.com/video/WYMp9w_VO5Y/w-d-xo.html Hey everyone! 👋 In this video, we dive into a fascinating algorithm problem: How to find the minimum characters to add at the front of a string to make it a palindrome? A palindrome is a string that reads the same ...
Non Repeating Character | Strings | EASY
มุมมอง 1814 วันที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/non-repeating-character-1587115620/1 Hey everyone! 👋 🔍 In this video, we’ll show you how to find the first non-repeating character in a string of lowercase Latin letters! 🌟 💡 We’ll walk through a simple and efficient approach where we count the frequency of each character and then identify the first one that appears only once. If all characters repea...
Anagrams | Strings | EASY
มุมมอง 914 วันที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/anagram-1587115620/1 Hey everyone! 👋 In this video, we will explore how to check if two strings are anagrams of each other using an efficient algorithm in C 🔍💻. An anagram is a word or phrase formed by rearranging the letters of another, using all the original letters exactly once 🔄. We'll walk through the implementation of the areAnagrams function, ...
Add Binary Strings | Strings | MEDIUM
มุมมอง 1714 วันที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/add-binary-strings3805/1 Hey everyone! 👋 In this video, we walk through an efficient algorithm to add two binary strings. The input strings may contain leading zeros, but the output will not. The function addBinary adds the binary strings bit by bit from right to left, handling carries just like traditional binary addition. 🔢💻 Key Concepts: Binary Ad...
Implement Atoi | Arrays | MEDIUM
มุมมอง 1514 วันที่ผ่านมา
Problem Link: www.geeksforgeeks.org/problems/implement-atoi/1 Hey everyone! 👋 📌 Title: Implementing myAtoi() - Convert String to Integer in C (Custom Solution) In this video, we walk through how to implement the myAtoi() function in C to convert a string into an integer. This custom solution follows the same principles as the atoi() function, but without using any built-in functions. We handle ...
Smallest Positive Missing Number| Arrays | MEDIUM
มุมมอง 4914 วันที่ผ่านมา
Smallest Positive Missing Number| Arrays | MEDIUM
Maximum Circular Subarray Sum | Arrays | HARD
มุมมอง 5721 วันที่ผ่านมา
Maximum Circular Subarray Sum | Arrays | HARD
Maximum Product Subarray | Arrays | MEDIUM
มุมมอง 3821 วันที่ผ่านมา
Maximum Product Subarray | Arrays | MEDIUM
Minimize the Heights I | Arrays | MEDIUM
มุมมอง 5121 วันที่ผ่านมา
Minimize the Heights I | Arrays | MEDIUM
Stock Buy and Sell - Max one Transaction Allowed | Arrays | EASY
มุมมอง 4121 วันที่ผ่านมา
Stock Buy and Sell - Max one Transaction Allowed | Arrays | EASY
Stock Buy and Sell - Multiple Transaction Allowed | Dynamic Programming | MEDIUM
มุมมอง 3721 วันที่ผ่านมา
Stock Buy and Sell - Multiple Transaction Allowed | Dynamic Programming | MEDIUM
Move All Zeroes to End | Arrays | EASY LEVEL | Girls and Boys Seating Arrangement Example
มุมมอง 38หลายเดือนก่อน
Move All Zeroes to End | Arrays | EASY LEVEL | Girls and Boys Seating Arrangement Example
Intersection Point in Y Shaped Linked Lists | Linked Lists | MEDIUM
มุมมอง 20หลายเดือนก่อน
Intersection Point in Y Shaped Linked Lists | Linked Lists | MEDIUM
Great explanation 🎉looking forward for more videos like these 👏
It made my day you liked the video 😍
Nice explanation 👌
Glad to Hear, keep supporting 🥳🥳
Best Explanation, very intuitive and indepth explanation 😍😍
Thank you so much for the kind words 😍. Keep supporting and learning
Brute Force Approach: int countFreq(vector<int>& arr, int target) { int n = arr.size(); int count = 0; for(int i = 0; i < n; ++i){ if(arr[i] == target) count += 1; } return count; } Optimised Approach: int countFreq(vector<int>& arr, int target) { int n = arr.size(); int startIndex = lower_bound(begin(arr), end(arr), target) - begin(arr); int endIndex = upper_bound(begin(arr), end(arr), target) - begin(arr); return endIndex - startIndex; }
👏 👏
🥳🥳
int minRemoval(vector<vector<int>> &intervals) { sort(begin(intervals), end(intervals), [&](vector<int>& a,vector<int>& b){ return a[1] < b[1]; }); int n = intervals.size(); int count = 0; int endInterval = intervals[0][1]; for(int i = 1; i < n; ++i){ if(endInterval > intervals[i][0]){ count += 1; }else{ endInterval = intervals[i][1]; } } return count; }
Wonderful video sir 👏 looks like you've poured your whole heart into the video...please continue making videos this way 🎉please reveal your face too 👏 👏
@@Angopoonam Thanks for the comment, it made me so happy 😍. Also, my face is visible in my DP, although I'm not that good looking guy 😅
int hIndex(vector<int>& citations) { int n = citations.size(); vector<int> freq(n + 1, 0); for(int i = 0; i < n; ++i){ if(citations[i] >= n){ freq[n] += 1; } else { freq[citations[i]] += 1; } } int count = 0; int i = n; while(i > 0){ count += freq[i]; if(count >= i) break; i -= 1; } return i; }
BRUTE FORCE: bool areRotations(string &s1, string &s2) { int n = s1.size(); int count = n; while(count--){ if(s1 == s2) return true; s1.push_back(s1.front()); s1.erase(0, 1); } return false; } USING FIND METHOD: bool areRotations(string &s1, string &s2) { s1 = s1 + s1; return s1.find(s2) != -1; } USING KMP: th-cam.com/video/WYMp9w_VO5Y/w-d-xo.html bool areRotations(string &s1, string &s2) { s1 = s1 + s1; int n = s1.size(), m = s2.size(); // 1. Generating LPS array vector<int> lps(m, 0); int length = 0; int i = 1; while(i < m){ if(s2[i] == s2[length]){ ++length; lps[i] = length; ++i; }else{ if(length > 0) length = lps[length - 1]; else ++i; } } // 2. Matching Pattern (Only Once) i = 0; int j = 0; while(j < n){ if(s1[j] == s2[i]){ ++j; ++i; }else{ if(i > 0) i = lps[i - 1]; else ++j; } if(i == m) return true; } return false; }
One short correction in the example explanation, the overall string should be cbaabc, it will work
Yeah, sorry for that 😅
vector<int> search(string& pat, string& txt) { int n = pat.size(), m = txt.size(); vector<int> lps(n, 0); int length = 0; int i = 1; // 1. Generating LPS array while(i < n){ if(pat[i] == pat[length]){ length += 1; lps[i] = length; i += 1; }else{ if(length > 0){ length = lps[length - 1]; }else{ i += 1; } } } // 2. Matching Pattern vector<int> res; i = 0; int j = 0; while(j < m){ if(txt[j] == pat[i]){ i += 1; j += 1; }else{ if(i > 0){ i = lps[i - 1]; }else{ j += 1; } } if(i == n){ res.push_back(j - i); i = lps[i - 1]; } } return res; }
int minChar(string& s) { string temp = s; reverse(begin(temp), end(temp)); string str = s + temp; int n = str.size(); vector<int> lps(n, 0); int i = 1; int length = 0; while(i < n){ if(str[i] == str[length]){ ++length; lps[i] = length; ++i; }else{ if(length > 0) length = lps[length - 1]; else ++i; } } return s.size() - lps[n-1]; }
char nonRepeatingChar(string &s) { vector<int> freq(26, 0); for(auto& ch : s) freq[ch - 'a'] += 1; for(auto& ch : s) if(freq[ch - 'a'] == 1) return ch; return '$'; }
bool areAnagrams(string& s1, string& s2) { if(s1.size() != s2.size()) return false; vector<int> freq(26, 0); for(auto& ch : s1) freq[ch - 'a'] += 1; for(auto& ch : s2) freq[ch - 'a'] -= 1; for(auto& num : freq){ if(num != 0) return false; } return true; }
string addBinary(string& s1, string& s2) { int n = s1.size(), m = s2.size(); int i = n-1, j = m-1; string res = ""; int carry = 0; while(i >= 0 && j >= 0){ int sum = 0; sum += s1[i] == '1'; sum += s2[j] == '1'; sum += carry; res += (sum % 2) + '0'; carry = sum / 2; --i; --j; } while(i >= 0){ carry += s1[i] == '1'; res += (carry % 2) + '0'; carry /= 2; --i; } while(j >= 0){ carry += s2[j] == '1'; res += (carry % 2) + '0'; carry /= 2; --j; } if(carry) res += carry + '0'; while(!res.empty() && res.back() == '0') res.pop_back(); reverse(begin(res), end(res)); return res; }
int myAtoi(char *s) { int n = strlen(s); int i = 0; while(i < n && s[i] == ' '){ ++i; } if(i == n) return 0; bool isNeg = false; if(s[i] == '-'){ isNeg = true; ++i; } bool allZeros = true; long long res = 0; while(i < n){ int digit = s[i] - '0'; if(digit < 0 || digit > 9){ break; } res = (res * 10) + digit; if(isNeg){ if(-res < INT_MIN) return INT_MIN; }else{ if(res > INT_MAX) return INT_MAX; } ++i; } return isNeg ? -res : res; }
BRUTE FORCE: int missingNumber(vector<int> &arr) { unordered_set<int> set; for(auto& num : arr){ if(num > 0) set.insert(num); } int numToFind = 1; while(set.count(numToFind)) numToFind += 1; return numToFind; } OPTIMISED: int missingNumber(vector<int> &arr) { int n = arr.size(); for(int i = 0; i < n; ++i){ while(arr[i] > 0 && arr[i] < n && arr[arr[i] - 1] != arr[i]){ swap(arr[i], arr[arr[i] - 1]); } } for(int i = 0; i < n; ++i){ if(arr[i] != i + 1) return i + 1; } return n + 1; }
Great video Sir, the application of Kadane's algorithm makes it more easier and your explanation makes it more simpler to understand...please continue teaching, understood every bit of the video🥳💯Thank you so much for coming up with this channel🥳
That means a lot Poonam 😍. Keep learning 🥳
Understood the algorithm really well Sir, it's locked in my brain forever, I was able to write the code and dry run by myself just by listening to your example explanation, Thanks...please continue teaching, more success to come🤩🥳
It made my day that I was able to help 😍
This is the most promising explanation, everyone else just explains the code but not the intuition,was searching for this !
Thanks Rama ❤️. Keep the spirit of understanding the logic rather than code 🥳
Nice explanation, The way your explaining is really fantastic.
Thank you so much. Keep learning ❤️
Link to Kadane's Algorithm: th-cam.com/video/iAqw7soVZvA/w-d-xo.html int circularSubarraySum(vector<int> &arr) { int n = arr.size(); int maxSum = INT_MIN, minSum = INT_MAX; int currMaxSum = 0, currMinSum = 0; int totalSum = 0; for(int i = 0; i < n; ++i){ totalSum += arr[i]; currMaxSum += arr[i]; maxSum = max(maxSum, currMaxSum); if(currMaxSum < 0) currMaxSum = 0; currMinSum += arr[i]; minSum = min(minSum, currMinSum); if(currMinSum > 0) currMinSum = 0; } return max(maxSum, totalSum - minSum); }
Link to Kadane's Algorithm: th-cam.com/video/iAqw7soVZvA/w-d-xo.html int maxProduct(vector<int> &arr) { int n = arr.size(); int maxProd = INT_MIN; int currProd = 1; for(int i = 0; i < n; ++i){ currProd *= arr[i]; maxProd = max(maxProd, currProd); if(currProd == 0) currProd = 1; } currProd = 1; for(int i = n-1; i >= 0; --i){ currProd *= arr[i]; maxProd = max(maxProd, currProd); if(currProd == 0) currProd = 1; } return maxProd; }
int maxSubarraySum(vector<int> &arr) { int n = arr.size(); int maxSum = INT_MIN, currSum = 0; for(int i = 0; i < n; ++i){ currSum += arr[i]; maxSum = max(maxSum, currSum); if(currSum < 0) currSum = 0; } return maxSum; }
Best channel ever🎉wonderful...please continue teaching the same way...lots and lots of success coming your way sir🎉😊
@@Angopoonam thanks for the kind words. It made my day 😍
int getMinDiff(int k, vector<int> &arr) { int n = arr.size(); sort(begin(arr), end(arr)); int minHeight = arr[n-1] - arr[0]; for(int i = 1; i < n; ++i){ int shortHeight = min(arr[0] + k, arr[i] - k); int longHeight = max(arr[n-1] - k, arr[i-1] + k); minHeight = min(minHeight, longHeight - shortHeight); } return minHeight; }
int maximumProfit(vector<int> &prices) { int n = prices.size(); int minPrice = INT_MAX; int maxProfit = 0; for(int i = 0; i < n; ++i){ minPrice = min(minPrice, prices[i]); maxProfit = max(maxProfit, prices[i] - minPrice); } return maxProfit; }
int maximumProfit(vector<int> &prices) { int n = prices.size(); vector<vector<int> > dp(n+1, vector<int>(2, -1)); function<int(int,bool)> dfs = [&](int i, bool buy)->int{ if(i >= n) return 0; // caching/memoising int& cache = dp[i][buy]; if(cache != -1) return cache; int profit; if(buy){ int ignoreForBestSell = dfs(i+1, true); int sellRightNow = prices[i] + dfs(i+1, false); profit = max(ignoreForBestSell, sellRightNow); }else{ int ignoreForBestBuy = dfs(i+1, false); int buyRightNow = -prices[i] + dfs(i+1, true); profit = max(ignoreForBestBuy, buyRightNow); } return cache = profit; }; return dfs(0, false); }
vector<int> findMajority(vector<int>& arr) { int n = arr.size(); int num1 = 0, num2 = 0; int count1 = 0, count2 = 0; for(int i = 0; i < n; ++i){ if(arr[i] == num1){ count1 += 1; }else if(arr[i] == num2){ count2 += 1; } else if(count1 == 0){ num1 = arr[i]; count1 = 1; }else if(count2 == 0){ num2 = arr[i]; count2 = 1; }else{ count1 -= 1, count2 -= 1; } } count1 = count2 = 0; for(int i = 0; i < n; ++i){ if(arr[i] == num1){ count1 += 1; }else if(arr[i] == num2){ count2 += 1; } } vector<int> res; if(count1 > n/3) res.push_back(num1); if(count2 > n/3) res.push_back(num2); sort(begin(res), end(res)); return res; }
void nextPermutation(vector<int>& arr) { int n = arr.size(); int ind1 = -1; for(int i = n-2; i >= 0; --i){ if(arr[i] < arr[i+1]){ ind1 = i; break; } } if(ind1 == -1){ reverse(begin(arr), end(arr)); return; } int ind2 = -1; for(int i = n-1; i > ind1; --i){ if(arr[i] > arr[ind1]){ ind2 = i; break; } } swap(arr[ind1], arr[ind2]); reverse(begin(arr) + ind1 + 1, end(arr)); }
Link to reverse array: th-cam.com/video/KVH0BOjSFak/w-d-xo.htmlsi=SkVJE7UREARAnVgC void rotateArr(vector<int>& arr, int d) { int n = arr.size(); auto reverse = [&](int start, int end){ while(start < end){ swap(arr[start], arr[end]); start += 1; end -= 1; } }; d %= n; reverse(0, n-1); reverse(0, n-d-1); reverse(n-d, n-1); }
Very nice explanation Sir...want more teaching videos like this...AMAZING👏 👌 🎉
Thanks Poonam 🤩. Keep learning 🤩🤩
void reverseArray(vector<int> &arr) { int i = 0, j = arr.size() - 1; while(i < j){ swap(arr[i], arr[j]); i += 1; j -= 1; } }
The example used by you is very relatable and very easily understandable sir...please continue teaching like this🎉🎉This channel will reach great heights one day,for sure 🥳Thanks for making it easier
Thank you for such kind words 🤩🤩. Keep learning 🥳
void pushZerosToEnd(vector<int>& arr) { int n = arr.size(); int nonZeroIndex = 0; for(int i = 0; i < n; ++i){ if(arr[i] == 0) continue; swap(arr[nonZeroIndex], arr[i]); nonZeroIndex += 1; } for(int i = nonZeroIndex + 1; i < n; ++i){ arr[i] = 0; } }
Greattt Solution , nice explanation
Thanks for watching
Great Buddiiiessss Superbbbbbb
Thanks 🤗
int getSecondLargest(vector<int> &arr) { int n = arr.size(); int largest = -1, secondLargest = -1; for(int i = 0; i < n; ++i){ if(arr[i] > largest){ secondLargest = largest; largest = arr[i]; }else if(arr[i] > secondLargest && arr[i] != largest){ secondLargest = arr[i]; } } return secondLargest; }
void nearlySorted(vector<int>& arr, int k) { priority_queue<int, vector<int>, greater<int> > pq; for(int i = 0; i < k; ++i){ pq.push(arr[i]); } int n = arr.size(); int j = 0; for(int i = k; i < n; ++i){ pq.push(arr[i]); int mini = pq.top(); pq.pop(); arr[j++] = mini; } while(!pq.empty()){ arr[j++] = pq.top(); pq.pop(); } }
int findLength(Node* head){ int length = 0; while(head){ head = head->next; length += 1; } return length; } int findIntersection(Node* head1, Node* head2){ while(head1 && head2){ if(head1 == head2) return head1->data; head1 = head1->next; head2 = head2->next; } return -1; } // Function to find intersection point in Y shaped Linked Lists. int intersectPoint(Node* head1, Node* head2) { int len1 = findLength(head1); int len2 = findLength(head2); while(len1 > len2){ head1 = head1->next; len1 -= 1; } while(len2 > len1){ head2 = head2->next; len2 -= 1; } return findIntersection(head1, head2); }
bool canAttend(vector<vector<int>> &arr) { sort(begin(arr), end(arr), [&](vector<int>& a, vector<int>& b){ return a[1] < b[1]; }); int endTime = arr[0][1]; int n = arr.size(); for(int i = 1; i < n; ++i){ if(arr[i][0] >= endTime){ endTime = arr[i][1]; }else return false; } return true; }
int minIncrements(vector<int> arr) { int n = arr.size(); sort(begin(arr), end(arr)); int increments = 0; for(int i = 1; i < n; ++i){ if(arr[i] <= arr[i-1]){ int difference = arr[i-1] - arr[i]; arr[i] = arr[i-1] + 1; increments += difference + 1; } } return increments; }
vector<int> findUnion(vector<int> &a, vector<int> &b) { int n = a.size(), m = b.size(); int i = 0, j = 0; vector<int> res; while(i < n && j < m){ int num1 = a[i], num2 = b[j]; if(num1 == num2){ res.push_back(num1); i += 1; j += 1; } else if(num1 < num2){ res.push_back(num1); i += 1; }else{ res.push_back(num2); j += 1; } } while(i < n){ res.push_back(a[i]); i += 1; } while(j < m){ res.push_back(b[j]); j += 1; } return res; }
string minSum(vector<int> &arr) { sort(begin(arr), end(arr), greater<int>()); int n = arr.size(); int carry = 0; string res = ""; for(int i = 0; i < n; i += 2){ int sum = 0; sum += arr[i]; if(i+1 < n) sum += arr[i+1]; sum += carry; res += (sum % 10) + '0'; carry = sum / 10; } if(carry) res += carry + '0'; while(!res.empty() && res.back() == '0') res.pop_back(); reverse(begin(res), end(res)); return res; }
int minRepeats(string& s1, string& s2) { string temp = s1; int count = 1; if(s1 == s2) return count; while(temp.size() < s2.size()){ temp += s1; count += 1; if(temp.find(s2) != string::npos) return count; } temp += s1; count += 1; if(temp.find(s2) != string::npos){ return count; } return -1; }
vector<int> findSplit(vector<int>& arr) { int total = accumulate(begin(arr), end(arr), 0); int n = arr.size(); if(total == 0) return {0, 0}; if(total % 3 != 0) return {-1, -1}; int equalHalves = total / 3; int firstHalf = 0; int i = 0; while(i < n && firstHalf + arr[i] <= equalHalves){ firstHalf += arr[i]; i += 1; } if(i == n || firstHalf != equalHalves) return {-1, -1}; int thirdHalf = 0; int j = n - 1; while(j >= i && thirdHalf + arr[j] <= equalHalves){ thirdHalf += arr[j]; j -= 1; } if(thirdHalf != equalHalves) return {-1, -1}; return {i, j}; }
int treePathsSum(Node *root) { int totalSum = 0; function<void(Node*, int)> preOrder = [&](Node* node, int currNum){ if(!node){ return; } currNum = currNum * 10 + node->data; if(!node->left && !node->right){ totalSum += currNum; return; } preOrder(node->left, currNum); preOrder(node->right, currNum); }; preOrder(root, 0); return totalSum; }
void rotate(vector<vector<int> >& matrix) { int n = matrix.size(); for(int i = 0; i < n; ++i){ for(int j = i+1; j < n ; ++j){ swap(matrix[i][j], matrix[j][i]); } } for(int i = 0; i < n; ++i){ reverse(begin(matrix[i]), end(matrix[i])); } }
This code failing for a test case, can you explain why? vector<vector<int>> findTriplets(vector<int> &arr) { vector<vector<int>>ans; int n = arr.size(); for (int i = 0; i < n; i++) { unordered_map<int, int> m; for (int j = i+1 ; j < n; j++) { int third = -(arr[i] + arr[j]); if (m.find(third) != m.end()) { vector<int> temp = {i,m[third],j}; sort(temp.begin(),temp.end()) ans.push_back(temp); } m[arr[j]] = j; } } return ans; }
Could you please mention the test case
@galepraveen the test case is really huge and it's from gfg 1109/1110 cases are passing but one is failing
If you copy paste the same code that I gave in gfg "Find all triplets with sum zero", you will get it
@@chanchalchauhan1237 i got it bro, with your code, some of the outputs are missing. Try downloading the actual and expected output and compare them in any of the online platform, you'll get it.
@@chanchalchauhan1237 As I observed the missing outputs with your logic, the code is not working as expected i.e., some of the cases are missed . Thanks for sharing 🎉
EASY C++ SOLUTION ❤ (for beginners) int n = arr.size(); vector<vector<int>> res; // 2-D VECTOR //vector<int> res; 1-D VECTOR for(int i=0; i<n; i++){ for(int j=i+1; j<n; j++){ for(int k=j+1; k<n; k++){ if(arr[i]+arr[j]+arr[k] == 0){ res.push_back( {i,j,k} ); // if use 1-d vector so return like this // res.push_back(i); // res.push_back(j); // res.push_back(k); } } } } return res; }
Thanks for the brute force code ❤