Skip to content

Missing Test Case - 167. Two Sum II - Input Array Is Sorted #31717

@Kaushikj-7

Description

@Kaushikj-7

LeetCode Username

https://leetcode.com/u/kaushikj25

Problem Number, Title, and Link

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/

Bug Category

Missing test case (Incorrect/Inefficient Code getting accepted because of missing test cases)

Bug Description

/**

  • Approach: Two Pointers
  • Since the input array is sorted in non-decreasing order, we can use a
  • two-pointer technique to find the two numbers that add up to the target.
  • Initialize two pointers:
    • left at the beginning (index 0)
    • right at the end (index numbers.length - 1)
  • At each step:
    • Calculate the sum of the values at the left and right pointers.
    • If the sum is equal to the target, return their 1-based indices.
    • If the sum is less than the target, move the left pointer to the right.
    • If the sum is greater than the target, move the right pointer to the left.
  • Time Complexity: O(n)
  • Space Complexity: O(1)
  • Note: The problem guarantees exactly one solution, so we do not need to
  • handle the case where no solution exists.
    */

Language Used for Code

None

Code used for Submit/Run operation

class Solution {
        public int[] twoSum(int[] numbers, int target) {
         int left = 0, right = numbers.length - 1;

         while (left < right) {
            int sum = numbers[left] + numbers[right];

            if (sum == target) {
                return new int[] {left + 1, right + 1}; // 1-indexed
             } else if (sum < target) {
…

Expected behavior

if the elements of the added sum (target) is not found in the array then it should send a fallback return like return new int[] {-1, -1}; is a defensive programming habit. It prevents compiler errors (e.g., “missing return statement”) if you don't return anything in all code paths. But in this problem, it’s safe and clean to omit it entirely if your logic guarantees a return in every case.

Screenshots

No response

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions