Leetcode Practice - 16. 3Sum Closest The problem is solved using JAVA Given an integer array nums of length n and an integer target, find three integers at distinct indices in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. Example 1: Input: nums = [-1,2,1,-4], target = 1 Output: 2 Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). Example 2: Input: nums = [0,0,0], target = 1 Output: 0 Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0). Constraints: 3 <= nums.length <= 500 -1000 <= nums[i] <= 1000 -104 <= target <= 104 #LeetCode #Java #CodingPractice #ProblemSolving #DSA #Array #DeveloperJourney #TechLearning
3Sum Closest Problem Solution in Java
More Relevant Posts
-
Leetcode Practice - 8. String to Integer (atoi) The problem is solved using JAVA. Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm for myAtoi(string s) is as follows: ✔ Whitespace: Ignore any leading whitespace (" "). ✔ Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity if neither present. ✔ Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0. ✔ Rounding: If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than -231 should be rounded to -231, and integers greater than 231 - 1 should be rounded to 231 - 1. Return the integer as the final result. Example 1: Input: s = "42" Output: 42 Explanation: The underlined characters are what is read in and the caret is the current reader position. Step 1: "42" (no characters read because there is no leading whitespace) ^ Step 2: "42" (no characters read because there is neither a '-' nor '+') ^ Step 3: "42" ("42" is read in) #LeetCode #Java #StringHandling #CodingPractice #ProblemSolving #DSA #DeveloperJourney #TechLearning
To view or add a comment, sign in
-
-
🚀 Day 52/100 Today’s problem was based on String Manipulation — reversing the first k characters of a string. 🧠 What I learned: - How to efficiently manipulate strings - Using "StringBuilder" for reversal in Java - Writing clean and optimized code 💡 Approach: Reversed the first k characters and appended the remaining string. ⚡ Key Insight: Even simple problems help strengthen fundamentals, which are crucial for solving complex problems later. 👨💻 Code (Java): class Solution { public String reversePrefix(String s, int k) { String first = new StringBuilder(s.substring(0, k)).reverse().toString(); String rest = s.substring(k); return first + rest; } } 📈 Consistency is the key — showing up every day matters more than perfection. #Day52 #CodingChallenge #Java #DSA #LearningJourney #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
Leetcode Practice - 15. 3Sum The problem is solved using JAVA. Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets. Example 1: Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Explanation: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0. The distinct triplets are [-1,0,1] and [-1,-1,2]. Notice that the order of the output and the order of the triplets does not matter. Example 2: Input: nums = [0,1,1] Output: [] Explanation: The only possible triplet does not sum up to 0. Example 3: Input: nums = [0,0,0] Output: [[0,0,0]] Explanation: The only possible triplet sums up to 0. Constraints: 3 <= nums.length <= 3000 -105 <= nums[i] <= 105 #LeetCode #Java #CodingPractice #ProblemSolving #DSA #Array #DeveloperJourney #TechLearning
To view or add a comment, sign in
-
-
🔢 Divisible Sum Pairs Problem | Java Solution 💻 Today I solved an interesting problem based on arrays and modular arithmetic! 📌 Problem: Given an array and a number k, count pairs (i, j) such that: 👉 i < j 👉 (arr[i] + arr[j]) % k == 0 ⚡ Approach: Instead of brute force O(n²), I used an optimized O(n) approach using remainder frequency. ✔️ Key Idea: Store frequency of (element % k) For each element, find its complement remainder Count valid pairs efficiently 💡 This improves performance significantly for large inputs! 🧠 Concepts Used: Arrays Modulo Arithmetic Hashing Technique 👨💻 Language: Java #Java #CodingPractice #DataStructures #Algorithms
To view or add a comment, sign in
-
-
📅 Day 10 – Strings in Java 📚 Topic: String Manipulation (Hashing) Today I solved the problem of checking whether two strings are anagrams using an efficient frequency-count approach. This helped me understand how hashing can be used to compare character distributions instead of sorting. ✔ Key Learnings: • Applied hashing using a frequency array • Improved understanding of character frequency comparison • Learned how to optimize from sorting (O(n log n)) to linear time (O(n)) Building problem-solving skills step by step and focusing on writing efficient code 🚀 #DSA #Strings #Java #ProblemSolving #LearningJourney
To view or add a comment, sign in
-
-
Solved LeetCode 17 – Letter Combinations of a Phone Number using backtracking in Java. Approach: Mapped each digit (2–9) to its corresponding characters using a simple array for O(1) access. Then used backtracking to build combinations digit by digit. For every digit: Pick each possible character Append → explore next digit → backtrack Key idea: Treat it like a tree of choices, where each level represents a digit and branches represent possible letters. Key learnings: Backtracking = build → explore → undo StringBuilder helps avoid unnecessary string creation Problems like this are about systematic exploration of choices Time Complexity: O(4^n * n) Space Complexity: O(n) recursion stack + output Consistent DSA practice is strengthening pattern recognition day by day. #Java #DSA #Backtracking #LeetCode #CodingInterview #SoftwareEngineering
To view or add a comment, sign in
-
-
This Java snippet trips up even experienced developers 👇 Integer a = 127; Integer b = 127; Integer c = 128; Integer d = 128; System.out.println(a == b); System.out.println(c == d); One prints true. One prints false. At first glance, both comparisons look identical. Same type. Same assignment. Same operator. So what’s going on? If you know the reason, you’ve got a deeper grasp of how Java actually works under the hood. Drop your explanation below — no Googling 👇 #Java #Programming #SoftwareEngineering #DevCommunity #CodeChallenge
To view or add a comment, sign in
-
#Day86 of #100DaysOfCode Started learning String manipulation in Java. Covered: * Basic string operations * Using built-in methods like length() and charAt() Practiced problems like: * Reversing a string * Checking palindrome strings * Counting vowels Focused on understanding how to process and manipulate text data. #Java #Strings #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Binary Tree Inorder Traversal (LeetCode 94) 💻 Language: Java 🔹 Approach: To solve this problem, I used Depth-First Search (DFS) with Recursion to perform inorder traversal of the binary tree. • Recursively traverse the left subtree • Visit the root node and store its value • Recursively traverse the right subtree • Follow Inorder pattern: Left → Root → Right This approach works efficiently because recursion naturally follows the inorder traversal structure. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) (including recursion stack) 📖 Key Learning: This problem strengthened my understanding of DFS, recursion, and tree traversal patterns. It also reinforced the inorder pattern (Left → Root → Right), which is a fundamental concept used in many tree-based problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #BinaryTree #DFS #Recursion #TreeTraversal
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Binary Tree Preorder Traversal (LeetCode 144) 💻 Language: Java 🔹 Approach: To solve this problem, I used Depth-First Search (DFS) with Recursion to perform preorder traversal of the binary tree. • Visit the root node first • Store the root value in the result list • Recursively traverse the left subtree • Recursively traverse the right subtree • Follow Preorder pattern: Root → Left → Right This approach works efficiently because recursion naturally follows the preorder traversal structure. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) (including recursion stack) 📖 Key Learning: This problem strengthened my understanding of DFS, recursion, and tree traversal patterns. It also reinforced the preorder pattern (Root → Left → Right), which is a fundamental concept used in many tree-based problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #BinaryTree #DFS #Recursion #TreeTraversal
To view or add a comment, sign in
-
Explore related topics
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