Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Two Sum II – Input array is sorted problem solution

YASH PAL, 31 July 2024

In this Leetcode Two Sum II – Input array is sorted problem solution we have Given an array of integers numbers that are already sorted in non-decreasing order, find two numbers such that they add up to a specific target number.

Return the indices of the two numbers (1-indexed) as an integer array answer of size 2, where 1 <= answer[0] < answer[1] <= numbers.length. The tests are generated such that there is exactly one solution. You may not use the same element twice.

Leetcode Two Sum II - Input array is sorted problem solution

Problem solution in Python.

class Solution(object):
    def twoSum(self, numbers, target):
        head = 0
        tail = len(numbers) - 1
        while numbers[head] + numbers[tail] != target:
            if numbers[head] + numbers[tail] > target:
                tail -= 1
            else:
                head += 1
        return [head + 1, tail + 1]

Problem solution in Java.

class Solution {
public int[] twoSum(int[] numbers, int target) {
int[]a=new int[2];
int start=0,end=numbers.length-1;
while(start<end)
{
if(numbers[start]+numbers[end]==target)
{
return new int[]{start+1,end+1};

        }
        else  if(numbers[start]+numbers[end]>target)
        {
            end--;
        }
        else
            
        {
            start++;
        }
        
    }
    return new int[2];
    
}
}

Problem solution in C++.

class Solution 
{
    public:
        vector<int> twoSum(vector<int>& numbers, int target) 
        {
            int start = 0, end = (numbers.size() - 1);
            while (start < end)
            {
                if ((numbers[start] + numbers[end]) > target) end--;
                else if ((numbers[start] + numbers[end]) < target) start++;
                else return {++start, ++end};
            }
            return {};
        }
};

Problem solution in C.

int* twoSum(int* numbers, int numbersSize, int target, int* returnSize){
    int i=0,j=numbersSize-1, sum;
	
    while(i<j){
        sum = numbers[i]+numbers[j];
        if(sum<target) i++;
        else if(sum>target) j--;
        else break;
    }
	
    int *dst = (int*)malloc(2*sizeof(int));
    dst[0]=i+1;
    dst[1]=j+1;
    *returnSize=2;
    return dst;
}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes