Day 21 of My DSA Journey Solved “Perfect Number” using Java. Problem Summary A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). Example: 28 → divisors are 1, 2, 4, 7, 14 → sum = 28 Approach Initially, I checked all divisors up to n/2. This works but is inefficient for larger numbers. Then I optimized the solution by iterating only up to √n. For every divisor i, there exists a corresponding pair (n / i). So instead of checking all numbers, we only check till √n and add both divisors. Important points: • Start sum = 1 (since 1 is always a divisor) • Avoid counting the square root twice when i == n / i • Exclude the number itself from the sum Optimization Insight Divisors always occur in pairs. Using this property significantly reduces the number of iterations. Complexity Time Complexity: O(√n) Space Complexity: O(1) Key Learning A brute-force solution may work, but understanding mathematical properties helps in optimizing it. Thinking in terms of patterns and divisor behavior leads to better solutions. #DSA #LeetCode #Java #ProblemSolving #CodingJourney #100DaysOfCode
Optimizing Perfect Number Solution in Java with √n Approach
More Relevant Posts
-
🚀 Day 38 / 180 – DSA with Java 🚀 📘 Topic Covered: Strings & Two-Pointer Technique 🧩 Problem Solved: Is Subsequence Problem: Given two strings s and t, check whether s is a subsequence of t. Approach: Used two pointers to traverse both strings. Moved the pointer in s only when characters matched, and always moved the pointer in t, ensuring order is maintained. Key Learning: ✔️ Applying two-pointer technique on strings ✔️ Understanding subsequence vs substring ✔️ Writing clean and efficient O(n) solutions If you’re also preparing for DSA, let’s connect and learn together 🤝 #DSA #Java #180DaysOfCode #LearningInPublic #Strings #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 40 / 180 – DSA with Java 🚀 📘 Topic Covered: Binary Exponentiation (Fast Power) 🧩 Problem Solved: Pow(x, n) Problem: Implement a function to calculate x raised to the power n, handling both positive and negative values of n. Approach: Used Binary Exponentiation to reduce time complexity. Repeatedly squared the base and halved the exponent, multiplying the result only when needed. Also handled negative powers by taking the reciprocal. Key Learning: ✔️ Optimizing from O(n) to O(log n) ✔️ Understanding divide-and-conquer in exponentiation ✔️ Handling edge cases like negative powers If you’re also preparing for DSA, let’s connect and learn together 🤝 #DSA #Java #180DaysOfCode #LearningInPublic #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 37 / 180 – DSA with Java 🚀 📘 Topic Covered: Arrays & Two-Pointer Technique 🧩 Problem Solved: Squares of a Sorted Array Problem: Given a sorted array of integers (including negatives), return a new array of the squares of each number, also sorted in non-decreasing order. Approach: Used a two-pointer approach from both ends of the array. Compared squares of elements and filled the result array from the end to maintain sorted order efficiently. Key Learning: ✔️ Handling negative values in sorted arrays ✔️ Using two-pointer technique for optimal solutions ✔️ Avoiding extra sorting to achieve O(n) time complexity If you’re also preparing for DSA, let’s connect and learn together 🤝 #DSA #Java #180DaysOfCode #LearningInPublic #Arrays #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Mastering Java & DSA Through LeetCode Day 34 Today I solved a Medium-level Tree problem that strengthened my understanding of Prefix Sum + DFS — a powerful pattern used in many interview questions. LeetCode Problem: 437. Path Sum III Problem Summary: Given a binary tree and a target sum, we need to find the number of paths where the sum of node values equals the target. The path must go downward (parent → child), but it doesn’t need to start from the root. Key Insight: Instead of checking all possible paths (which is inefficient), we use: Prefix Sum HashMap to store frequencies DFS traversal This helps reduce time complexity from O(n²) → O(n) ⚡ Approach: Maintain a running sum while traversing the tree Check if (currentSum - target) exists in the map Use backtracking to maintain correct state What I Learned: How prefix sum works in trees (not just arrays!) Optimizing brute-force solutions Importance of hashmap in reducing complexity Consistency Update: Day 34 of solving DSA problems daily 💪 Small steps every day = big results over time #LeetCode #Java #DSA #CodingJourney #100DaysOfCode #SoftwareEngineering #PlacementPreparation #CodingInterview
To view or add a comment, sign in
-
-
DSA with Java.... Today I learnt about insertion sort algorithm. This algorithm starts at index 1, compares elements to the left and shifts elements to the right to insert a value. It has a complexity of 0(n^2) which is quite decent for small datasets but terrible for large datasets. It has less steps than bubble sort and it's best case scenario is 0(n) compared to selection sort, 0(n^2). I implemented insertion sort with Java to understand how the computer operates with such algorithm. Out of all these algorithms, which do you consider best to use in business applications? Cheers 🥂 #dsa #java #insertionsort #softwareengineering
To view or add a comment, sign in
-
-
🚀 Java DSA Progress Update I’ve solved 125/310 problems (~40%) as part of my structured Java DSA roadmap. ✔️ Strong in: Arrays, Strings, Linked Lists, Binary Search ⚡ Focusing next on: Heap, Graphs, Dynamic Programming, Sliding Window 📌 Approach: Pattern-based problem solving Time & space analysis Learning from mistakes Re-solving for retention 🎯 Goal: Become interview-ready in 30 days with strong problem-solving fundamentals. Consistent progress > perfection. #Java #DSA #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
🚀 Day 23 of my Java DSA Journey Today I worked on Linked List Cycle — LeetCode #141 🔹 Topic: Linked List 🔹 Pattern: Fast & Slow Pointers (Floyd’s Algorithm) 💡 Key Idea: Instead of using extra space like a HashSet, we use two pointers: • Slow pointer moves 1 step • Fast pointer moves 2 steps If a cycle exists, the fast pointer will eventually meet the slow pointer inside the loop. 🧠 Key Learning: The real trick is understanding why they meet — Fast pointer gains one step every move, so in a loop it will always catch up. 📊 Complexity: • Time: O(n) • Space: O(1) Sharing a visual breakdown of the algorithm 👇 Back to building consistency. 🔗 GitHub Solution: https://lnkd.in/gQRv2Qp3 #Java #DSA #LinkedList #LeetCode #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Mastering Java Through LeetCode 🧠 Day 21 of My DSA Journey 📌 Problem Solved: Q.1657 – Determine if Two Strings Are Close 💡 Problem Insight: At first glance, this problem looks like a simple string comparison… But it actually tests your understanding of patterns, hashing, and transformations. We are allowed to: ✔ Swap characters (change order) ✔ Transform characters (swap frequencies) 🧠 Key Learning: Two strings are "close" if: ✅ They have the same set of characters ✅ Their frequency distribution matches (order doesn’t matter) 👉 That means: Order is irrelevant Only character presence + frequency pattern matters 🔍 Approach I Used: 1️⃣ Checked if lengths are equal 2️⃣ Counted frequency using arrays 3️⃣ Verified both strings have same unique characters 4️⃣ Sorted frequency arrays and compared ⚡ Example: word1 = "cabbba" word2 = "abbccc" ✔ Same characters → {a, b, c} ✔ Frequencies match after sorting → [1,2,3] 👉 Result: true Tech Stack: Java Concepts Covered: Hashing | Arrays | Frequency Count Takeaway: This problem taught me how to: Think beyond direct comparison Focus on data patterns instead of structure Consistency + Practice = Growth #LeetCode #DSA #Java #CodingJourney #100DaysOfCode #ProblemSolving #Developers #SoftwareEngineer #Learning #Growth #CDAC #PlacementPreparation #Tech
To view or add a comment, sign in
-
-
🚀 Day 40 of My Java DSA Journey Today I worked on an interesting Binary Tree problem: 🌳 Flatten Binary Tree to Linked List 💡 Problem idea: Convert a binary tree into a linked list (in-place) following preorder traversal. 🔍 Approach I used: • Performed preorder traversal (Root → Left → Right) • Stored nodes in a list • Reconnected nodes such that: Left pointer → null Right pointer → next node in preorder ⚡ Key Learning: Understanding traversal order is crucial — preorder ensures the correct sequence for flattening. 🔥 What improved today: • Tree traversal skills • Pointer manipulation • Converting tree structure into linear form 🎯 Takeaway: Complex transformations become easier when broken into simple traversal steps. #Day40 #90DaysOfCoding #Java #DSA #BinaryTree #Recursion #Preorder #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 36 / 180 – DSA with Java 🚀 📘 Topic Covered: Binary Search (Peak Finding) 🧩 Problem Solved: Find Peak Element Problem: Given an array, find a peak element (an element greater than its neighbors) and return its index. Approach: Used Binary Search by comparing the middle element with its neighbors. Based on the increasing or decreasing slope, moved towards the side where a peak must exist, reducing the search space efficiently. Key Learning: ✔️ Applying binary search on unsorted arrays using patterns ✔️ Understanding how slope direction guides decisions ✔️ Solving peak problems in O(log n) time If you’re also preparing for DSA, let’s connect and learn together 🤝 #DSA #Java #180DaysOfCode #LearningInPublic #BinarySearch #ProblemSolving #Consistency
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