Skip to main content

Java Problem 05 | The Two-Sum = Target of an Array in Java

Hey everyone! Back with another Java problem walkthrough. This time we’re tackling a classic — the Two-Sum problem. You know, the one where you’re given an array and a target number, and you have to find which two numbers add up to that target. It sounds simple, but it trips up a lot of people starting out.
In this video, I’ll walk you through my thought process while solving it step by step. We’ll start by breaking down the problem, then I’ll show you how to set up the loops properly to avoid using the same element twice (a common mistake!). I’ll also talk about why a simple approach like this works, even if it’s not the fanciest or fastest.
At the end, I’ll run the code with some test cases to make sure it really works. Stick around to see if you can spot how we could improve this solution next time — hint: there’s a much quicker way if we use a different data structure!
If this helped you, give it a thumbs up 👍 and drop a comment below if you’ve seen this problem in an interview or exam! Don’t forget to subscribe and hit the bell so you don’t miss the next one.







import java.util.Arrays;

public class SumTwo {
	// Problem: Given an array of integers nums and an integer target,
	// return indices of the two numbers such that they add up to target.
	// Example: nums[] = {1,2,3,4,5}, target = 8 → returns [2, 4]
	// Constraint: You may not use the same element twice.
	
	public static void main(String[] args) {
		int[] nums = new int[] {6,7,8,9,10}; // Input array
		int target = 19; // Target sum
		System.out.println(Arrays.toString(getIndexes(nums, target))); // Print result
	}
	
	static int[] getIndexes(int[] nums, int target) {
		int[] ans = new int[0]; // Default empty array if no solution found
		
		// Brute force approach: check all possible pairs
		for (int i = 0; i < nums.length; i++) {
			for (int j = 0; j < nums.length; j++) {
				// Check if sum equals target and indices are different
				if (nums[i] + nums[j] == target && i != j) {
					System.out.println(nums[i] + " , " + nums[j]); // Debug print
					return ans = new int[] {i, j}; // Return solution indices
				}
			}
		}
		return ans; // Return empty array if no solution found
	}
}


TwoSum Java solution tutorial, LeetCode problem 1, coding interview preparation, beginner Java programming, array algorithms, brute force method, find two numbers that add to target, step by step coding, programming for beginners, how to solve TwoSum, Java coding examples, software engineering interview questions, data structures and algorithms, programming tutorial, Java practice problems, algorithm explanation, coding challenge walkthrough, computer science basics, learn programming, Java development skills, problem solving in Java, coding interview tips, technical interview practice, Java array problems, programming logic building
#TwoSum #JavaSolution #LeetCode #CodingInterview #JavaProgramming #BeginnerJava #ArrayAlgorithms #BruteForceMethod #TwoNumbersAddToTarget #StepByStepCoding #ProgrammingForBeginners #HowToSolveTwoSum #JavaCodingExamples #SoftwareEngineeringInterview #DataStructuresAndAlgorithms #ProgrammingTutorial #JavaPracticeProblems #AlgorithmExplanation #CodingChallengeWalkthrough #ComputerScienceBasics #LearnProgramming #JavaDevelopment #ProblemSolvingInJava #CodingInterviewTips #TechnicalInterviewPractice #JavaArrayProblems #ProgrammingLogic


Comments

Popular posts from this blog

How to fix MOV files not playing on Windows 10 or 11

How to TEST KEYBOARDS on PC or Laptop

Apple CONFIRMS iPhone 17 Pro Max Camera Noise Bug - Fix Coming?