-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathFraudulentActivityNotifications.cs
More file actions
118 lines (96 loc) · 2.97 KB
/
FraudulentActivityNotifications.cs
File metadata and controls
118 lines (96 loc) · 2.97 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
107
108
109
110
111
112
113
114
115
116
117
118
// Fraudulent Activity Notifications
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
// Complete the activityNotifications function below.
static int activityNotifications(int[] expenditure, int d)
{
int count = 0;
// create freq array as exp <= 200 always
//maintain a queue to find ontgoing and incoming exp
//get median from freq array
var freq = new int[201];
List<int> q = new List<int>();
for (int i = 0; i < expenditure.Length; i++)
{
while (i < d)
{
q.Add(expenditure[i]);
freq[expenditure[i]] = freq[expenditure[i]] + 1;
i++;
}
float median = getMedian(freq, d);
if (expenditure[i] >= 2 * median)
{
count++;
}
// removing outing going element freq
int elem = q[0];
q.RemoveAt(0);
freq[elem] = freq[elem] - 1;
// adding incoming element to freq
q.Add(expenditure[i]);
freq[expenditure[i]] = freq[expenditure[i]] + 1;
}
return count;
}
private static float getMedian(int[] freq, int d)
{
if (d % 2 == 1)
{
int center = 0;
for (int i = 0; i < freq.Length; i++)
{
center = center + freq[i];
if (center > d / 2)
{
return i;
}
}
}
else
{
int count = 0;
int first = -1;
int second = -1;
for (int i = 0; i < freq.Length; i++)
{
count = count + freq[i];
if (count == d / 2)
{
first = i;
}
else if (count > d / 2)
{
if (first < 0) first = i;
second = i;
return ((float)first + (float)second) / 2;
}
}
}
return 0f;
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] nd = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(nd[0]);
int d = Convert.ToInt32(nd[1]);
int[] expenditure = Array.ConvertAll(Console.ReadLine().Split(' '), expenditureTemp => Convert.ToInt32(expenditureTemp))
;
int result = activityNotifications(expenditure, d);
textWriter.WriteLine(result);
textWriter.Flush();
textWriter.Close();
}
}