Breaking

Tuesday, 3 October 2017

Fraudulent Activity Notifications

   Fraudulent Activity Notifications


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 the last  days, they send the client a notification about potential fraud. The bank doesn't send the client any notifications until they have at least  prior days of transaction data.
Given the value of  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.
Note: The median of a list of numbers can be found by arranging all the numbers from smallest to greatest. If there is an odd number of numbers, the middle one is picked. If there is an even number of numbers, median is then defined to be the average of the two middle values. (Wikipedia)
Input Format
The first line contains two space-separated integers denoting the respective values of  (the number of days there is transaction data for) and  (the number of prior days the bank uses to calculate median spending).
The second line contains  space-separated non-negative integers where each integer  denotes (i.e., the client's total  for day ).
Constraints
Output Format
Print an integer denoting the total number of times the client receives a notification over a period of  days.
Sample Input 0
9 5
2 3 4 2 3 6 8 4 5
Sample Output 0
2
Explanation 0
We must determine the total number of  the client receives over a period of  days. For the first five days, the customer receives no notifications because the bank has insufficient transaction data and .
On the sixth day, the bank has  days of prior transaction data, , and  dollars. The client spends  dollars, which triggers a notification because . Thus, .
On the seventh day, the bank has  days of prior transaction data, , and  dollars. The client spends  dollars, which triggers a notification because . Thus, .
On the eighth day, the bank has  days of prior transaction data, , and  dollars. The client spends  dollars, which does not trigger a notification because .
On the ninth day, the bank has  days of prior transaction data, , and a transaction median of dollars. The client spends  dollars, which does not trigger a notification because .
We then print the final value of  (which is ) as our answer.
Sample Input 1
5 4
1 2 3 4 4
Sample Output 1
0

Code:


public class FraudulentActivityNotifications {
    private static final int MAX_EXPENDITURE = 200;
    private static int solve(int[] a, int n, int d) {
        int ans = 0;
        int[] histogram = new int[MAX_EXPENDITURE + 1];
        // Carry a histogram of the last d expenditures
        for (int i = 0; i < d; i++) {
            histogram[a[i]] = histogram[a[i]] + 1;
        }
        for (int i = d; i < n; i++) {
            int cursor = 0;
            int currentAmount = a[i];
            double median = 0;
            int left = -1;
            int right = -1;
            for (int e = 0; e <= MAX_EXPENDITURE; e++) {
                cursor += histogram[e];
                if (d % 2 == 1) {
                    // Odd -> Pick middle one for median
                    if (cursor >= d / 2 + 1) {
                        median = e;
                        break;
                    }
                } else {
                    // Even -> Pick average of two middle values for median
                    if (cursor == d / 2) {
                        left = e;
                    }
                    if (cursor > d / 2 && left != -1) {
                        right = e;
                        median = (left + right) / 2.0;
                        break;
                    }
                    if (cursor > d / 2 && left == -1) {
                        median = e;
                        break;
                    }
                }
            }
            if (currentAmount >= 2 * median) {
                ans++;
            }
            // Update histogram: slide window 1 index to right
            histogram[a[i - d]] = histogram[a[i - d]] - 1;
            histogram[a[i]] = histogram[a[i]] + 1;
        }
        return ans;
    }
    public static void main(String[] args) throws FileNotFoundException {

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int d = scanner.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        System.out.println(solve(a, n, d));
    }
}

1 comment:

  1. Harrah's Philadelphia Casino & Racetrack - MapyRO
    Harrah's Philadelphia 천안 출장마사지 Casino & Racetrack. 777 Harrah's Blvd, Philadelphia, PA 제주 출장안마 The Chester-West Chester-Philadelphia International 파주 출장마사지 Airport is a 안양 출장마사지 short 나주 출장안마 walk from the

    ReplyDelete

Like