Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Two Sum Sorted Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Two Sum Sorted

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a sorted array, check if there exist two numbers whose sum is zero.

Naive Approach

We loop over all pairs of elements in the array and check if we can find two numbers such that A[i] = -A[j]. If yes, return true, else return false.

 Analysis

  • Time Complexity: O(n2)
  • Space Complexity: O(1)

Implementation

C++

bool hasTwoSumZero(vector<int> A) {
	int n = A.size();
	for(int i = 0; i < n; i++) {
		for(int j = i + 1; j < n; j++) {
			if(A[i] + A[j] == 0){
				return true;
			}
		}
	}
	return false;
}

Java

class Solution {
	boolean hasTwoSumZero (int[] A) {
		int n = A.length;
		for(int i = 0; i < n; i++) {
			for(int j = i + 1; j < n; j++) {
				if(A[i] + A[j] == 0){
					return true;
				}
			}
		}
		return false;
	}
}

Optimal Approach

Since it is told that the array is sorted, we will use the 2 pointers technique to solve this problem. We keep 2 pointers, pointing at either end of the array. If A[left] + A[right] = 0, we return true, if A[left] + A[right] > 0, we move the right pointer to the left by 1, else we move the left pointer to right by 1. We continue this, till the left pointer exceeds the right pointer.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation

C++
bool hasTwoSumZero(vector<int> A) {
	int n = A.size();
	int left = 0, right = n - 1;
	while(left < right) {
		if(A[left] + A[right] == 0) {
			return true;
		}
		else if(A[left] + A[right] > 0) {
			right--;
		}
		else {
			left++;
		}
	}
	return false;
}
Java
class Solution {
	boolean hasTwoSumZero (int[] A) {
		int n = A.length;
		int left = 0, right = n - 1;
		while(left < right) {
			if(A[left] + A[right] == 0) {
				return true;
			}
			else if(A[left] + A[right] > 0) {
				right--;
			}
			else {
				left++;
			}
		}
		return false;
	}
}
Related Content
Dutch National Flag
k-diff pairs
K-Subarray Sum
k-Substring Vowels
Kth element of two sorted lists
Maximum K-Subarray Sum
Maximum k-Substring Vowels
Merge Two Sorted Arrays
Remove occurences
Sorted Arrays Intersection
Three Sum
Trapping Rain Water
Unique Elements in Sorted Array
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet