How can I optimise this code there is time out error

Program Challenge:

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 double 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, find and print the number of times the client will receive a notification over all days.

For example, d = 3 and expenditures = [10, 20, 30, 40, 50]. On the first three days, they just collect spending data. At day 4, we have trailing expenditures of [10, 20, 30]. The median is 20 and the day's expenditure is 40. Because \$40 \ge 2*20\$, there will be a notice. The next day, our trailing expenditures are [20, 30, 40] and the expenditures are 50. This is less than \$2*30\$ 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 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 and, the number of days of transaction data, and the number of trailing days' data used to calculate median spending. The second line contains space-separated non-negative integers where each integer denotes.


  • \$ 1 \le n \le 2*10^5 \$
  • \$ 1 \le d \le n \$
  • \$ 0 \le expenditure[i] \le 200\$

Output Format

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

Sample Input 0

9 5

2 3 4 2 3 6 8 4 5

Sample Output 0


HackerRank fraudulent activity.

public static int[] CountSort(int []arr,int si,int ei) {
     int []count=new int[201];
      int result[]=new int[ei-si];
      for(int i=si;i<ei;i++) {
      for(int i=1;i<count.length;i++) {
      for(int i=ei-1;i>=si;i--) {

      return result;
 // Complete the activityNotifications function below.
 static int activityNotifications(int[] expenditure, int d) {
     int notice=0;
     for(int i=0;i<=expenditure.length-d;i++) {
         int arr[]=new int[d];
         double median=0;
         if(d%2==0) {
             int mid1=(d)/2;
             int mid2=(d)/2 +1;
         }else {
             int mid=(d)/2;
         if(i+d<expenditure.length&&expenditure[i+d]>=2*median) {
     return notice;

Quite often with HackerRank puzzles, there will be a check that your algorithm scales to large sizes. Here we are explicitly told that n may be up to 199 999.

You have managed to come up with a homebrew sort that appears to be efficient given the data constraints. However, you are calling it in the outer loop, so your algorithm appears to be O(n^2) overall.

Try working through the sample input "9 5 / 2 3 4 2 3 6 8 4 5" with pencil and paper; I'll bet you find it didn't involve nearly as much sorting. Then think about what data structures would best model what you did.

| improve this answer | |
  • \$\begingroup\$ thanks it is a great help \$\endgroup\$ – Ritik Aggarwal Jun 29 at 17:25
  • \$\begingroup\$ its min heap ?? am I right \$\endgroup\$ – Ritik Aggarwal Jun 29 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.