Fraudulent Activity Notifications

Sort by

recency

|

1184 Discussions

|

  • + 0 comments

    Python solution:

    def activityNotifications(expenditure, d):
        # Write your code here
        notifications=0
        
        current_count=[0 for i in range(max(expenditure)+1)]
        
        for n in expenditure[:d]:
            current_count[n]+=1
        
        for i,e in enumerate(expenditure[d:]):
            
            cum_sum=0
            for j,m in enumerate(current_count):
                cum_sum+=m
                if cum_sum>d//2:
                    median=j
                    break
                elif cum_sum==d/2:
                    for k,l in enumerate(current_count[j+1:]):
                        if l!=0:
                            median=(j+(k+j+1))/2
                            break
                    break
            print(median,e)
            if e>=(2*median):
                notifications+=1
                
            current_count[e]+=1
            current_count[expenditure[i]]-=1
                
                
        return notifications
    
  • + 0 comments

    JS:

    function findIndex(array, element) {
      let firstIndex = 0;
      let lastIndex = array.length - 1;
      let indexOfElement;
    
      while (firstIndex <= lastIndex) {
        const middleIndex = Math.floor((firstIndex + lastIndex) / 2);
        if (array[middleIndex] === element) return middleIndex;
    
        if (array[middleIndex] > element) {
          lastIndex = middleIndex - 1;
          indexOfElement = middleIndex;
        } else {
          firstIndex = middleIndex + 1;
          indexOfElement = firstIndex;
        }
      }
    
      return indexOfElement;
    }
    
    function activityNotifications(expenditure, d) {
      let notifications = 0;
      const sorted = expenditure.slice(0, d).sort((a, b) => a - b);
    
      for (let i = d; i < expenditure.length; i++) {
        const median =
          d % 2 === 1
            ? sorted[Math.floor(d / 2)]
            : (sorted[d / 2 - 1] + sorted[d / 2]) / 2;
        if (expenditure[i] >= 2 * median) notifications += 1;
    
        sorted.splice(findIndex(sorted, expenditure[i - d]), 1);
        sorted.splice(findIndex(sorted, expenditure[i]), 0, expenditure[i]);
      }
    
      return notifications;
    }
    
  • + 0 comments

    Here is a Java solution with sorted arrayList and sliding window:

    public static int activityNotifications(List<Integer> expenditure, int d) {
            var list = new ArrayList<Integer>();
            int notifications = 0;
    
            Function<List<Integer>, Double> medFunc;
            if (d % 2 == 0) {
                medFunc = (arr) -> ((arr.get(d / 2) + arr.get(d / 2 - 1)) / 2.);
            } else {
                medFunc = (arr) -> (double) arr.get(d / 2);
            }
    
            var indexToRemove = 0;
            for (var i = 0; i < expenditure.size(); i++) {
                if (i < d) {
                    add(expenditure.get(i), list);
                } else {
                    double med = medFunc.apply(list);
                    if (med * 2 <= expenditure.get(i)) {
                        notifications++;
                    }
    
                    if (i < expenditure.size() - 1) {
                        add(expenditure.get(i), list);
                        remove(expenditure.get(indexToRemove), list);
                        indexToRemove++;
                    }
                }
            }
    
            return notifications;
        }
    
        public static void add(Integer value, List<Integer> list) {
            int index = Collections.binarySearch(list, value);
    
            // If value is not found, binarySearch returns (-(insertion point) - 1)
            if (index < 0) {
                index = -(index + 1);
            }
    
            list.add(index, value);
        }
    
        public static void remove(Integer value, List<Integer> list) {
            int index = Collections.binarySearch(list, value);
            if (index >= 0) {
                list.remove(index);
            }
        }
    
  • + 0 comments
    • my solution for JS, worked for me on all test-cases
    • with explanation due to JS being very slow on high-load-operations
    function activityNotifications(expenditure, d) {
      // Write your code here
      let notificationCounter = 0;
      const middle = Math.ceil(d / 2);
      
      // sort first trailing days, use this as base for future operations and to calculate the median
      const sortedTrailingDays = expenditure
        .slice(0, d)
        .sort((a, b) => (b < a ? 0 : -1));
    
      for (let i = d; i < expenditure.length; i++) {
        const spentThisDay = expenditure[i];
    
        if (i > d) { // ignore first iteration, trailings days are sorted once before the loop
          /**
           * 2 operations on the sorted array of trailing day expenditures
           *    1. remove the expenditure of the day that dropped out of the trailing days
           *    2. add the expenditure of the previous day to the trailing days at the correct position
           * 
           * this is way faster than sorting the whole array on every iteration 
           * with the js-array-sort method (as done above the for-loop)
           *  -> this way the function was fast enough to succeed all test-cases, 
           *     even in slow-ass single-CPU-javascript :D
           */
          // remove "dropped out" number/day
          sortedTrailingDays.splice(
            sortedTrailingDays.indexOf(expenditure[i - d - 1]),
            1
          );
          // insert "previous-day-expenditure" to sorted array at the correct position
          const spentDayBefore = expenditure[i - 1];
          if (spentDayBefore <= sortedTrailingDays[0]) {
            sortedTrailingDays.unshift(spentDayBefore); // first pos
          } else if (
            spentDayBefore >= sortedTrailingDays[sortedTrailingDays.length - 1]
          ) {
            sortedTrailingDays.push(spentDayBefore);  // last pos
          } else {
            // find first higher number -> insert there
            const idx = sortedTrailingDays.indexOf(
              sortedTrailingDays.find((n) => n > spentDayBefore) // most likely to slow for testcase-5
            );
            sortedTrailingDays.splice(idx, 0, spentDayBefore);
          }
        }
        
        // calculate median
        const median = d % 2 === 0 ? 
            (sortedTrailingDays[middle] + sortedTrailingDays[middle - 1]) / 2 
            : sortedTrailingDays[middle - 1];
    
        if (spentThisDay >= median * 2) {
          notificationCounter++;
        }
      }
    
      return notificationCounter;
    }
    
  • + 0 comments
    #include <bits/stdc++.h>
    
    using namespace std;
    
    vector<string> split_string(string);
    double getMedian(vector<int> occur, int elements);
    // Runs in O(n*d)
    int activityNotifications(vector<int> exp, int d) {
        
        int n;
        int count;
        int old_exp;
        vector<int>  occur(201);
        queue <int>  transaction_queue;
        double median;//i have some confusion in the code if there is a function that is activityNotificatiion give output of two function & logic of two function then what's "give the error" here the code of two logic in two different function.
        
        count = 0;
        n = exp.size();
        
        for(int i =0; i< d;i++)
            occur[exp[i]]++;
        
        
        for(int i =0; i< d;i++)
            transaction_queue.push(exp[i]);
        
        
        for(int i = d; i< n;i++){
            median = getMedian(occur, d);
            if(exp[i] >= 2.0*median){
                count++;
            }
            old_exp = transaction_queue.front();
            occur[old_exp]--;
            transaction_queue.pop();
            
            transaction_queue.push(exp[i]);
            occur[exp[i]]++;
        }
        
        
        return count;
    }
    
    double getMedian(vector<int> occur, int elements){
        
        int i;
        int j;
        bool isEven;
        int count;
        double median;
        
        i = 0;
        isEven = (elements % 2 == 0);
        count = 0;
        
        if(isEven){
            count = elements/2;
            
            while(count > 0){
                count -= occur[i];
                i++;
            }
            i--;
            if(count < 0){
                median = (double) i;
            }else{
                j = i+1;
                while(occur[j] == 0){
                    j++;
                }
                
                median = (double) (i+j)/(2);
            }
        }else{
            count = floor(elements/2);
            while(count >= 0){
                count -= occur[i];
                i++;
            }
            i--;
            
            median = (double) (i);
        }
        
        return median;
    }
    
        return splits;
    }