Question

Write three different algorithms of different time complexities to find the closest pair in a sorted...

Write three different algorithms of different time complexities to find the closest pair in a sorted array of integers. (Using java)

Homework Answers

Answer #1

1. Naive algorithm

public static void closestPair(int[] Arr, int sum)
   {
       for (int i = 0; i < Arr.length - 1; i++)
       {           for (int j = i + 1; j < Arr.length; j++)
           {
               if (Arr[i] + Arr[j] == sum)
               {
                   System.out.println("Pair found at index " + i + " and " + j);
                   return;
               }
           }
       }
       System.out.println("Pair not found");
   }

Time complexity: O(n2)

2. nlogn algorithm

public static void closestPair(int[] Arr, int sum)
   {
       int low = 0;
       int high = Arr.length - 1;
       while (low < high)
       {
           if (Arr[low] + A[high] == sum)
           {
               System.out.println("Closest Pair found");
               return;
           }

           if (Arr[low] + Arr[high] < sum) {
               low++;
           } else{
               high--;
           }
       }

       System.out.println("Closest Pair not found");
   }

Time complexity: O(nlogn)

3. O(n) algorithm using hashing

import java.util.HashMap;
import java.util.Map;
class Main
{
   public static void closestPair(int[] Arr, int sum)
   {
       Map<Integer, Integer> map = new HashMap<>();
       for (int i = 0; i < Arr.length; i++)
       {

           if (map.containsKey(sum - Arr[i]))
           {
               System.out.println("Closest Pair found at index " +
                               map.get(sum - Arr[i]) + " and " + i);
               return;
           }

           map.put(Arr[i], i);
       }

       System.out.println("Closest Pair not found");
   }


}

Time complexity: O(n)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
UNSORTED VERSION: Write a Java program that uses “brute force” to find the closest pair, given...
UNSORTED VERSION: Write a Java program that uses “brute force” to find the closest pair, given N distinct integer points in 1-dimension. That is, all points are integers on the x-axis, and are in an array. This algorithm should go through all pairs (without sorting the elements of the array), one pair at a time, to find the current closest pair. For example, assume we had the following integers: 4, 2, -2, -1 in an array. We would then generate...
write program to compute intersection of two sorted array of integers and compute the CPU time...
write program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers DONT FORGET CPU TIME FOR EACH ONE Java
Test each algorithm (Counting Sort, Radix Sort, and Bucket Sort algorithms) on three different arrays of...
Test each algorithm (Counting Sort, Radix Sort, and Bucket Sort algorithms) on three different arrays of 1000 elements each with the following properties: By the way: To perform bucket sort the right way, convert the array elements to a value between 0 and 1 Array1: integers only in the range 0 through 999, already sorted in increasing order Array2: integers only in the range 0 through 999, already sorted in decreasing order Array3: integers only in the range 0 through...
DIVIDE AND CONQUER Implement the two algorithms for finding the kth smallest integer in a set...
DIVIDE AND CONQUER Implement the two algorithms for finding the kth smallest integer in a set of integers using only one array that contains all the integers. Test your programs and compute the CPU time for different sets of integers generated by a random number generator. Must sure have the CPU time.
Write code in java Implement a method to build an AVL tree out of a sorted...
Write code in java Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You...
1. Define the problem Closest-Pair as follows. • Input: an array A consisting of distinct numbers....
1. Define the problem Closest-Pair as follows. • Input: an array A consisting of distinct numbers. • Output: the numbers x, y in A such that |x − y| is as small as possible. Design an O(n log n) time algorithm for this problem . Define the List-Delete problem as follows. • Input: A linked list L of distinct integers and an element a of L. • Output: L with the element a deleted. Design an O(1)-time algorithm for the...
write JAVA code of Particle Swarm Optimization (PSO) algorithms to solve Travelling Salesman Problem (TSP): find...
write JAVA code of Particle Swarm Optimization (PSO) algorithms to solve Travelling Salesman Problem (TSP): find shortest path to visit 26 cities.
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two...
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two different classes in the program being made as objects in the main (Bunny class and Dog Class). ask the user "what kind of animal would you like to sort?" user chooses the type of animal (bunny or dog), then you ask the user how big would you like the array of animals to be (maximum array size = 20)? and then you ask what...
Problem five: Design an algorithm called which searches for a target t in the sorted subarray...
Problem five: Design an algorithm called which searches for a target t in the sorted subarray , returning the appropriate index if t is found, and 0 if t is not found. TenarySearch will work by dividing the input array into three subarrays of approximately equal length, and calling itself recursively on each subarray. a. Write pseudo-code for TernarySearch. b. Write down the recurrence for the run time of TernarySearch and find a tight asymptotic bound on its solution.
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT