Cerinta completa

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. The 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, determine the number of times the client will receive a notification over all days.

Example

On the first three days, they just collect spending data. At day , trailing expenditures are . The median is and the day’s expenditure is . Because , there will be a notice. The next day, trailing expenditures are and the expenditures are . This is less than so no notice will be sent. Over the period, there was one notice sent.

Note: The median of a list of numbers can be found by first sorting the numbers ascending. If there is an odd number of values, the middle one is picked. If there is an even number of values, the median is then defined to be the average of the two middle values. (Wikipedia)

Function Description

Complete the function activityNotifications in the editor below.

activityNotifications has the following parameter(s):

  • int expenditure[n]: daily expenditures
  • int d: the lookback days for median spending

Returns

  • int: the number of notices sent

Input Format

The first line contains two space-separated integers and , the number of days of transaction data, and the number of trailing days’ data used to calculate median spending respectively.
The second line contains space-separated non-negative integers where each integer denotes .

Constraints

Output Format

Sample Input 0

STDIN               Function
-----               --------
9 5                 expenditure[] size n =9, d = 5
2 3 4 2 3 6 8 4 5   expenditure = [2, 3, 4, 2, 3, 6, 8, 4, 5]

Sample Output 0

2

Explanation 0

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: .

On the sixth day, the bank has days of prior transaction data, , and dollars. The client spends dollars, which triggers a notification because : .

On the seventh day, the bank has days of prior transaction data, , and dollars. The client spends dollars, which triggers a notification because : .

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 : .

Sample Input 1

5 4
1 2 3 4 4

Sample Output 1

0

There are days of data required so the first day a notice might go out is day . Our trailing expenditures are with a median of The client spends which is less than so no notification is sent.


Limbajul de programare folosit: java8

Cod:

//Problem: https://www.hackerrank.com/challenges/fraudulent-activity-notifications
//Java 8
/*
Initial Thoughts: We can use an array to sort our transactions
                  using counting sort, since there is a small
                  range of different transactions ranging from
                  0->200. Then to get our median we can iterate
                  over our sorted array decrementing a counter
                  variable set to the d and when it hits 0, we
                  know we have found our median. In the case of
                  an even element median, we just need to keep
                  track of the current element and the prev 
                  element and then we can average them to get 
                  our median
                  
Time Complexity: O(n^2) //For each transaction we calculate median, and in worse case median takes n time
Space Complexity: O(n) //Our queue is at most d which is bounded by n
*/
import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int d = input.nextInt();
        int notifications = 0;
        Queue<Integer> queue = new LinkedList<>();
        int[] pastActivity = new int[201];
        
        //Wait for d transactions before any notifications
        for(int i = 0; i < d; i++)
        {
            int transaction = input.nextInt();
            queue.offer(transaction);
            pastActivity[transaction] = pastActivity[transaction]+1;
        }
            
        for(int i = 0; i < n-d; i++)
        {
            int newTransaction = input.nextInt();
            
            //Check if fraudulent activity may have occurred
            if(newTransaction >= (2* median(pastActivity, d)))
                notifications++;
                
            //Remove the oldest transaction
            int oldestTransaction = queue.poll();
            pastActivity[oldestTransaction] = pastActivity[oldestTransaction]-1;
            
            //Add the new transaction
            queue.offer(newTransaction);
            pastActivity[newTransaction] = pastActivity[newTransaction]+1;
        }
        
        System.out.println(notifications);
    }
    
    static double median(int[] array, int elements)
    {
        int index = 0;
        
        if(elements % 2 == 0)//Find median of even # of elements
        {
            int counter = (elements / 2);

            while(counter > 0)
            {                
                counter -= array[index];
                index++;
            }
            index--;//Remove extra iteration
            if(counter <= -1)//This index covers both medians
                return index;
            else//(counter == 0) We need to find the next median index 
            {
                int firstIndex = index;
                int secondIndex = index+1;
                while(array[secondIndex] == 0)//Find next non-zero transaction
                {
                    secondIndex++;
                }
                return (double) (firstIndex + secondIndex) / 2.0;//Calculate the average of middle two elements
            }
        }
        else//Find median of odd # of elements
        {
            int counter = (elements / 2);

            while(counter >= 0)
            {
                counter -= array[index]; index++;
            }
            return (double) index-1;
        }
    }
    
    
    static void printArray(int[] array)
    {
        System.out.println("Array");
        for(int i = 0; i < array.length; i++)
        {
            if(array[i] > 0)
                System.out.println(i+" : "+array[i]);
        }
    }
}

Scor obtinut: 1.0

Submission ID: 464602699

Link challenge: https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem

Fraudulent Activity Notifications