Fraudulent Activity Notifications


Problem Statement :


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 2 x 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 d and a client's total daily expenditures for a period of n 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)

Function Description

Complete the function activityNotifications in the editor below. It must return an integer representing the number of client notifications.

activityNotifications has the following parameter(s):

expenditure: an array of integers representing daily expenditures
d: an integer, the lookback days for median spending


Input Format

The first line contains two space-separated integers n and d, the number of days of transaction data, and the number of trailing days' data used to calculate median spending.
The second line contains n space-separated non-negative integers where each integer i denotes expenditure[ i ].


Constraints

1   <=  n  <= 2* 10^5
1    <=  d  <=  n
0  <=   expenditure[ i ]  <=  200


Output Format

Print an integer denoting the total number of times the client receives a notification over a period of  n days.



Solution :



title-img


                            Solution in C :

In  C :




#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int GetMedian(int f[], int d);

int main() {
    int n, d;
    scanf("%d %d", &n, &d);
    int s[n];
    int i;
    for(i = 0; i < n; i++) {
        scanf("%d", &s[i]);
    }
    
    int frequency[201];
    for(i = 0; i < 201; i++) {
        frequency[i] = 0;
    }
    
    for(i = 0; i < d; i++) {
        frequency[s[i]]++;
    }
    
    int m[n];
    m[d] = GetMedian(frequency, d);
    
    for(i = d + 1; i < n; i++) {
        frequency[s[i-1]]++;
        frequency[s[i-d-1]]--;
        m[i] = GetMedian(frequency, d);
    }
    int not = 0;
    for(i = d; i < n; i++) {
        if(s[i] >= m[i]){
            not++;
        }
    }
    printf("%d", not);
    return 0;
}

int GetMedian(int f[], int d) {
    int Median;
    if(d % 2 == 0) {
        int mid = d/2;
        int cF = 0;
        for(int i = 0; i < 201; i ++) {
            cF += f[i];
            if(cF > mid) {
                Median = 2*i;
                break;
            }
            else if(cF == mid) {
                for(int j = i + 1; j < 201; j++) {
                    if(f[j] != 0) {
                        //printf("\n%d %d\n", i, j);
                        Median = i + j;
                        break;
                    }
                }
                break;
            }
        }
    }
    else {
        int mid = d/2 + 1;
        int cF = 0;
        for(int i = 0; i < 201; i++) {
            cF += f[i];
            if(cF >= mid) {
                Median = 2*i;
                break;
            }
        }
    }
    //printf("%d", Median);
    return Median;
}
                        


                        Solution in C++ :

In C ++ ;





#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

#define MAXE 210

int A[200010];
int F[MAXE];

int median2(int D) {
  int p = 0;
  for (int i = 0; i < MAXE; i++) {
    p += F[i];
    if (p * 2 > D) {
      return 2 * i;
    } else if (p * 2 == D) {
      for (int j = i + 1; ; j++) {
        if (F[j]) {
          return i + j;
        }
      }
    }
  }
  return -1;
}

int main() {
  int N, D;
  cin >> N >> D;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int result = 0;
  for (int i = 0; i < N; i++) {
    if (i >= D) { 
      if (A[i] >= median2(D)) {
        ++result;
      }
      F[A[i - D]]--;
    }
    F[A[i]]++;
  }
  cout << result << endl;
  return 0;
}
                    


                        Solution in Java :

In Java :




import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

   public static double median(int[]b){

		int mid=(int)b.length/2;
		if(b.length%2==1){
			return b[mid];
		}
		else{
			return (b[mid-1]+b[mid])/2.0;
		}
	}
	
	public static int BS(int[] arr,int in, int start,int end){
		
		 if(start>end){
				return -1;
				}
		
			int mid=start+(end-start)/2;
			if(arr[mid]==in){
		return mid;
			}
			
			else if(arr[mid]<in){
				return BS(arr,in,mid+1,end);
			}
			else {
				return BS(arr,in,start,mid-1);
			}	
		}
	public static void replace(int[]b,int x, int y){
		//for(int i=0;i<b.length;i++)
			//System.out.print(b[i]);
		int ind=BS(b,x,0,b.length-1);
		//System.out.println(x);
		b[ind]=y;
		if((ind==0&&b[ind]<=b[ind+1])||(ind==b.length-1&&b[ind]>=b[ind-1])){
			return;
		}
		else if(b[ind]>b[ind+1]){
			int temp=0;
			while(b[ind]>b[ind+1]){

				temp=b[ind];
				b[ind]=b[ind+1];
				b[ind+1]=temp;
				if(ind==b.length-2){
					
					break;
				}
				ind++;
			}
		}
		else if(b[ind]<b[ind-1]){
			int temp=0;
			while(b[ind]<b[ind-1]){
				if(ind==1){
					break;
				}
				temp=b[ind];
				b[ind]=b[ind-1];
				b[ind-1]=temp;
				if(ind==1){
					break;
				}
				ind--;
			}
		}
	}

    public static void main(String[] args) throws IOException {
    	 Scanner scan=new Scanner(System.in);
    	 int n=scan.nextInt();
    	 int d=scan.nextInt();
    	int a[]=new int[n];
    	
for(int i=0;i<n;i++){
	a[i]=scan.nextInt();
}
int b[]=new int[d];
int not=0;

for(int i=0;i<d;i++){
	b[i]=a[i];
} 
Arrays.sort(b);
for(int i=d;i<n;i++){
if(a[i]>=2*median(b)){

	not=not+1;
}
replace(b,a[i-d],a[i]);
}

System.out.println(not);

    }
}
                    


                        Solution in Python : 
                            
In Python3 :





n,d = map(int,input().strip().split())
exp = list(map(int, input().strip().split()))
count = {}
i = 0
while i<d:
    x = exp[i]
    if x not in count:
        count[x] = 0
    count[x] += 1
    i+=1

def median():
    global count
    cnt,midval = 0,0
    for i in range(201):
        if i in count:
            cnt += count[i]
            if cnt >= d//2:
                midval = i
                break
    secondval = midval
    
    if cnt == d//2:
        x = midval + 1
        while x not in count or count[x] == 0:
            x+=1
        secondval = x
    if d%2==1:
        return secondval
    return (midval + secondval)/2
        
ans = 0
i-=1
while i<n-1:
    md = median()
    if exp[i+1] >= 2*md:
        ans += 1
    count[exp[i+1-d]]-=1
    x = exp[i+1]
    if x not in count:
        count[x] = 0
    count[x]+=1
    i+=1
    
print (ans)
                    


View More Similar Problems

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →