|
| 1 | +## Smallest greater elements in whole array |
| 2 | +Given an array A of N length. We need to calculate the next greater element for each element in a given array. If the next greater element is not available in a given array then we need to fill -10000000 at that index place. [🔗Goto](https://practice.geeksforgeeks.org/problems/smallest-greater-elements-in-whole-array2751/1) |
| 3 | + |
| 4 | +``` |
| 5 | +Example 1: |
| 6 | +
|
| 7 | +Input : arr[] = {13, 6, 7, 12} |
| 8 | +Output : _ 7 12 13 |
| 9 | +Explanation: |
| 10 | +Here, at index 0, 13 is the greatest value |
| 11 | +in given array and no other array element |
| 12 | +is greater from 13. So at index 0 we fill |
| 13 | +'-10000000'. |
| 14 | +
|
| 15 | +Example 2: |
| 16 | +
|
| 17 | +Input : arr[] = {6, 3, 9, 8, 10, 2, 1, 15, 7} |
| 18 | +Output : 7 6 10 9 15 3 2 _ 8 |
| 19 | +Explanation: Here, at index 7, 15 is the greatest |
| 20 | +value in given array and no other array element is |
| 21 | +greater from 15. So at index 7 we fill '-10000000'. |
| 22 | +``` |
| 23 | +<details> |
| 24 | +<summary>Full Code</summary> |
| 25 | + |
| 26 | +```java |
| 27 | +import java.io.*; |
| 28 | +import java.util.*; |
| 29 | + |
| 30 | + |
| 31 | +class Array { |
| 32 | + |
| 33 | + // Driver code |
| 34 | + public static void main (String[] args) throws IOException{ |
| 35 | + // Taking input using buffered reader |
| 36 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 37 | + |
| 38 | + int testcases = Integer.parseInt(br.readLine()); |
| 39 | + |
| 40 | + // looping through all testcases |
| 41 | + while(testcases-- > 0){ |
| 42 | + int sizeOfArray = Integer.parseInt(br.readLine()); |
| 43 | + int arr [] = new int[sizeOfArray]; |
| 44 | + |
| 45 | + String line = br.readLine(); |
| 46 | + String[] elements = line.trim().split("\\s+"); |
| 47 | + for(int i = 0;i<sizeOfArray;i++){ |
| 48 | + arr[i] = Integer.parseInt(elements[i]); |
| 49 | + } |
| 50 | + |
| 51 | + Complete obj = new Complete(); |
| 52 | + arr = obj.greaterElement(arr, sizeOfArray); |
| 53 | + for(int i=0; i< sizeOfArray;i++){ |
| 54 | + if(arr[i] == -10000000) |
| 55 | + System.out.print("_ "); |
| 56 | + else |
| 57 | + System.out.print(arr[i] + " "); |
| 58 | + } |
| 59 | + System.out.println(); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +// } Driver Code Ends |
| 66 | + |
| 67 | + |
| 68 | +//User function Template for Java |
| 69 | + |
| 70 | +//User function Template for Java |
| 71 | + |
| 72 | +//User function Template for Java |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +class Complete{ |
| 77 | + |
| 78 | + |
| 79 | + // Function for finding maximum and value pair |
| 80 | + public static int[] greaterElement (int arr[], int n) { |
| 81 | + Integer[] nums = Arrays.stream( arr ).boxed().toArray( Integer[]::new ); |
| 82 | + TreeSet<Integer> set = new TreeSet<>(Arrays.asList(nums)); |
| 83 | + int[] res = new int[n]; |
| 84 | + for(int i=0; i< n;i++){ |
| 85 | + if(set.higher(arr[i]) != null){ |
| 86 | + res[i] = set.higher(arr[i]); |
| 87 | + } |
| 88 | + else{ |
| 89 | + res[i] = -10000000; |
| 90 | + } |
| 91 | + } |
| 92 | + return res; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | +} |
| 97 | +``` |
| 98 | +</details> |
| 99 | + |
| 100 | +```java |
| 101 | +class Complete{ |
| 102 | + |
| 103 | + |
| 104 | + // Function for finding maximum and value pair |
| 105 | + public static int[] greaterElement (int arr[], int n) { |
| 106 | + Integer[] nums = Arrays.stream( arr ).boxed().toArray( Integer[]::new ); |
| 107 | + TreeSet<Integer> set = new TreeSet<>(Arrays.asList(nums)); |
| 108 | + int[] res = new int[n]; |
| 109 | + for(int i=0; i< n;i++){ |
| 110 | + if(set.higher(arr[i]) != null){ |
| 111 | + res[i] = set.higher(arr[i]); |
| 112 | + } |
| 113 | + else{ |
| 114 | + res[i] = -10000000; |
| 115 | + } |
| 116 | + } |
| 117 | + return res; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +``` |
0 commit comments