Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java get the closest pair to a given sum in two arrays

public int[] pairToTarget(int[] a, int[] b, int target) {

		Map<Integer, int[]> map = new TreeMap<>();

		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < b.length; j++) {
				int[] pair = new int[2];
				pair[0] = a[i];
				pair[1] = b[j];
				map.put(Math.abs(target - (a[i] + b[j])), pair);
			}
		}
		List<Integer> l1 = new ArrayList<>(map.keySet());

		return map.get(l1.get(0));
	}
Comment

PREVIOUS NEXT
Code Example
Java :: franchiseRulesTemp 
Java :: android paint drawtext multiline 
Java :: replacing string with dynamic avalues java 
Java :: jadoh meaning 
Java :: api to accept a csv file spring boot 
Java :: fail method java 
Java :: system.out.println int in java concatenate 
Java :: String in Queue 
Java :: Java Default capacity and load factor 
Java :: 2 multiples 
Java :: regex plit string with dash or undescore java 
Java :: camera for barcode android studio 
Java :: Kotlin Toast : error-none-of-the-following-functions-can-be-called-with-the-arguments-supplied 
Java :: java writing an object 
Java :: object class of java 
Java :: kivy menu bar 
Java :: OpenCV mat to float bytebuffer java 
Java :: Algorithms - Max value 
Java :: rules for naming variables in java 
Java :: telegraf-agent-configuration 
Java :: adding entity to calenderfx 
Java :: trivers json node as node type 
Java :: TYPE_INT_ARGB 
Java :: java list sort comparator date descending lambda 
Java :: Android popBackStack to specific fragment 
Java :: java gui lookandfeel flatlaf background color 
Java :: how to extract a set of binary strings in java 
Java :: ezra margolin 
Java :: ban vs sri 
Java :: java inetaddress workaround how to get localhost secure 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =