🚀 Just solved LeetCode #2442 — Count Number of Distinct Integers After Reverse Operations 📘 Problem: You are given an array of positive integers. For each integer, reverse its digits and add it to the array. Finally, return the total count of distinct integers in the resulting array. Example: Input → [1,13,10,12,31] Output → 6 Explanation → After reversing and adding, the array becomes [1,13,10,12,31,1,31,1,21,13]. The distinct numbers are 1, 10, 12, 13, 21, and 31. 🧠 My Approach: I used a HashSet to automatically handle uniqueness. 1️⃣ Loop through each element in the array and add it to the set. 2️⃣ Reverse each number and add the reversed version as well. 3️⃣ Return the final size of the HashSet, which represents the count of distinct integers. 💡 What I Learned: ✅ How HashSet simplifies handling of unique elements ✅ Efficiently reversing numbers using arithmetic instead of strings ✅ Reinforced understanding of loops and data structures working together 💻 Language Used: Java 🔍 Concept: HashSet | Array Manipulation | Number Reversal #LeetCode #Java #DSA #HashSet #CodingUpdate #LearningByDoing #Programming
Solved LeetCode #2442 with HashSet and Number Reversal in Java
More Relevant Posts
-
DSA Practice – Day 69 🚀 Problem: Combination Sum (LeetCode 39) ✨ Optimal Approach (Backtracking) We build combinations step-by-step and backtrack whenever the sum exceeds the target. Steps: 1. Use recursion starting from index ind. 2. Pick the element if it doesn’t exceed the target. 3. Recurse with the same index (because repetition is allowed). 4. Backtrack and move to the next index. 5. When target == 0, we add the current list to the answer. ⭐ Key Idea Backtracking helps us explore only valid paths and cut off unnecessary branches early — making it much more efficient. #Day69 #Backtracking #DSA #Java #LeetCode #Placements
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟐𝟏 𝐨𝐟 #50DaysOfDSA 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐋𝐢𝐧𝐤 : https://lnkd.in/gzNm43Xx 𝐒𝐨𝐥𝐮𝐭𝐢𝐨𝐧 𝐋𝐢𝐧𝐤 : https://lnkd.in/gAXsPq4Q Today’s challenge was all about counting substrings efficiently — without generating them one by one. The problem? Given a binary string, find the number of substrings that contain only ‘1’s, modulo 1e9+7. 𝐊𝐞𝐲 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: Instead of checking all substrings, simply count consecutive 1s. For every continuous segment of k ones, the number of valid substrings is: 𝐤 * (𝐤 + 𝟏) / 𝟐 But we optimize further by counting while we iterate. 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: -> Maintain a running count of consecutive 1s -> Add that to the total whenever a 1 appears -> Reset when a 0 appears -> Handle large numbers using modulo 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝟏) #leetcode #dsa #java #coding #problemsolving #programming #learningeveryday #devcommunity #softwareengineering #100daysofcode #girlswhocode #problem solving
To view or add a comment, sign in
-
-
🚀 𝗗𝗮𝘆 𝟮𝟰𝟱 𝗼𝗳 𝟮𝟰𝟳 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 🔹 Problem: 📘 91. Decode Ways 🧩 Approach: We are given a string of digits, and we must determine how many ways it can be decoded to letters (using mapping "1" -> 'A' to "26" -> 'Z'). Dynamic Programming (DP) is used to count possible decodings by analyzing single and two-digit combinations. 📘 Steps to Solve: If the string starts with '0', return 0 (invalid code). Use a DP array where dp[i] represents the number of ways to decode up to index i. For each index i, check: The single digit (s[i-1]) is valid (1–9). The two-digit substring (s[i-2..i-1]) is valid (10–26). Add valid possibilities to build dp[i]. The final answer is dp[n]. ✅ Time Complexity: O(n) ✅ Space Complexity: O(n) ✨ What I Learned Today: Dynamic Programming simplifies complex recursive problems by storing partial solutions — a beautiful example of optimization in action! 💡 Let's do this! #LeetCode #Day247 #Java #DynamicProgramming #MediumProblem #247Challenge #CodingJourney
To view or add a comment, sign in
-
-
🚀 Just solved LeetCode #2744 — Find Maximum Number of String Pairs 📘 Problem: You’re given an array of distinct strings. We need to count how many pairs (i, j) can be formed such that the string at index i is the reverse of the string at index j. Each string can only belong to one pair. Example: Input → ["cd", "ac", "dc", "ca", "zz"] Output → 2 Explanation → ✅ "cd" ↔ "dc" ✅ "ac" ↔ "ca" 🧠 My Approach: I used a HashSet to efficiently track reversed strings. 1️⃣ For each word, I created its reversed version. 2️⃣ If the reversed version already exists in the set, I counted a valid pair and removed it from the set. 3️⃣ Otherwise, I added the word to the set for future checks. 💡 What I Learned: ✅ How HashSet helps reduce time complexity for lookup and removal ✅ How to reverse strings efficiently using StringBuilder ✅ Strengthened logic-building for string manipulation and pairing problems 💻 Language Used: Java 🔍 Concept: HashSet | String Manipulation | Two-Pointer Logic #LeetCode #Java #DSA #String #HashSet #CodingUpdate #LearningByDoing #Programming
To view or add a comment, sign in
-
-
🚀 Day 73 of #100DaysOfCode Today I solved LeetCode 3346 – Maximum Frequency of an Element After Performing Operations I 🧩 This problem was all about logical range manipulation — figuring out how many elements could be converted into the same number when each element can be modified by ±k, but with a limited number of allowed operations. At first, it felt similar to the “most frequent element” sliding window problem, but the twist here was controlling how many elements can be changed, not just the total cost. After a few failed attempts (and a battle with edge cases 😅), I finally implemented a correct solution using the difference array + prefix sum approach. 💡 Key Takeaways: Think in terms of ranges, not just differences. Prefix-sum (difference map) logic is incredibly powerful for interval counting. Always recheck constraints — “number of operations” vs “total cost” makes a huge difference! Here’s a snippet from my final Java solution: int achievable = Math.min(running, same + numOperations); ans = Math.max(ans, achievable); It’s small details like these that make problem-solving such a rewarding process 💪 #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🎯 Day 8 of #365DaysofDSA Today I solved “Convert Sorted Array to Binary Search Tree” (LeetCode #108) 🌲 💡 Concepts used: • Recursion • Divide and Conquer • Binary Search Tree Construction ✨ Approach Summary: The goal is to convert a sorted array into a height-balanced BST. To maintain balance, I picked the middle element of the array as the root — ensuring roughly equal elements on both sides. Then recursively repeated the process for left and right halves of the array. For each recursive call: 1️⃣ Find the middle index of the current subarray. 2️⃣ Create a node with that middle element. 3️⃣ Recursively build left and right subtrees using the respective halves. 🚀 Key takeaway: The divide and conquer strategy naturally fits problems that require balance and recursion — just like in this BST construction. Breaking a big problem into symmetric subproblems leads to elegant and efficient solutions. 🌿 #Day8 #DSA #LeetCode #Java #BinarySearchTree #Recursion #ProblemSolving #CodingJourney #LearnByDoing #Programming
To view or add a comment, sign in
-
-
#Day-43) Problem Solving with BFS | LeetCode 1625 Just tackled an interesting string manipulation challenge: "Lexicographically Smallest String After Applying Operations" 🔢 Given a digit string, the task is to apply two operations—adding to odd indices and rotating right—to find the smallest possible string. I approached it using Breadth-First Search (BFS) to explore all valid transformations while avoiding cycles with a hash set. 🔍 My approach: Used a queue to explore all reachable states. Maintained a HashSet to avoid revisiting strings. Compared strings lexicographically to track the optimal one. 💡 This problem was a great exercise in: State-space exploration String manipulation Efficient pruning with hashing Here’s a peek at the Java implementation I used 👇 Would love to hear how others approached it—DFS, greedy, or something more exotic? #LeetCode #Cplusplus #ProblemSolving #DSA #CodingChallenge #BFS #TechJourney #LinkedInCoding #PranshuCodes
To view or add a comment, sign in
-
-
🚀 Day 12 of #50DaysOfLeetCodeChallenge 🚀 🧩 Problem 1260: Shift 2D Grid:: You are given a 2D grid of size m x n and an integer k. The task is to shift the grid k times. 💡 Approach: Flattened the 2D grid into a 1D array to simplify shifting. Calculated the effective shifts using k % totalElements to avoid redundant rotations. Created a new shifted array by mapping each element from the original flattened array to its new position. Converted the 1D shifted array back into a 2D array row by row. ✅ Efficient: O(m * n) ✅ Handles all edge cases, including k = 0 or k > total elements ✅ Clear and easy-to-follow logic 📚 Key Takeaways: Strengthened understanding of 2D to 1D mapping and vice versa. Learned how modular arithmetic helps in rotation problems. Improved skills in array transformations and index calculations. Huge thanks to Trainer Shishir chaurasiya and PrepInsta for constant guidance and motivation! 🙏 #50DaysOfLeetCodeChallenge #LeetCode #Java #ProblemSolving #CodingJourney #TechCommunity #KeepLearning #DSA
To view or add a comment, sign in
-
-
🚀 Day 414 of #500DaysOfCode 🔢 LeetCode 2536: Increment Submatrices by One (Medium) Today I worked on an interesting matrix manipulation problem that teaches an important optimization idea — 2D Difference Arrays. 🧠 Problem Summary We are given an n x n matrix initialized with zeros and a list of queries. Each query represents a submatrix, and we need to increment every element inside that submatrix by 1. A brute-force solution would update each cell for every query, which becomes inefficient when the matrix and number of queries grow. ⚡ Key Insight Instead of updating all cells directly, we use a 2D prefix / difference matrix technique: Update only the corners of each submatrix Convert the difference matrix into the final matrix using prefix sums Achieve O(n² + q) performance instead of O(q · n²) A neat trick that’s extremely useful for range update problems! ✅ Takeaways Difference arrays are not just for 1D — they scale beautifully to 2D Matrix prefix sums simplify many complex update operations Thinking in terms of range operations often leads to faster algorithms 💻 Tech Used Java 2D prefix sum optimization Matrix manipulation Another day, another concept mastered. On to the next challenge! 💪🔥 #coding #leetcode #java #programming #dsa #matrix #problemsolving #500DaysOfCode #Day414
To view or add a comment, sign in
-
-
🚀 Day 43 of #100DaysOfCode – Striver’s DSA Sheet 🚀 ✅ Topic Covered Today: Queue – Basic Implementation Using Java JCF 💡 Lesson of the Day (Approach-Focused): 🧠 Basic Queue Implementation (Java JCF) Today, I revised how queues work internally and how Java’s Queue interface helps implement FIFO operations efficiently using LinkedList. 🔹 Operations Covered: add() → Insert element at the rear poll() → Remove element from the front peek() → Access the front element isEmpty() → Check if the queue is empty size() → Get current size of queue All operations follow the FIFO Principle (First In, First Out). 🔹 Approach & Understanding: Queue does not support indexing → traversal happens sequentially. LinkedList is commonly used because insertion and deletion at ends are O(1). Helps build intuition for problems like sliding window, BFS, task scheduling, and producer-consumer logic. 🧮 Time Complexity: Enqueue (add): O(1) Dequeue (poll): O(1) Peek: O(1) Traversal: O(n) 💾 Space Complexity: O(n) for storing elements in queue. 💭 Learning: Revisiting how Queue works through JCF helped reinforce the fundamentals of FIFO behavior, which is essential before diving into advanced queue-based problems. #100DaysOfCode #DSA #StriversSheet #Java #Queue #JCF #ProblemSolving #CodingJourney #LogicBuilding #Consistency
To view or add a comment, sign in
-
More from this author
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