Find closest value for every element in array
Given an array of integers, find the closest element for every element.
Examples:
Input : arr[] = {10, 5, 11, 6, 20, 12}
Output : 6, -1, 10, 5, 12, 11Input : arr[] = {10, 5, 11, 10, 20, 12}
Output : 5 -1 10 5 12 11
A simple solution is to run two nested loops. We pick an outer element one by one. For every picked element, we traverse remaining array and find closest element. Time complexity of this solution is O(n*n)
An efficient solution is to use Self Balancing BST (Implemented as set in C++ and TreeSet in Java). In a Self Balancing BST, we can do both insert and closest greater operations in O(Log n) time.
// Java program to demonstrate insertions in TreeSet import java.util.*; class TreeSetDemo { public static void closestGreater(int[] arr) { if (arr.length == -1) { System.out.print(-1 + " "); return; } // Insert all array elements into a TreeMap. // A TreeMap value indicates whether an element // appears once or more. TreeMap<Integer, Boolean> tm = new TreeMap<Integer, Boolean>(); for (int i = 0; i < arr.length; i++) { // A value "True" means that the key // appears more than once. if (tm.containsKey(arr[i])) tm.put(arr[i], true); else tm.put(arr[i], false); } // Find smallest greater element for every // array element for (int i = 0; i < arr.length; i++) { // If there are multiple occurrences if (tm.get(arr[i]) == true) { System.out.print(arr[i] + " "); continue; } // If element appears only once Integer greater = tm.higherKey(arr[i]); Integer lower = tm.lowerKey(arr[i]); if (greater == null) System.out.print(lower + " "); else if (lower == null) System.out.print(greater + " "); else { int d1 = greater - arr[i]; int d2 = arr[i] - lower; if (d1 > d2) System.out.print(lower + " "); else System.out.print(greater + " "); } } } public static void main(String[] args) { int[] arr = { 10, 5, 11, 6, 20, 12, 10 }; closestGreater(arr); } } |
10 6 12 5 12 11 10
Exercise : Another efficient solution is to use sorting that also works in O(n Log n) time. Write complete algorithm and code for sorting based solution.
Recommended Posts:
- Find closest smaller value for every element in array
- Find closest greater value for every element in array
- Closest greater element for every array element from another array
- Closest greater or same value on left side for every element in array
- Find the closest element in Binary Search Tree
- Find the closest element in Binary Search Tree | Space Efficient Method
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Find the Sub-array with sum closest to 0
- Find closest number in array
- Find last element after deleting every second element in array of n integers
- Distance of closest zero to every element
- Find an element in array such that sum of left array is equal to sum of right array
- Find element in array that divides all array elements
- Find if array has an element whose value is half of array sum
- Find Second largest element in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



