Day 90 of #100DaysOfCode Solved Squares of a Sorted Array in Java 🔠 Approach The task was to take an array of integers sorted in non-decreasing order, square each number, and then return the result array also sorted in non-decreasing order. Brute-Force Method The solution implemented here is a straightforward two-step brute-force approach: Squaring: I iterated through the input array nums and replaced each element with its square (i.e., nums[i] * nums[i]). This handles both positive and negative numbers correctly. Sorting: After squaring all elements, I used Java's built-in Arrays.sort(nums) method to sort the entire array. While correct, this approach has a time complexity dominated by the sorting step, which is O(NlogN), where N is the number of elements. The runtime of 10 ms shows that a more efficient, two-pointer approach (which can solve this in O(N) time) is generally preferred for optimal performance. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Array #Sorting #ProblemSolving
Solved Squares of a Sorted Array in Java with brute-force method
More Relevant Posts
-
🚀 Day 18/100 of #100DaysOfCode ✅ Solved “Isomorphic Strings” (LeetCode #205) using Java! 🧩 Problem: Given two strings s and t, determine if they are isomorphic. 👉 Two strings are isomorphic if characters in one string can be replaced to get the other — while keeping order and unique mapping intact. 🧠 Approach: Used a HashMap to store the mapping of characters from s → t. Checked if each character in s has a consistent mapping in t. Also ensured that no two characters in s map to the same character in t. ✨ Example: s = "egg", t = "add" → true (e→a, g→d) s = "foo", t = "bar" → false 📈 Complexity: Time: O(n) Space: O(n) 💡 Key Learnings: Understood how to maintain one-to-one character mapping using a HashMap. Reinforced logic building for pattern matching between strings. 💻 Tech Used: Java | HashMap | Problem Solving #LeetCode #100DaysOfCode #Java #ProblemSolving #DSA #CodingJourney #LearnByDoing #DevCommunity
To view or add a comment, sign in
-
-
𝐓𝐨𝐩 𝐉𝐚𝐯𝐚 𝐕𝐢𝐫𝐭𝐮𝐚𝐥 𝐓𝐡𝐫𝐞𝐚𝐝𝐬 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 (2025) If you're preparing for Java interviews or brushing up on Project Loom, here are the MOST asked Virtual Threads questions you should master: ✅ 𝐓𝐨𝐩 𝐕𝐢𝐫𝐭𝐮𝐚𝐥 𝐓𝐡𝐫𝐞𝐚𝐝𝐬 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 1. What are Virtual Threads in Java? 2. Why were Virtual Threads introduced in Java? 3. How do Virtual Threads differ from Platform Threads? 4. What problem do Virtual Threads solve? 5. In which Java version did Virtual Threads become a stable feature? 6. Are Virtual Threads suitable for CPU-bound or I/O-bound tasks? 7. How do Virtual Threads improve scalability? 8. How does the JVM schedule Virtual Threads? 9. What happens when a Virtual Thread performs a blocking I/O operation? 10. What is the role of carrier threads in Virtual Threads? 11. How do Virtual Threads reduce memory usage? 12. Can we create millions of Virtual Threads? Why? 13. How do you create a Virtual Thread in Java? 14. What is 𝐓𝐡𝐫𝐞𝐚𝐝.𝐨𝐟𝐕𝐢𝐫𝐭𝐮𝐚𝐥() used for? 15. What is 𝐄𝐱𝐞𝐜𝐮𝐭𝐨𝐫𝐬.𝐧𝐞𝐰𝐕𝐢𝐫𝐭𝐮𝐚𝐥𝐓𝐡𝐫𝐞𝐚𝐝𝐏𝐞𝐫𝐓𝐚𝐬𝐤𝐄𝐱𝐞𝐜𝐮𝐭𝐨𝐫()? 16. Do Virtual Threads eliminate the need for thread pools? 17. What is structured concurrency in Project Loom? 18. How do Virtual Threads integrate with Executors? 19. What are the limitations of Virtual Threads? 20. What happens if a Virtual Thread enters a long synchronized block? 21. Are Virtual Threads preemptive or cooperative? 22. Do existing Java libraries need to change to support Virtual Threads? 23. How do Virtual Threads help in designing scalable servers? 24. Can Virtual Threads run on multiple CPU cores? 25. How do Virtual Threads handle blocking JDBC calls? 26. What challenges might occur when using Virtual Threads with legacy code? 27. How do Virtual Threads compare to Kotlin coroutines or Go goroutines? 28. What is pinning in the context of Virtual Threads? 29. How do Virtual Threads interact with locks and synchronization? 30. Can Virtual Threads be debugged like normal threads? If you found this helpful, follow Madhu K. for more Java, Spring Boot, and backend interview content #Java #SpringBoot #VirtualThreads #ProjectLoom #BackendDevelopment #InterviewPrep #CodingMadeSimple #MadhuKumari #BiharGeeks
To view or add a comment, sign in
-
-
Day 28/100 – #100DaysOfCode 🚀 | #Java #LeetCode #DynamicProgramming ✅ Problem Solved: Scramble String 🔀 🧩 Problem Summary: Given two strings s1 and s2, determine whether one is a scrambled version of the other. A string is scrambled by recursively dividing it into two non-empty substrings and swapping them. 💡 Approach Used: Implemented Recursion + Memoization using a HashMap for overlapping subproblems. Used character frequency checks to prune unnecessary recursion calls. Used Java’s BiFunction with inline helper logic for recursion. ⚙️ Time Complexity: O(n⁴) (due to substring operations and recursion) 📦 Space Complexity: O(n²) ✨ Takeaway: Even complex recursive problems can be optimized efficiently with Memoization and early pruning. 🚀 #Java #LeetCode #DynamicProgramming #Recursion #ProblemSolving #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 80 of #100DaysOfCode Solved Frequency Sort in Java 🔠 Approach Today's problem was to sort an array based on the frequency of its numbers. My initial solution was accepted, but it exposed a major efficiency gap! My strategy involved: Sorting the array first. Using nested loops to count the frequency of each number and store it in a HashMap. (Implied) Using the counts to perform the final custom sort. The core issue was the counting method: running a full loop inside another full loop to get the frequency. This quadratic $O(N^2)$ counting completely tanked the performance. ✅ Runtime: 29 ms (Beats 12.02%) ✅ Memory: 45.26 MB (Beats 5.86%) Another great lesson in algorithmic complexity! The difference between $O(N^2)$ and the optimal $O(N \log N)$ for this problem is massive. Time to refactor and implement the fast, single-pass $O(N)$ frequency count using getOrDefault! 💪 #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #HashMap #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
Day 85 of #100DaysOfCode Solved Range Sum Query - Immutable (NumArray) in Java ➕ Approach Today's problem was a classic that demonstrates the power of pre-computation: finding the sum of a range in an array many times. The optimal solution is the Prefix Sum technique. Pre-computation: In the constructor, I built a prefixSum array where prefixSum[i] holds the sum of all elements from index 0 up to index $i-1$ in the original array. This takes $O(N)$ time. $O(1)$ Query: The magic happens in the sumRange(left, right) method. The sum of any range $[left, right]$ is found instantly by calculating prefixSum[right + 1] - prefixSum[left]. The cost of a single $O(N)$ setup is outweighed by the ability to perform every subsequent query in $O(1)$ time! #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #PrefixSum #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
Today I worked on the problem "Convert Sorted List to Binary Search Tree" in Java — a perfect blend of Linked List and Tree logic! 🌳 🔍 Key Concepts Covered: Finding the middle element of a Linked List using the slow & fast pointer approach Recursively constructing a height-balanced BST Strengthening understanding of Divide and Conquer strategies This problem really sharpened my thinking around recursive structures and pointer management — small yet powerful steps toward writing more efficient and elegant Java code. 💻✨ 💬 What’s your favorite data structure to work with — Linked Lists or Trees? #LeetCode #Java #DataStructures #Algorithms #CodingJourney #LearningEveryday #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Extract a character from a string To get a character from a string in Java, you can use the charAt() method of the String class. This method takes an index as an argument and returns the character at that position (0-based index). Here's a simple Java program to demonstrate this: Code : public class GetCharacterFromString { public static void main(String[] args) { String str = "Hello, World!"; int index = 7; // Index of the character to retrieve (0-based) // Get the character at the specified index char ch = str.charAt(index); // Print the character System.out.println("Character at index " + index + " is: " + ch); } } Can anyone guess the output ?. #JavaProgramming #JavaCode #Coding #ProgrammingTips #LearnJava #JavaDevelopment #CodeSnippet #SoftwareDevelopment #TechTips #100DaysOfCode
To view or add a comment, sign in
-
Day 91 of #100DaysOfCode Solved Base 7 in Java 🔢 Approach The challenge was to convert a given integer (num) to its base 7 string representation. Conversion Method The core of the solution lies in the standard algorithm for base conversion: repeated division and remainder collection. Handle Zero and Negatives: If the input num is 0, the result is immediately "0". I determined if the number is negative and stored this in a boolean flag, then proceeded with the absolute value of the number (num = Math.abs(num)) for the conversion logic. Conversion Loop: I used a while loop that continues as long as num > 0. In each iteration: The remainder when num is divided by 7 (num % 7) gives the next digit in base 7. This digit is appended to a StringBuilder. num is then updated by integer division by 7 (num /= 7). Final Result: Since the remainders are collected in reverse order, I called sb.reverse(). If the original number was negative, I prepended a hyphen (-) to the reversed string. Finally, I returned the result as a string. This simple and efficient implementation had a very fast runtime, beating 77.39% of submissions. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #BaseConversion #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 7 — The Multithreading Mystery That Breaks Developer Logic 🧩 Every Java developer says: “I know how threads work.” But when two threads share the same object… even pros get confused about what actually happens 👇 class Printer implements Runnable { int count = 0; @Override public void run() { for (int i = 0; i < 3; i++) { System.out.println(Thread.currentThread().getName() + " → " + count++); } } public static void main(String[] args) { Printer printer = new Printer(); Thread t1 = new Thread(printer, "Thread-A"); Thread t2 = new Thread(printer, "Thread-B"); t1.start(); t2.start(); } } 💭 Question: What could be the possible output? 1️⃣ Each thread prints 0 1 2 independently 2️⃣ The count value increases continuously (shared between threads) 3️⃣ Compile-time error 4️⃣ Unpredictable output 💬 Drop your guess in the comments 👇 Most devs think they know the answer — until they realize what “shared object” actually means in Java threading 😵💫 Can you explain why it happens? 🧠 #Java #Multithreading #Concurrency #CodingChallenge #JavaDeveloper #InterviewQuestion #Day7Challenges #SpringBoot
To view or add a comment, sign in
-
You should use 𝚂𝚝𝚛𝚒𝚗𝚐 𝚊 = “𝙷𝚎𝚕𝚕𝚘”, and not 𝚂𝚝𝚛𝚒𝚗𝚐 𝚊 = 𝚗𝚎𝚠 𝚂𝚝𝚛𝚒𝚗𝚐(“𝙷𝚎𝚕𝚕𝚘”) in Java. Here’s why. 👇 If you write Java, you deal with String objects all day long. But do you know the hidden memory optimization that saves your application from running out of space? It's the 𝐒𝐭𝐫𝐢𝐧𝐠 𝐂𝐨𝐧𝐬𝐭𝐚𝐧𝐭 𝐏𝐨𝐨𝐥. 𝐒𝐭𝐫𝐢𝐧𝐠 𝐩𝐨𝐨𝐥, 𝐨𝐫 𝐭𝐡𝐞 𝐒𝐭𝐫𝐢𝐧𝐠 𝐂𝐨𝐧𝐬𝐭𝐚𝐧𝐭 𝐏𝐨𝐨𝐥, is a special storage area in the Java 𝐇𝐞𝐚𝐩 𝐦𝐞𝐦𝐨𝐫𝐲 where string literals are stored. The core idea is to avoid creating multiple duplicate String objects in memory when they have the same content. The String Pool is the 𝐤𝐞𝐲 to efficient, scalable code and a crucial JVM feature. When you declare a String literal (𝐒𝐭𝐫𝐢𝐧𝐠 𝐚 = "𝐇𝐞𝐥𝐥𝐨"), the JVM first checks the Pool. If the string already exists, the new variable will simply point to that existing object in the pool, avoiding new memory allocation. This optimization only works because Java String objects are 𝐢𝐦𝐦𝐮𝐭𝐚𝐛𝐥𝐞. 𝐖𝐡𝐞𝐧 𝐲𝐨𝐮 𝐰𝐫𝐢𝐭𝐞: 𝚂𝚝𝚛𝚒𝚗𝚐 𝚜𝟷 = “𝙷𝚎𝚕𝚕𝚘”; 𝚂𝚝𝚛𝚒𝚗𝚐 𝚜𝟸 = “𝙷𝚎𝚕𝚕𝚘”; Both strings point to the same object in memory. So if you do: 𝚂𝚢𝚜𝚝𝚎𝚖.𝚘𝚞𝚝.𝚙𝚛𝚒𝚗𝚝𝚕𝚗(𝚜𝟷 == 𝚜𝟸); // 𝚝𝚛𝚞𝚎 𝐖𝐡𝐞𝐧 𝐲𝐨𝐮 𝐰𝐫𝐢𝐭𝐞: 𝚂𝚝𝚛𝚒𝚗𝚐 𝚜𝟹 = 𝚗𝚎𝚠 𝚂𝚝𝚛𝚒𝚗𝚐(“𝙷𝚎𝚕𝚕𝚘”); It always 𝐟𝐨𝐫𝐜𝐞𝐬 the creation of a new object in the main Heap, outside the String Pool. So if you do: 𝚂𝚢𝚜𝚝𝚎𝚖.𝚘𝚞𝚝.𝚙𝚛𝚒𝚗𝚝𝚕𝚗(𝚜𝟷 == 𝚜𝟹); // 𝚏𝚊𝚕𝚜𝚎 If you absolutely must use the new operator you can call .𝚒𝚗𝚝𝚎𝚛𝚗() on your string to explicitly move or fetch its reference from the Pool, forcing the JVM to 𝐫𝐞𝐮𝐬𝐞 𝐨𝐛𝐣𝐞𝐜𝐭𝐬. 𝚂𝚝𝚛𝚒𝚗𝚐 𝚜𝟹 = 𝚗𝚎𝚠 𝚂𝚝𝚛𝚒𝚗𝚐(“𝙷𝚎𝚕𝚕𝚘”).𝚒𝚗𝚝𝚎𝚛𝚗(); 𝚂𝚢𝚜𝚝𝚎𝚖.𝚘𝚞𝚝.𝚙𝚛𝚒𝚗𝚝𝚕𝚗(𝚜𝟷 == 𝚜𝟹); // 𝚝𝚛𝚞𝚎 What’s your favorite JVM optimization trick? Share in the comments! 👇 #Java #JVM #SoftwareEngineering #TechCareer #ProgrammingTips #MemoryManagement #PerformanceTuning #Coding
To view or add a comment, sign in
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development