TASK#3:: Code For Threesum
TASK#3:: Code For Threesum
package a01_daa;
/**
*
* @author
*/
public class ThreeSum {
/**
* Prints to standard output the (i, j, k) with {@code i < j < k}
* such that {@code a[i] + a[j] + a[k] == 0}.
*
* @parameter a is the array of integers
*/
public static void printAllThreeSum(int[] a) {
int n = a.length; //stored the array length in an ineteger N
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
for (int k = j+1; k < n; k++) {
if (a[i] + a[j] + a[k] == 0) {
System.out.println("Pairs are:" + a[i] + " " + a[j] + " " + a[k]); //printing pairs
of 3sum
}
}
}
}
}
/**
1
* Returns the number of triples (i, j, k) with {@code i < j < k}
* such that {@code a[i] + a[j] + a[k] == 0}.
*
* @parameter a is the array of integers
*/
public static int countSum(int[] a) {
int n = a.length; //stored array length in an integer
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
for (int k = j+1; k < n; k++) {
if (a[i] + a[j] + a[k] == 0) {
count++; //increment when found
}
}
}
}
return count; //getting number of triplets
}
}
----------------------------------------------------------------------------------------------------------------
package a01_daa;
import java.util.*;
/**
*
* @author
*/
public class FastThreeSum {
int binarySearch(int arr[], int l, int r, int x)//method to perform binary search
{
if (r>=l) //if elemengt at right is greater or equal to left element
{
int mid = l + (r - l)/2;
// If element is smaller than mid, then it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid-1, x);
}
----------------------------------------------------------------------------------------------------------------
MAIN Function:
package a01_daa;
import java.util.*;
import java.time.*;
import java.lang.*;
/**
*
* @author
*/
public class A01_DAA {
private static int[] anArray; //initialzied an array
public static void main(String[] args) //main function
{
anArray = new int[100];
Random rand = new Random(); //Random object instantiation
for (int i = 0; i < 100; i++) {
anArray[i] = ((rand.nextInt(100)%100)-50); //generating random numbers and storing in
array
}
}
}
----------------------------------------------------------------------------------------------------------------
a.
Table:
Running times for input data in seconds
1K 4K 8K 12K
3-Sum 0.485 45.851 159.392 630.527
Count of Triplets
b.
Graphs:
For THREESUM:
Fast THREESUM:
Computer? Answer:
Processor: Intel®Core™i7-5500U CPU@ 2.40GHz (4 CPUs), ~2.4Ghz
RAM: 8192MB
OS: Windows 8.1 Single Language 64-bit (6.3, Build 9600)
Question 3: What makes Fast-3-sum faster than 3-sum
implementation?
Answer:
Fast THREESUM is faster than the naïve approach for THREESUM because of following
reasons:
1. Instead of checking for every possible value of K, Binary search in FAST
THREESUM helps to find the index on which A[i]+A[j] has negative value.
2. A lot of searching time is being saved in case of FAST THREESUM.
3. There are three loops in Brute-Force while two in case of FAST THREESUM.
4. The complexity for Brute-Force is O(n^3).
5. The complexity for Fast THREESUM is O(n^2log(n)).
6. The time taken to compute is greater for Cubic function as compared to Quadratic-
Logarithmic function.
7. The graph above shows the time taken for brute-force approach is almost 37-28 times.
8. The use of binary search in fast three sum makes it faster as compared to brute-force
having three for loops.
Acknowledgments:
1. https://www.geeksforgeeks.org
2. https://codereview.stackexchange.com
3. https://www.programcreek.com
4. https://github.com/