Day 21.... 💡 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
Java Prime Factorization Algorithm Optimization
More Relevant Posts
-
🚀 DSA in Java – Day 75 Today I solved the Remove Linked List Elements problem on LeetCode. 🔹 Problem Statement: Given the head of a linked list and an integer val, remove all nodes of the linked list that have Node.val == val, and return the new head. 🔹 My Approach: I used an iterative traversal technique with two pointers: temp → to traverse the linked list prev → to keep track of the previous node Steps: 1️⃣ Traverse the linked list using a loop. 2️⃣ If the current node value equals val, remove that node. 3️⃣ Handle the special case when the head node itself needs to be deleted. 4️⃣ Continue until the end of the list. 🔹 Key Learning: Handling edge cases in Linked Lists (especially when deleting the head node). Proper use of previous and current pointers. Strengthening my understanding of Linked List traversal. Consistency is the key to mastering Data Structures & Algorithms. One step closer to becoming a better problem solver! 💻 #DSA #Java #LeetCode #LinkedList #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 DSA in Java – Day 72 ✅ LeetCode Problem Solved: Subsets II 🔁 Concept Used: Backtracking + Recursion 🧠 Key Focus: Handling duplicate elements correctly Today I solved Subsets II, where the main challenge was generating all unique subsets from an array that may contain duplicates. 🔑 What I learned: Why sorting the array is important before recursion How to skip duplicates using this condition: if (idx > i && nums[idx] == nums[idx - 1]) continue; Proper backtracking steps (add → recurse → remove) How recursion builds subsets step by step Learning something new every day and getting better at problem-solving 💪 #DSA #Java #LeetCode #Backtracking #Recursion #ProblemSolving #CodingJourney #DailyLearning #Consistency
To view or add a comment, sign in
-
-
Day 27 of #100DaysOfLeetCode 💻✅ Solved #83. Remove Duplicates from Sorted List on LeetCode using Java. Approach: • Utilized the fact that the linked list is already sorted • Traversed the list using a single pointer • Compared current node value with next node value • If duplicate found, skipped the next node by updating links • Continued traversal until reaching the end of the list Performance: ✓ Runtime: 0 ms (Beats 100% submissions) ✓ Memory: 45.30 MB (Beats 85.70% submissions) Key Learning: ✓ Understood how sorting simplifies duplicate removal logic ✓ Strengthened pointer manipulation skills in linked lists ✓ Learned efficient in-place modification without extra space Learning one problem every single day 🚀 #Java #LeetCode #DSA #LinkedList #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 2/30 – Java DSA Challenge 🔎 Problem 10: 2016. Maximum Difference Between Increasing Elements (LeetCode – Easy) Solved another interesting array problem today 🔥 This problem is similar to the stock profit problem but with a slight twist. 🧠 Problem Statement Given an integer array nums, find the maximum difference: nums[j] - nums[i] Such that: ✔ 0 ≤ i < j < n ✔ nums[i] < nums[j] If no such pair exists → return -1. 💡 Example Input: [7,1,5,4] Best choice: Buy at 1 Sell at 5 Maximum Difference = 4 ✅ 👨💻 Approach (Greedy – Single Pass) ✔ Maintain the minimum value seen so far ✔ For each element: Calculate difference = current − minimum Update maximum difference ✔ Update minimum value if smaller element is found ✔ If no valid increasing pair → return -1 ⏱ Time Complexity: O(n) – Single traversal of the array 📦 Space Complexity: O(1) – No extra data structures used 📌 Key Learning: Maintaining a running minimum helps solve many array-based optimization problems efficiently. Day 2 progress continues strong 💪🔥 Small improvements daily → Big growth over time 🚀 #Day2 #30DaysOfCode #Java #DSA #LeetCode #ArrayProblems #GreedyAlgorithm #CodingJourne
To view or add a comment, sign in
-
-
🚀 DSA Journey – Day 4 (Java) Today I moved deeper into Functions in Java (Methods) and explored how Java handles memory and parameters internally. In Java, functions are called methods, and I understood the structure clearly: static returnType methodName(parameters) { // method body } 📘 What I learned today: 🔹 Pass by Value in Java Java is strictly pass by value. For primitive types (int, char, double, etc.) Only a copy of the value is passed. So swapping two numbers inside a method does NOT affect the original variables. For objects (arrays, strings, classes) A copy of the reference is passed. So modifying the object inside the method affects the original object. It is not pass by reference because Java does not expose raw pointers like C/C++. 🔹 Scoping & Shadowing Local variables exist only inside their block/method Class-level variables can be accessed across methods Shadowing happens when a local variable hides a class variable with the same name 🔹 Variable Arguments (Varargs) Using: datatype... v Allows passing multiple values Internally treated as an array Must always be the last parameter 🔹 Method Overloading Multiple methods with the same name but different parameters (Changing type or number of parameters) This improves readability and flexibility of code. Today’s session made me realize that understanding how methods behave internally is crucial before moving deeper into DSA. 📌 Next: Strengthening arrays and problem-solving. Step by step. Consistency over speed. #DSA #Java #LearningInPublic #Day4 #BTechStudent #AspiringSoftwareEngineer
To view or add a comment, sign in
-
🚀 Day 2/30 – Java DSA Challenge 🔎 Problem 8: 121. Best Time to Buy and Sell Stock (LeetCode – Easy) Solved another important array problem today 🔥 This is a classic interview question focused on greedy approach and array traversal. 🧠 Problem Statement You are given an array prices where prices[i] represents the stock price on the ith day. You need to: ✔ Buy one stock ✔ Sell it on a future day ✔ Maximize profit If no profit is possible → return 0. 💡 Example Input: [7,1,5,3,6,4] Buy at price = 1 Sell at price = 6 Profit = 5 ✅ 👨💻 Approach (Greedy + Single Pass) ✔ Track the lowest price seen so far ✔ For each day: Calculate profit = current price − lowest price Update maximum profit ✔ Update lowest price if a smaller value appears This ensures we always buy before we sell. ⏱ Time Complexity: O(n) – Single traversal of the array 📦 Space Complexity: O(1) – No extra data structures used 📌 Key Learning: Greedy algorithms work efficiently when we make the best decision at each step without revisiting previous states. Day 2 progressing strong 💪 Consistency builds confidence 🚀 #Day2 #30DaysOfCode #Java #DSA #LeetCode #ArrayProblems #GreedyAlgorithm #CodingJourney
To view or add a comment, sign in
-
-
🚀 DSA Day 5 / 100 Solved the Two Sum problem on LeetCode using Java. Logic : • Go through the array one number at a time • For each number, check what value is needed to reach the target • Store each number with its index in a HashMap • If the needed value is already stored, we’ve found the answer #DSA #100DaysOfCode #Java #LeetCode #BeginnerDSA #LearningStepByStep #Consistency
To view or add a comment, sign in
-
-
📝 Day 18/30 – LeetCode #153 (Find Minimum in Rotated Sorted Array) | Java I’ve been a bit inconsistent with daily posting lately due to focusing on deeper revision and understanding core patterns rather than just completing problems for the sake of streaks. Coming back with a classic binary search problem. The key insight here was realizing that even after rotation, at least one half of the array remains sorted. By comparing the middle element with the right boundary, we can decide which half contains the minimum and eliminate the other in each step. This approach reduces the search space logarithmically, resulting in an O(log n) solution. Progress isn’t always linear, but learning is still happening. #LeetCode #Java #DSA #BinarySearch #ProblemSolving #LearningInPublic #ConsistencyOverStreaks
To view or add a comment, sign in
-
-
🚀 Day 2/30 – Java DSA Challenge 🔎 Problem 12: 1299. Replace Elements with Greatest Element on Right Side (LeetCode – Easy) Solved another array traversal problem today 🔥 This problem focuses on understanding how to compare elements on the right side of an array. 🧠 Problem Statement Given an array arr, replace every element with the greatest element among the elements to its right. ✔ Replace the last element with -1 ✔ Return the modified array 💡 Example Input: [17,18,5,4,6,1] Output: [18,6,6,6,1,-1] Explanation: Each element is replaced by the maximum element present to its right. 👨💻 Approach (Brute Force) ✔ Traverse each index ✔ For every element, check all elements to its right ✔ Find the maximum among them ✔ Replace current element with that maximum ✔ For the last element → assign -1 Since constraints are manageable, this approach works. ⏱ Time Complexity: O(n²) – Nested loops 📦 Space Complexity: O(1) – In-place modification 📌 Key Learning: Understanding brute-force solutions is important before moving to optimized right-to-left traversal (which can reduce it to O(n)). Day 2 progressing consistently 💪🔥 Small steps every day lead to big improvements 🚀 #Day2 #30DaysOfCode #Java #DSA #LeetCode #ArrayProblems #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 1/30 – Java DSA Challenge 🔎 Problem 2: 3110. Score of a String (LeetCode – Easy) Solved my second problem for Day 1 of the 30 Days DSA challenge. 🧠 Problem Statement The score of a string is defined as the sum of the absolute difference between ASCII values of adjacent characters. We need to return the total score. 💡 Example Input: "hello" ASCII values: h = 104 e = 101 l = 108 l = 108 o = 111 Score calculation: |104 − 101| + |101 − 108| + |108 − 108| + |108 − 111| = 3 + 7 + 0 + 3 = 13 👨💻 Approach ✔ Start from index 1 ✔ Compare each character with its previous character ✔ Add the absolute difference to a running sum Since characters in Java are stored using ASCII values internally, we can directly subtract them. ⏱ Time Complexity: O(n) – We traverse the string once. 📦 Space Complexity: O(1) – Only a single variable is used. 📌 Key Learning: Understanding how characters are stored internally (ASCII values) helps simplify string problems. Excited to continue this journey 🚀 #Day1 #30DaysOfCode #Java #DSA #LeetCode #ProblemSolving #CodingJourney
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