1. Two sum leetcode java Solution in Tamil - Leet code series Problem - 1 in தமிழ்

Опубликовано: 03 Август 2026
на канале: siya_tech_tales
244
9

Follow us on Instagram: https://www.instagram.com/siyatechtal...
---------------------------------
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:

Input: nums = [3,3], target = 6

Output: [0,1]

---------------------------------------------------
📌What is the Problem Asking?
We are given an array of numbers (integers) and a target sum.
The goal is to find two numbers in the array whose sum equals the target.
The question asks us to return the indices (positions) of those two numbers in the array.

Important rule: You cannot use the same element twice, and there is always one valid solution.

Example 1:
Input: nums = [2, 7, 11, 15], target = 9
We need to find two numbers in the array that add up to 9.
If we check, 2 + 7 = 9, so the correct output is the indices of 2 and 7, which are [0, 1].

Example 2:
Input: nums = [3, 2, 4], target = 6
The two numbers that add up to 6 are 2 + 4. Their indices are [1, 2].
Plan to Solve the Problem
Here’s how we are going to approach this:

Loop through the array: We will go through each number in the array using a single loop.

Calculate the required number: For each number, we will calculate what number we need to make the sum equal to the target. For example, if the number is 2 and the target is 9, we need a 7 (9 - 2 = 7).

Check if this number already appeared: As we loop through the array, we’ll check if this required number has appeared earlier in the array (before the current number).

Return the indices: If the required number is found, return the index of the required number and the index of the current number.

How We Will Code This
Loop through the array: We’ll use a simple for loop to check each number.
Keep track of the previous numbers: We’ll compare the current number with the numbers we have already checked.
Return the answer: As soon as we find a match, we will return the indices of the two numbers.

-------------------------------------------------------
Copyright Disclaimer under Section 52 of the Copyright Act, 1957 (India):
This video is made for educational, informational, and entertainment purposes only. The content is transformative in nature, and its use is considered fair use under Indian copyright law. No copyright infringement is intended, and all rights belong to their respective owners. The purpose of this video is to educate, inform, and entertain, and it is not intended to harm or exploit any individual or entity. If you have any concerns or objections, please contact us at [email protected]
-------------------------------------------------------
#leetcode
#twosum
#javaprogramming