Floor of every element in same array
Given an array of integers, find the closest smaller or same element for every element. If all elements are greater for an element, then print -1. We may assume that the array has at least two elements.
Examples:
Input : arr[] = {10, 5, 11, 10, 20, 12}
Output : 10 -1 10 10 12 11
Note that there are multiple occurrences of 10, so floor of 10 is 10 itself.Input : arr[] = {6, 11, 7, 8, 20, 12}
Output : -1 8 6 7 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 greater element. Time complexity of this solution is O(n*n)
A better solution is to sort the array and create a sorted copy, then do a binary search for floor. We traverse the array, for every element we search for the first occurrence of an element that is greater than or equal to given element. Once we find such an element, we check if the next of it is also the same, if yes, then there are multiple occurrences of the element, so we print the same element as output. Otherwise, we print previous element in the sorted array. In C++, lower_bound() returns iterator to the first greater or equal element in a sorted array.
Implementation:
C++
// C++ implementation of efficient algorithm to find // floor of every element #include <bits/stdc++.h> using namespace std; // Prints greater elements on left side of every element void printPrevGreater( int arr[], int n) { // Create a sorted copy of arr[] vector< int > v(arr, arr + n); sort(v.begin(), v.end()); // Traverse through arr[] and do binary search for // every element. for ( int i = 0; i < n; i++) { // Floor of first element is -1 if there is only // one occurrence of it. if (arr[i] == v[0]) { (arr[i] == v[1]) ? cout << arr[i] : cout << -1; cout << " " ; continue ; } // Find the first element that is greater than or // or equal to given element auto it = lower_bound(v.begin(), v.end(), arr[i]); // If next element is also same, then there // are multiple occurrences, so print it if (it != v.end() && *(it + 1) == arr[i]) cout << arr[i] << " " ; // Otherwise print previous element else cout << *(it - 1) << " " ; } } /* Driver program to test insertion sort */ int main() { int arr[] = { 6, 11, 7, 8, 20, 12 }; int n = sizeof (arr) / sizeof (arr[0]); printPrevGreater(arr, n); return 0; } |
Java
// Java implementation of efficient algorithm to find floor // of every element import java.io.*; import java.util.*; class GFG { // Function to count the occurences of a target number. static int count( int [] arr, int target) { int count = 0 ; for ( int i = 0 ; i < arr.length; i++) { if (arr[i] == target) { count++; } } return count; } // Function to find index of an element static int index( int [] arr, int target) { int index = - 1 ; for ( int i = 0 ; i < arr.length; i++) { if (arr[i] == target) { return i; } } return index; } // Prints greater elements on left // side of every element static void printPrevGreater( int [] arr, int n) { // Create a sorted copy of arr int [] v = new int [n]; for ( int i = 0 ; i < n; i++) { v[i] = arr[i]; } Arrays.sort(v); int it = 0 ; // Traverse through arr[] and do // binary search for every element. for ( int i = 0 ; i < n; i++) { // Floor of first element is -1 if // there is only one occurrence of it. if (arr[i] == v[ 0 ]) { System.out.print( ((arr[i] == v[ 1 ]) ? arr[i] : - 1 ) + " " ); continue ; } // Find the first element that is greater // than or or equal to given element if (count(arr, arr[i]) > 0 ) { it = v[index(v, arr[i])]; } else { it = v[n - 1 ]; } // If next element is also same, then there // are multiple occurrences, so print it if (it != v[n - 1 ] && v[index(v, it) + 1 ] == arr[i]) { System.out.print(arr[i] + " " ); } // Otherwise print previous element else { System.out.print(v[index(v, it) - 1 ] + " " ); } } } public static void main(String[] args) { int [] arr = { 6 , 11 , 7 , 8 , 20 , 12 }; int n = arr.length; printPrevGreater(arr, n); } } // This code is contributed by lokeshmvs21. |
Python3
# Python3 implementation of efficient # algorithm to find floor of every element # Prints greater elements on left # side of every element def printPrevGreater(arr, n) : # Create a sorted copy of arr v = arr.copy() v.sort() # Traverse through arr[] and do # binary search for every element. for i in range (n) : # Floor of first element is -1 if # there is only one occurrence of it. if (arr[i] = = v[ 0 ]) : if (arr[i] = = v[ 1 ]) : print (arr[i], end = " " ) else : print ( - 1 , end = " " ) continue # Find the first element that is greater # than or or equal to given element if v.count(arr[i]) > 0 : it = v[v.index(arr[i])] else : it = v[n - 1 ] # If next element is also same, then there # are multiple occurrences, so print it if (it ! = v[n - 1 ] and v[v.index(it) + 1 ] = = arr[i]) : print (arr[i], end = " " ) # Otherwise print previous element else : print (v[v.index(it) - 1 ], end = " " ) # Driver Code if __name__ = = "__main__" : arr = [ 6 , 11 , 7 , 8 , 20 , 12 ] n = len (arr) printPrevGreater(arr, n) # This code is contributed by Ryuga |
C#
// C# implementation of efficient algorithm to find floor // of every element using System; using System.Collections; public class GFG { // Function to count the occurences of a target number. static int count( int [] arr, int target) { int count = 0; for ( int i = 0; i < arr.Length; i++) { if (arr[i] == target) { count++; } } return count; } // Function to find index of an element static int index( int [] arr, int target) { int index = -1; for ( int i = 0; i < arr.Length; i++) { if (arr[i] == target) { return i; } } return index; } // Prints greater elements on left // side of every element static void printPrevGreater( int [] arr, int n) { // Create a sorted copy of arr int [] v = new int [n]; for ( int i = 0; i < n; i++) { v[i] = arr[i]; } Array.Sort(v); int it = 0; // Traverse through arr[] and do // binary search for every element. for ( int i = 0; i < n; i++) { // Floor of first element is -1 if // there is only one occurrence of it. if (arr[i] == v[0]) { Console.Write( ((arr[i] == v[1]) ? arr[i] : -1) + " " ); continue ; } // Find the first element that is greater // than or equal to given element if (count(arr, arr[i]) > 0) { it = v[index(v, arr[i])]; } else { it = v[n - 1]; } // If next element is also same, then there // are multiple occurrences, so print it if (it != v[n - 1] && v[index(v, it) + 1] == arr[i]) { Console.Write(arr[i] + " " ); } // Otherwise print previous element else { Console.Write(v[index(v, it) - 1] + " " ); } } } static public void Main() { // Code int [] arr = { 6, 11, 7, 8, 20, 12 }; int n = arr.Length; printPrevGreater(arr, n); } } // This code is contributed by lokeshmvs21. |
Javascript
<script> // JavaScript implementation of efficient algorithm to find // floor of every element // Prints greater elements on left side of every element function printPrevGreater(arr, n) { // Create a sorted copy of arr[] let v = [...arr] v.sort((a, b) => a - b); // Traverse through arr[] and do binary search for // every element. for (let i = 0; i < n; i++) { // Floor of first element is -1 if there is only // one occurrence of it. if (arr[i] == v[0]) { (arr[i] == v[1]) ? document.write(arr[i]) : document.write(-1); document.write( " " ); continue ; } // Find the first element that is greater than or // or equal to given element if (v.includes(arr[i])) it = v[v.indexOf(arr[i])] else it = v[n - 1] // If next element is also same, then there // are multiple occurrences, so print it if (it != v[n - 1] && (v[v.indexOf(it) + 1] == arr[i])) document.write(arr[i] + " " ); // Otherwise print previous element else document.write(v[v.indexOf(it) - 1] + " " ); } } function lower_bound(arr, val){ } /* Driver program to test insertion sort */ let arr = [ 6, 11, 7, 8, 20, 12 ]; let n = arr.length; printPrevGreater(arr, n); // This code is contributed by _saurabh_jaiswal </script> |
-1 8 6 7 12 11
Complexity Analysis:
- Time Complexity: O(n Log n)
- Auxiliary Space: O(n)
Please Login to comment...