Question: 2. In this exercise, you will develop a quadratic-time algorithm for the 3-sum problem. When we ask you to design an algorithm , give a

2. In this exercise, you will develop a quadratic-time algorithm for the 3-sum problem. When we ask you to design an algorithm, give a crisp and concise English description of your algorithmdon't write Java code.

A. Given an integer x and a sorted array a[] of N distinct integers, design a linear-time algorithm to find if there exists two distinct indices i and j such that (a[i] + a[j] == x). (20 Points)

Hint: start by checking whether a[0] + a[N-1] is smaller than, greater than, or equal to x.

B. Given an array a[] of N distinct integers, design a quadratic-time algorithm to find if there exists three distinct indices i, j, and k such that (a[i] + a[j] + a[k] == 0). (20 Points)

Hint: Use the result from (a). You can assume the array is sorted since sorting the array can be done in quadratic (and even linearithmic) time.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!