-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathA_-_Books.java
More file actions
47 lines (46 loc) · 1.78 KB
/
Copy pathA_-_Books.java
File metadata and controls
47 lines (46 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public static void main(String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
int t,n;
t = sc.nextInt();
while (t-- > 0) {
Map < Integer, Integer > map = new HashMap < > ();
n=sc.nextInt();
int[] a=sc.readArray(n);
int[] b=new int[n];
for(int i=0;i<n-1;i++){
int count=0;
for(int j=i+1;j<n;j++){
if(a[j]>a[i]) count++;
}
b[i]=count;
}
for(int i=0;i<n;i++) System.out.print(b[i]+" ");
sout("");
}
}
// used brute force to count the number greater than current numbers in loop , add them and assign to current index of b array
// there is also another approach to do it in O(n) as follows
/*
public static void main(String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] a = sc.readArray(n);
// Since the list is sorted, we can compute B directly without using a map.
for (int i = 0; i < n; i++) {
int countGreater = 0;
// For each element, count how many elements are strictly greater than it.
// Using the fact that the array is sorted, all elements strictly greater than a[i]
// will be to its right.
if (i < n-1 && a[i] != a[i+1]) { // Optimization: Only compute countGreater if next element is different
countGreater = n - i - 1; // All elements to the right of a[i] are greater if array is sorted.
}
System.out.print(countGreater + " ");
}
System.out.println();
}
}
*/