Day 3 - Extending Frequency Logic to Handle Negative Integers Hello Connections, Continuing my DSA with Java journey, today I took yesterday’s concept of frequency mapping and applied it to a more flexible scenario - counting integer frequencies even when the numbers include negatives. Problem I Solved: Count the frequency of integers within a given range including negative values. Example: Range → max = 5, min = -5 Numbers → 1, -1, -3, -1, 4, 2 Output should show frequency for every number from -5 to +5 My Approach: Yesterday, I used ASCII values to map letters into array indexes. Today, I applied the exact same idea to integers. Step 1: Finding the Range: To store frequencies from min to max, I need an array that has enough slots for every integer in between. So the range is calculated using: range = max - min + 1; Step 2: Mapping Any Number to an Array Index: Since Java array indexes start from 0, we shift each number relative to min, index = number - min; This creates a direct-access table where every integer has its own slot. Understanding how to calculate the range and map numbers to indexes made this problem simple and elegant. It’s amazing how the same idea from Day 2 can scale to more complex scenarios and I think that’s the real beauty of DSA. Excited for day 4! #dsawithjava #dsa #java
Counting Integer Frequencies with Negative Values in Java
More Relevant Posts
-
🚀 #100DaysOfDSA — Day 27/100 Topic: Linear Search in Java 💻 💡 What I Did Today: Today I learned and implemented the Linear Search algorithm — one of the simplest and most fundamental searching techniques in DSA. It’s all about checking each element in an array one by one until the key is found (or not found). 🧠 Logic Used: Traverse the entire array. Compare each element with the key. If it matches → return the index. If no match is found → return -1. 📊 Output Example: Array → {2, 4, 6, 8, 10, 12, 50, 16} Key → 50 ✅ Output → Key is at index: 6 ✨ Takeaway: Linear Search may not be the most efficient algorithm, but it’s the foundation of all searching logic. Understanding it helps in learning more advanced techniques like Binary Search later 🔍 #100DaysOfCode #Day27 #Java #DSA #LinearSearch #ProblemSolving #CodingJourney #LearnInPublic #CodeNewbie #DeveloperLife
To view or add a comment, sign in
-
-
🚀 #100DaysOfDSA — Day 26/100 Topic: Arrays as Function Arguments in Java 💻 💡 What I Did Today: Today, I learned how arrays can be passed as arguments to functions in Java — and how changes made inside a function affect the original array. This concept is super important for understanding how references work in Java! ⚙️ 🧠 Logic Used: Defined an update() function that increases every array element by 1. Passed the marks[] array into the function. Observed that updates inside the function reflected in the original array — proving arrays are passed by reference (not by value). 📊 Output: Before update → 66 68 72 68 74 After update → 67 69 73 69 75 ✨ Takeaway: Today’s practice gave me clarity on how data moves between methods in Java. Understanding references helps avoid unexpected behavior and write cleaner, safer code 💡 #100DaysOfCode #Day26 #Java #DSA #Arrays #ProblemSolving #CodingJourney #LearnInPublic #CodeNewbie #DeveloperLife
To view or add a comment, sign in
-
-
💡 Logic Building with DSA using Java Today I solved the Merge Intervals problem ➡️ You’re given a list of ranges like [start, end], and some of them overlap. The goal is to combine all overlapping intervals so the final list is clean and non-overlapping. Brute Force: Compare every interval with every other interval and merge the ones that overlap. Time complexity: O(n²) — works, but inefficient. Optimal Approach: 1️⃣ Sort all intervals by their start time 2️⃣ Go through them one by one 3️⃣ If the current interval overlaps with the last one in the result → update the end and merge 4️⃣ If it doesn’t overlap → add it as a new interval Question link: https://lnkd.in/gFYtG9Xz 👏 Special thanks to Anchal Sharma and Ikshit for thir support and guidance. #DSA #CodingJourney #LearningEveryday #Java #BackendDeveloper #PersonalGuidanceProgramByAnchalSharma #Pgp
To view or add a comment, sign in
-
-
Today’s session focused on understanding Time Complexity (TC) and Space Complexity (SC) — essential concepts for writing optimized code. To apply these concepts, I practiced several Java programs and analyzed their efficiency: ✔ Programs Practiced • Reverse an array TC: O(n) (single loop) SC: O(1) (in-place) • Find the maximum of two numbers TC: O(1) SC: O(1) • Linear Search TC: O(n) SC: O(1) • Binary Search TC: O(log n) (halve the search space) SC: O(1) Understanding TC and SC helped me evaluate how efficient my solutions are and how to choose the best approach depending on the problem. Karthikeyan A Learning to think not just about correctness, but performance, is a major step in problem-solving and DSA. Excited to keep improving every day! 🚀 #Java #TimeComplexity #SpaceComplexity #DSA #ProblemSolving #CodingJourney #LearningInPublic #SparkTribe #SoftwareDevelopment #ContinuousLearning
To view or add a comment, sign in
-
-
💡 Today I learned about Prime Factorization in Java! Prime Factorization means breaking a number into its prime factors — the building blocks of the number. 🧩 Basic Approach: int i = 2; while (n > 1) { while (n % i == 0) { System.out.println(i); n = n / i; } i++; } ⏱️ Time Complexity: O(n) ⚙️ Optimized Approach: int i = 2; while (i * i <= n) { while (n % i == 0) { System.out.println(i); n = n / i; } i++; } if (n > 1) System.out.println(n); ⏱️ Time Complexity: O(√n) ✅ Key Takeaway: Checking factors only up to √n makes the algorithm much faster — no need to go till n. Small change, big improvement in performance! #Java #DSA #LearningJourney #Coding #Algorithms #Optimization
To view or add a comment, sign in
-
🚀Day 96/100 #100DaysOfLeetCode 🧩Problem: Reverse Linked List II✅ 💻Language: Java 💡 Approach: 1️⃣ First, use a dummy node to handle edge cases where reversal starts at the head. 2️⃣ Traverse to the node just before the left position — call it prev. 3️⃣ Reverse the sublist between left and right using standard pointer manipulation. 4️⃣ Reconnect the reversed portion back into the original list. 🔑Key Takeaways: 🔹Dummy nodes simplify linked list edge cases. 🔹In-place reversal reduces memory overhead. 🔹Careful pointer tracking ensures list integrity. ⚙️Performance: ⏱️Runtime: 0 ms(beats 100.00%) 💾Memory: 41.29 MB(beats 70.37%) #100DaysOfLeetCode #Java #LinkedList #CodingJourney #ProblemSolving #DSA #LeetCode #CodingChallenge
To view or add a comment, sign in
-
-
Hii Connections 👋 I recently solved an interesting problem in Java: Problem: Count the number of pairs in an array that have a specific difference d. My Approach: I used the two-pointer technique for an optimized solution: Start with two pointers, left and right. Compare the difference between the elements at these pointers. If the difference < d, move right forward. If the difference == d, increment the count and move both pointers. If the difference > d, move left forward. This avoids unnecessary comparisons and runs in O(n) for a sorted array, unlike the brute-force O(n²) approach. What I Learned: ✅ Two-pointer technique is super powerful for array problems. ✅ Thinking in terms of patterns rather than brute force makes code efficient. ✅ Small optimizations can drastically improve performance. Sharing my journey and progress on DSA practice! #Java #DSA #Coding #ProblemSolving #TwoPointers #LeetCode #LearningEveryday #CodeNewbie
To view or add a comment, sign in
-
-
🔥 #100DaysOfDSA — Day 30/100 Topic: Reverse an Array in Java 🔁 💡 What I Did Today: Today, I explored how to reverse an array manually using the two-pointer approach. Instead of relying on built-in methods, I learned how to swap elements from both ends until the array is completely reversed. 🧠 Logic Used: Initialize two pointers: start = 0 (beginning of array) end = numbers.length - 1 (end of array) While start < end: Swap numbers[start] and numbers[end] Move pointers inward → start++ and end-- 📊 Example: Input → {2, 4, 6, 8, 10} Output → {10, 8, 6, 4, 2} ⚙️ Time Complexity: O(n) — each element is swapped once. O(1) space — done in-place without using extra arrays. ✨ Takeaway: Learning to reverse an array manually helps strengthen the concept of pointers, loops, and in-place operations. Sometimes the simplest logic gives the biggest "aha!" moment 💡 #100DaysOfCode #Day30 #Java #DSA #Arrays #ProblemSolving #CodingJourney #LearnInPublic #DeveloperLife #CodeNewbie #LogicBuilding
To view or add a comment, sign in
-
-
🚀Day 99/100 #100DaysOfLeetCode 🔍Problem: Sum of Two Integers✅ 💻Language: Java 💡Approach: Instead of using the ‘+’ or ‘–’ operators, this problem leverages bit manipulation to perform addition. 🔸Use XOR (^) to calculate the sum without carry. 🔸Use AND (&) followed by a left shift (<< 1) to calculate the carry. 🔸Repeat until no carry remains. 📚Key Takeaways: 🔹Reinforced understanding of bitwise operations. 🔹Learned how addition can be simulated using logical operations. 🔹Improved understanding of low-level arithmetic computation. ⚡Performance: ⏱️Runtime: 0 ms (Beats 100.00%) 💾Memory: 40.88 MB (Beats 10.05%) #100DaysOfLeetCode #Java #BitManipulation #CodingChallenge #ProblemSolving #DSA #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🧠 Daily LeetCode Grind — Java Edition Today’s challenge: ✅ Palindrome Number (#9 - Easy) 📌 Goal: Check whether an integer reads the same backward as forward without converting it to a string. 📌 Approach: 🔹 Handle negatives and numbers ending in 0 (except 0 itself). 🔹 Reverse only half of the digits using modulo and division. 🔹 Compare the original and reversed halves for equality. 🧩 Test Cases: Input: 121 → Output: true Input: -121 → Output: false Input: 10 → Output: false 💡 Key Takeaways: 🔹 Strengthened arithmetic-based problem-solving (no string ops). 🔹 Learned efficient O(1) space reversal logic. 🔹 Improved understanding of numeric pattern recognition. 💻 Language: Java 🧠 Complexity: O(log₁₀ n) — reverse digits only once. #LeetCode #Java #CodingPractice #ProblemSolving #DSA #PalindromeNumber #DeveloperLife #AcceptedSolution #CybernautEdTech
To view or add a comment, sign in
-
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