Neha Bhatia’s Post

View profile for Neha Bhatia, graphic

SWE'24 | Software Engineer | Ex-Senior Software Engineer @ HSBC | AWS Certified | Actively looking for SDE roles | MS in Computer Science

✨ 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴: 𝗙𝗶𝗻𝗱𝗶𝗻𝗴 𝘁𝗵𝗲 𝗞𝘁𝗵 𝗟𝗮𝗿𝗴𝗲𝘀𝘁 𝗘𝗹𝗲𝗺𝗲𝗻𝘁 𝗘𝗳𝗳𝗶𝗰𝗶𝗲𝗻𝘁𝗹𝘆 ✨ Today, I tackled a LeetCode problem that initially seemed simple: finding the kth largest element in an array. Initially, the straightforward solution was to sort the array and pick the kth largest from the end. But sorting takes O(nlogn) time—what if we could do better? After some thought, I considered heaps! With a heap, we can reduce the time complexity to O(nlogk) by maintaining only the k largest elements. But that’s not where the magic ends... 𝗘𝗻𝘁𝗲𝗿 𝗖𝗼𝘂𝗻𝘁𝗶𝗻𝗴 𝗦𝗼𝗿𝘁! 🚀 I implemented counting sort, which gave me an elegant O(n) solution. Here's how it works: 1. 𝗙𝗶𝗻𝗱 𝘁𝗵𝗲 𝗠𝗶𝗻𝗶𝗺𝘂𝗺 𝗮𝗻𝗱 𝗠𝗮𝘅𝗶𝗺𝘂𝗺 𝗩𝗮𝗹𝘂𝗲𝘀: First, I identified the smallest and largest elements in the array to determine the range. 2. 𝗖𝗿𝗲𝗮𝘁𝗲 𝗮 𝗙𝗿𝗲𝗾𝘂𝗲𝗻𝗰𝘆 𝗔𝗿𝗿𝗮𝘆: When creating the frequency array, I considered an important detail: the array can have negative values. So, instead of just creating an array based on the maximum value, I also had to account for the minimum value. This means the array size is max - min + 1. For example, if the array is [-3, -1, 2, 4, -2], the minimum value is -3 and the maximum value is 4. To place the numbers in their correct positions in the frequency array, I use the formula num - minv. This means: • -3 becomes 0 (because -3 - (-3) = 0) • -1 becomes 2 (because -1 - (-3) = 2) • 2 becomes 5 (because 2 - (-3) = 5) • 4 becomes 7, and so on. This way, all negative numbers get valid positions in the array. 3. 𝗧𝗿𝗮𝘃𝗲𝗿𝘀𝗲 𝘁𝗵𝗲 𝗙𝗿𝗲𝗾𝘂𝗲𝗻𝗰𝘆 𝗔𝗿𝗿𝗮𝘆: Starting from the largest value, I decrement k by the count at each index. When k reaches zero or less, I've found the kth largest element. 𝗖𝗼𝗻𝘀𝗶𝗱𝗲𝗿𝗮𝘁𝗶𝗼𝗻𝘀 𝗮𝗻𝗱 𝗗𝗶𝘀𝗮𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲𝘀: While counting sort is a powerful technique, it cannot be used everywhere. Here are some disadvantages to keep in mind: • Space Complexity: The frequency array size is max - min + 1, which can lead to high memory usage if the range of numbers is large. • Inefficiency with Sparse Data: In cases where numbers are limited but widely spread apart, the frequency array can waste space. • Limited Applicability: It works primarily for integers within a defined range and may not suit other data types. • Potential for Integer Overflow: When calculating max and min in large arrays, there's a risk of overflow in some languages (though not in Java). • Less Generalized: Unlike other algorithms like Quickselect, counting sort is specifically for integers, limiting its versatility. This approach avoids sorting, making the solution much more efficient. I am excited to keep learning and applying these techniques as I go! 🙌 Here is the link to the problem - https://lnkd.in/g29Dcttm #LeetCode #CodingTips #Algorithms #ProblemSolving #LearningJourney

Kth Largest Element in an Array - LeetCode

Kth Largest Element in an Array - LeetCode

leetcode.com

NEHAARIKA MADDI

Software Developer Intern at BlockFrame, Inc.

1mo

Insightful!

To view or add a comment, sign in

Explore topics