Skip to main content

Command Palette

Search for a command to run...

tow sum in java

Published
1 min readView as Markdown
class Solution {
    boolean twoSum(int arr[], int target) {

      Arrays.sort(arr); // Step 1: Sort the array
        int n= arr.length;
        int left = 0;
        int right = n - 1;

        while (left < right) {
            int sum = arr[left] + arr[right];
            if (sum == target) {
                return true;
            } else if (sum < target) {
                left++;
            } else {
                right--;
            }

        }

        return false;
    }
}

More from this blog

Amit singh's blog

235 posts