
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Find the closest pair from two sorted arrays
To find the closest pair from two sorted array, the Java code is as follows −
Example
public class Demo { void closest_pair(int my_arr_1[], int my_arr_2[], int arr_1_len, int arr_2_len, int sum){ int diff = Integer.MAX_VALUE; int result_l = 0, result_r = 0; int l = 0, r = arr_2_len-1; while (l<arr_1_len && r>=0){ if (Math.abs(my_arr_1[l] + my_arr_2[r] - sum) < diff){ result_l = l; result_r = r; diff = Math.abs(my_arr_1[l] + my_arr_2[r] - result_l); } if (my_arr_1[l] + my_arr_2[r] > result_l) r--; else l++; } System.out.print("The closest pair that matches two arrays is [" + my_arr_1[result_l] + ", " + my_arr_2[result_r] + "]"); } public static void main(String args[]){ Demo my_ob = new Demo(); int my_arr_1[] = {56, 78, 99, 11}; int my_arr_2[] = {33, 12, 69, 87}; int arr_1_len = my_arr_1.length; int arr_2_len = my_arr_2.length; int val = 79; my_ob.closest_pair(my_arr_1, my_arr_2, arr_1_len, arr_2_len, val); } }
Output
The closest pair that matches two arrays is [56, 33]
A class named Demo contains a function named ‘closest_pair’, that iterates through both the arrays and checks to see which sum adds up to a number that is very near to a number previously specified. This pair from the array is returned as output. In the main function, a new instance of the Demo class is defined, the arrays are defined, and their lengths are assigned to two variables respectively. The function is called by passing the arrays, their lengths, and the value. The relevant message is displayed on the console.
- Related Articles
- Python Program for Find the closest pair from two sorted arrays
- Find the closest pair from two sorted arrays in c++
- Find three closest elements from given three sorted arrays in C++
- Java program to find common elements in three sorted arrays
- Merge two sorted arrays in Java
- Golang program to find intersection of two sorted arrays using two pointer approach
- Java program to create a sorted merged array of two unsorted arrays
- JavaScript Program for find common elements in two sorted arrays
- Print uncommon elements from two sorted arrays
- C++ Program to find the median of two sorted arrays using binary search approach
- C# program to merge two sorted arrays into one
- C++ Program to Find Closest Pair of Points in an Array
- Golang Program to find the common elements from two arrays
- Golang Program to find the uncommon elements from two arrays
- Golang Program to find the Distinct elements from two arrays

Advertisements