Fraudulent Activity Notifications

  • + 1 comment

    This code passes basic test cases but exceeding time limit...can you suggest me what i am doing wrong?

     // Complete the activityNotifications function below.
    static int activityNotifications(int[] expenditure, int d) {
        int output = 0;
        for (int i = 0; i <= expenditure.length-d; i++) {
            //System.out.println(Arrays.toString(expenditure));
            List<Integer> temp=new ArrayList<>();
            int count = 0;
            for (int j = i; temp.size()<d && j < expenditure.length; j++) {
                temp.add(expenditure[j]);
                ++count;
            }
            if (count == d) {
                Collections.sort(temp);
                float median = getMedian(temp );
                if (i + d < expenditure.length) {
                    if (expenditure[i + d] >= (median * 2)) {
                        output++;
                    }
                }
            }
        }
        return output;
    }
    
    private static float getMedian( List<Integer> temp) {
        if (temp.size() % 2 == 0) {
            int mNo1 = (temp.size() / 2) - 1;
            int mNo2 = (temp.size() / 2) ;
            return (temp.get(mNo1) + temp.get(mNo2))/2f;
        } else {
            return temp.get(temp.size() / 2);
        }
    }