-
Notifications
You must be signed in to change notification settings - Fork 1
/
2June(VI).java
65 lines (48 loc) · 1.21 KB
/
2June(VI).java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class NumbersSmallerThanCurrent {
// Comments mai smjhe bhee diya kr time bachaga
static int[] approach1(int[] nums){
int n = nums.length;
// int i = 0;
// int j = n-1;
int[] arr = new int[n];
for(int i = 0 ; i<n;i++){
int count = 0;
for(int j = 0; j<n; j++){
if(nums[j]<nums[i]){
count++;
}
}
arr[i] = count;
}
return arr;
}
static int[] approach2(int[] nums){
int n = nums.length;
int[] arr = new int[n];
int i =0;
int j = 0;
int count = 0;
while(j<n){
if(nums[i]<nums[j]){
count+=1;
}
if(i==n-1){
arr[j]=count;
j++;
i=0;
count = 0;
continue;
}
i++;
}
return arr;
}
public static void main(String[] args) {
int[] arr = {8,1,2,2,3};
// O/P = {4,0,1,1,3}
int[] result = approach2(arr);
for(int i : result){
System.out.print(i+",");
}
}
}