Fraudulent Activity Notifications

Sort by

recency

|

1180 Discussions

|

  • + 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;
    }
    
  • + 0 comments

    include

    using namespace std;

    int test(const vector &a, int n, int d) { int res = 0; vectorb(a.begin(), a.begin() + d); sort(b.begin(), b.end()); for(int i = d; i < n; i++) { int sum = 0; if(d % 2 == 1) { sum = 2 * b[d / 2]; } else { sum = b[d / 2] + b[d / 2 - 1]; } if(a[i] >= sum) { res++; } b.erase(lower_bound(b.begin(), b.end(), a[i - d])); b.insert(lower_bound(b.begin(), b.end(), a[i]), a[i]); } return res; }

    int main() { int n, d; cin >> n >> d; vector a(n); for(int i = 0; i < n; i++) { cin >> a[i]; } cout << test(a, n, d) << endl; return 0; }

  • + 0 comments

    Python3 using bisect library

    from bisect import bisect, insort
    def activityNotifications(expenditures, d):
        res = 0
        vals = sorted(expenditures[:d])
        for first_val, cur_val in zip(expenditures, expenditures[d:]):    
            if d % 2:
                median2 = 2 * vals[d//2]
            else:
                median2 = vals[d//2] + vals[d//2-1]
            if cur_val >= median2:
                res += 1
            vals.pop(bisect(vals, first_val) - 1)
            insort(vals, cur_val)
        return res
    
  • + 0 comments

    C#

    public static int activityNotifications(List<int> expenditure, int d)
    {
        var totalNotifications = 0;
        int[] frequency = new int[201]; // Create a frequency array.
    
        if (expenditure.Count > d)
        {
            for (int i = 0; i < d; ++i)
            {
                frequency[expenditure[i]]++;
            }
    
            for (int i = d; i < expenditure.Count; i++)
            {
                var median = GetMedian(frequency, d);
    
                if (expenditure[i] >= 2 * median)
                {
                    totalNotifications++;
                }
    
                frequency[expenditure[i - d]]--; // Remove element from the frequency.
                frequency[expenditure[i]]++; // Add element to the frequency.
            }
        }
    
        return totalNotifications;
    }
    
    public static double GetMedian(int[] frequency, int d)
    {
        var sum = 0;
        double median = 0;
    
        if (d % 2 == 1)
        {
            var mid = d / 2;
            for (int i = 0; i < frequency.Length; ++i)
            {
                sum += frequency[i];
                if (sum > mid)
                {
                    median = i;
                    break;
                }
            }
        }
        else
        {
            var mid1 = d / 2 - 1;
            var mid2 = d / 2;
            int median1 = -1, median2 = -1;
    
            for (int i = 0; i < frequency.Length; ++i)
            {
                sum += frequency[i];
                if (sum > mid1 && median1 == -1)
                {
                    median1 = i;
                }
                if (sum > mid2)
                {
                    median2 = i;
                    median = (median1 + median2) / 2.0;
                    break;
                }
            }
        }
    
        return median;
    }
    
  • + 1 comment

    i did this solution: but only 5/8. what is wrong?

    public static int activityNotifications(List expenditure, int d) { int counter=0; int sum=0;

        for(int i=0;i<d;i++){
            sum+=expenditure.get(i);
        }
        for(int i=d; i<expenditure.size();i++){
            double avg=(double)sum/d;
            if(expenditure.get(i)>=2*avg){
                counter++;
            }
    
            sum-=expenditure.get(i-d);
            sum+=expenditure.get(i);
    
        }
       return counter;
    }