⚙️ Day 38 of My LeetCode Journey — Problem #2654 “Minimum Number of Operations to Make All Array Elements Equal to 1” (Java Solution) 💡 Today’s challenge beautifully combined number theory with algorithmic optimization. The goal: turn every element in an array into 1 using the minimum operations — and the key insight was rooted in GCD properties. 🔍 My thought process: If any 1s exist → we just need to handle the rest (n - count(1)). Otherwise → find the shortest subarray with GCD = 1, since it’s the only way to generate a 1. The final answer = minimal window length + (n - 1) operations. It’s fascinating how understanding mathematical relationships can drastically simplify code complexity 🔢 Each problem reminds me — elegant logic is what turns code into art 🧠💻 #Day38 #LeetCode #Java #ProblemSolving #NumberTheory #Algorithms #DSA #CodingJourney #100DaysOfCode #CodeEveryday #SoftwareEngineering #LearningInPublic #TechCommunity
Solving LeetCode #2654 with Java and Number Theory
More Relevant Posts
-
💻 Day 51 of #LeetCode100DaysChallenge Solved LeetCode 13: Roman to Integer — a problem that tests mapping, iteration, and conditional logic. 🧩 Problem: Given a Roman numeral, convert it to an integer. Roman numerals use symbols I, V, X, L, C, D, and M. Normally, symbols are added from largest to smallest. However, in six cases (IV, IX, XL, XC, CD, CM), subtraction is applied. 💡 Approach — Mapping & Iteration: 1️⃣ Create a map of Roman symbols to integer values. 2️⃣ Iterate through the string: Add the value if the current symbol is greater than or equal to the next. Subtract the value if the current symbol is smaller than the next. 3️⃣ Sum all values to get the final integer. ⚙️ Complexity: Time: O(N) Space: O(1) ✨ Key Takeaways: ✅ Practiced mapping and iteration. ✅ Handled edge cases with subtraction logic. ✅ Reinforced sequential decision-making in string processing. #LeetCode #100DaysOfCode #Java #Mapping #Iteration #ConditionalLogic #DSA #CodingJourney #WomenInTech #RomanToInteger
To view or add a comment, sign in
-
-
DSA Practice – Day 52 🚀 Problem: Happy Number (LeetCode 202) 📌 Problem Statement: A number is called happy if repeatedly replacing it with the sum of the squares of its digits eventually leads to 1. Return true if it’s a happy number, otherwise false. 🧩 Brute Force Approach: Keep track of all numbers seen in a set to detect loops. If you reach 1, Happy Number If a number repeats, Not Happy Time Complexity: O(log n) Space Complexity: O(log n) ⚡ Optimal Approach (Floyd’s Cycle Detection): Use two pointers — slow and fast. Move slow by one step and fast by two steps (using sum of squares). If they meet, there’s a cycle (not happy). If you reach 1, it’s a happy number. Time Complexity: O(log n) Space Complexity: O(1) ✨ What I Learned: How to detect cycles in number transformations. Applying Floyd’s cycle detection beyond linked lists. Improved logical problem-solving for number-based questions. #LeetCode #Java #ProblemSolving #DSA #CodingJourney #PlacementPrep
To view or add a comment, sign in
-
-
🚀 115 days of #200DaysOfCode Problem: 24. Swap Nodes in Pairs Problem Statement: Given the head of a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes, only nodes themselves may be changed. Approach: Used a dummy node and iteratively swapped each adjacent node pair via pointer manipulation, which enabled in-place node swaps without extra space. Logic: Leveraged pointer rewiring to achieve the swaps efficiently with O(1) extra space and O(n) time complexity, cleanly iterating through the list to handle both even and odd-length cases. 👉 Question link 🔗: https://lnkd.in/g6cwvMgz #LeetCode #Java #LinkedList #Pointers #DSA #Coding #Algorithms #InterviewPrep #200DaysOfCode
To view or add a comment, sign in
-
-
✅Day 41 : Leetcode 153 - Find Minimum in Rotated Sorted Array #60DayOfLeetcodeChallenge 🧩 Problem Statement Given a sorted array that has been rotated at an unknown pivot, find the minimum element in the array. The array contains unique elements, and the solution must run in O(log n) time. 💡 My Approach I used a binary search technique to efficiently find the minimum element. I maintained two pointers, low and high. At each step, I calculated the mid-point. If the left part (nums[low] to nums[mid]) was sorted, I updated my answer with the smaller of nums[low] and current ans, and moved low to mid + 1. Otherwise, I updated my answer with nums[mid] and moved high to mid - 1. This approach ensures we keep narrowing the search space toward the minimum element. ⏱️ Time Complexity O(log n) — Because the search space is halved in each iteration. #BinarySearch #LeetCode #RotatedSortedArray #DSA #CodingPractice #Java #ProblemSolving
To view or add a comment, sign in
-
-
#Day36 Of Problem Solving Successfully solved LeetCode #343 – Integer Break 💡 Achieved an Accepted Solution with 0 ms runtime (beats 100% of Java submissions) 🎯 This problem was a great exercise in mathematical optimization and problem decomposition. The key insight was to maximize the product by breaking the integer into 3’s — leveraging both mathematical reasoning and efficient implementation. 📘 Concepts Used: Mathematical analysis of number partitioning Power function and modular logic Time complexity: O(1) Every solved problem is another step toward mastering algorithmic thinking and optimization ⚡ #LeetCode #Java #Coding #ProblemSolving #Mathematics #DSA #DynamicProgramming #CompetitiveProgramming #LearningJourney #SoftwareEngineering #Consistency #Linkedin #HackerRank
To view or add a comment, sign in
-
-
📌 Day 51/100 – Longest Word in Dictionary (LeetCode 720) 🔹 Problem: Given an array of words, find the longest word that can be built one character at a time by other words in the array. If multiple results exist, return the lexicographically smallest one. 🔹 Approach: Built a Trie structure to store all words. Used DFS traversal to explore all valid prefixes (where every prefix forms a valid word). Updated the answer when a longer or lexicographically smaller valid word was found. 🔹 Key Learning: Deepened understanding of Trie traversal with DFS. Practiced combining lexicographical comparison with prefix validation. Reinforced prefix-based word-building logic efficiently. 🔹 Complexity: Time: O(N × L) — N = number of words, L = average word length Space: O(26 × N × L) — for Trie storage #Day51Of100 #LeetCode720 #100DaysOfCode #Java #DSA #Trie #DFS #ProblemSolving #CodingChallenge #Strings #DataStructures #CodingJourney #KeepLearning
To view or add a comment, sign in
-
-
✅Day 54 : Leetcode 3228 - Maximum Number of Operations to Move Ones to the End #60DayOfLeetcodeChallenge 🧩 Problem Statement You are given a binary string s. You can perform the following operation any number of times: Choose an index i such that: i + 1 < s.length s[i] == '1' s[i+1] == '0' Then move this '1' to the right until it reaches the next '1' or the end of the string. Your task is to return the maximum number of such operations that can be performed. ✅ My Approach Instead of simulating every movement (which is slow), I used a counting strategy: 🔹 Key Idea Whenever I see a '1', I increase the count of ones seen so far. Whenever I see a pattern like …10…, this '1' can move past all previous ones, contributing extra operations equal to the number of '1's before it. 🔹 Steps Initialize: ones = 0 → counts how many '1' encountered so far. res = 0 → stores total operations. Traverse the string: If current char is '1' → increment ones. Else if the previous char is '1' (meaning we found 10) → add ones to result. Return res as the maximum operations. This avoids simulation and gives the optimal count directly. ⏱ Time Complexity O(n) — Single scan through the string O(1) space #dsa #leetcode #binarysearch #java #coding #problemsolving #100daysofdsa #interviewpreparation #learningeveryday #codingjourney
To view or add a comment, sign in
-
-
📅 Day 82 of #100DaysOfLeetCode Problem: Delete Node in a BST (LeetCode #450) Approach: The goal is to delete a node with a specific key from a Binary Search Tree (BST). The deletion process involves two main steps: Search for the node to be deleted. Delete it while maintaining the BST property. There are three cases when deleting a node: Leaf node: Simply remove it. One child: Replace the node with its child. Two children: Find the inorder successor (smallest value in the right subtree), replace the node’s value with it, and delete the successor recursively. Complexity: ⏱️ Time: O(h), where h is the height of the BST. 💾 Space: O(h), recursive call stack. 🔗 Problem Link: https://lnkd.in/dx4XEUgz 🔗 Solution Link: https://lnkd.in/d3-RJ6yJ #LeetCode #100DaysOfCode #BinarySearchTree #Recursion #Java #DSA #Algorithms #ProblemSolving #CodeNewbie #TreeTraversal #StudyWithMe #DailyCoding #BuildInPublic #LearnToCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 59 of #100DaysOfCode 🚀 🔹 Problem: Check if Digits Are Equal in String After Operations I – LeetCode ✨ Approach: Used an iterative reduction strategy 🔁 — repeatedly combined adjacent digits (mod 10) until only two numbers remained. Finally checked if both digits are equal! Simple yet logical 🧠 ⚡ Complexity Analysis: Time Complexity: O(n²) – iterative pairwise reduction until only two digits remain Space Complexity: O(n) – storing intermediate list of digits 📊 Performance: ✅ Runtime: 10 ms (Beats 35.69%) ✅ Memory: 45.51 MB (Beats 12.86%) 🔑 Key Insight: Sometimes, brute-force reduction problems aren’t about optimization — they’re about translating logic into clean code that mirrors the operation flow perfectly. ✨ #LeetCode #100DaysOfCode #Java #DSA #ProblemSolving #CodingChallenge #LogicBuilding #ProgrammingJourney #DailyCoding
To view or add a comment, sign in
-
-
💻 Day 11 of #100DaysOfLeetCode – Rotate Image Today’s challenge was “Rotate Image”, a classic matrix manipulation problem that tests both logic and spatial reasoning. 🔹 Problem Statement: Given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise) — all in place, without using extra memory for another matrix. 🔹 My Approach (in Java): I used a two-step in-place transformation technique: 1️⃣ Transpose the matrix — swap elements across the diagonal (matrix[i][j] ↔ matrix[j][i]). 2️⃣ Reverse each row — to achieve the 90° clockwise rotation. This approach ensures O(1) extra space and O(n²) time complexity, which is optimal for this problem. 🔹 Key Takeaways: ✅ Learned how matrix transformations can be broken down into simpler operations. ✅ Improved understanding of in-place algorithms and memory efficiency. ✅ Reinforced clean coding habits and edge case handling. Every rotation brings me one step closer to mastering problem-solving patterns! 💪 #100DaysOfLeetCode #CodingJourney #RotateImage #Java #DSA #ProblemSolving #LeetCode #CodingChallenge #LearningEveryday
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