-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFraudulentActivityNotifications.java
More file actions
106 lines (83 loc) · 3.33 KB
/
Copy pathFraudulentActivityNotifications.java
File metadata and controls
106 lines (83 loc) · 3.33 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
/*
HackerLand National Bank has a simple policy for warning clients about possible fraudulent account activity.
If the amount spent by a client on a particular day is greater than or equal to the client's median spending for a trailing number of days,
they send the client a notification about potential fraud. T
he bank doesn't send the client any notifications until they have at least that trailing number of prior days' transaction data.
Given the number of trailing days and a client's total daily expenditures for a period of days,
find and print the number of times the client will receive a notification over all days.
*/
public class FraudulentActivityNotifications {
// Complete the activityNotifications function below.
static int activityNotifications(int[] expenditure, int d) {
int start, end;
int count = 0;
double media = 0d;
int [] temp = new int [201];
/* Initialize with zeros */
for(int x = 0; x <= 200; x++) {
temp[ x ] = 0;
}
/* First section */
for(int x = 0; x < d; x++) {
temp[ expenditure[ x ] ] ++;
}
for(int x = 0; x < expenditure.length - d; x++) {
start = x;
end = start + d;
if( x > 0) {
temp[ expenditure[ x - 1 ] ] --;
temp[ expenditure[ end - 1 ] ] ++;
}
media = getMedian(expenditure, d, temp) * 2;
count = expenditure[ end ] >= media ? count + 1 : count;
}
return count;
}
/*
* Get the median of a set of elements
* Iterate to get the element in the mid position of an ordered list
*/
private static double getMedian(int[] expenditure, int d, int[] temp) {
int div = d / 2;
int count = 0;
int tmp = 0;
int previous = -1;
for(int x = 0; x <= 200; x++) {
tmp = temp[ x ];
while( tmp-- > 0 ) {
count ++;
if( count > div) {
return d % 2 == 0 ? (x + previous) / 2.0 : x;
}
previous = x;
}
}
return 1;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] nd = scanner.nextLine().split(" ");
int n = Integer.parseInt(nd[0]);
int d = Integer.parseInt(nd[1]);
int[] expenditure = new int[n];
String[] expenditureItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
int expenditureItem = Integer.parseInt(expenditureItems[i]);
expenditure[i] = expenditureItem;
}
int result = activityNotifications(expenditure, d);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}