Fraudulent Activity Notifications

  • + 4 comments

    @varun_u28 Sorry for late reply.

    I hope you understood about getMedian() method, which will simply calculate median of the freq[] array with the help of prefixsum concept. Please let me know if u need more explanation.

    In showingNotifications() method, flow is like this

    1.arr[] will store expenditures of n days,

    2.As given in problem all expenditures are in the range of 0 to 200 inclusive, So I declared freq[] array of size 201 which will store frequency of expenditure(how many times the same expenditure occured). this is important as while finding median we no need to sort expenditures.

    3.I started loop iteration from index = d beacuse we need atleast d days expenditures to caluclate median.

    4.At any point of time freq[] array will have only d days expenditures only. When we are incrementing next index+1 expenditure to freq[] array, we will decrement index-d expenditure from freq[] array.

    5.For Ex. if n = 10 and d = 5 and expenditures are in the range of 0 to 8

    expenditures array:

    index: 0 1 2 3 4 5 6 7 8 9 value: 2 3 4 2 3 6 8 4 5 0

    freq array:(Intially) index: 0 1 2 3 4 5 6 7 8 value: 0 0 0 0 0 0 0 0 0

    we will iterate array from i = 5 to n-1(which is 8)

    when i = 5, array range(0 to 4) freq array: index: 0 1 2 3 4 5 6 7 8 value: 0 0 2 2 1 0 0 0 0 find median with this

    when i = 6, array range(1 to 5) freq array: index: 0 1 2 3 4 5 6 7 8 value: 0 0 1 2 1 0 1 0 0 find median with this

    when i = 7, array range(2 to 6) freq array: index: 0 1 2 3 4 5 6 7 8 value: 0 0 1 1 1 0 1 0 1 find median with this

    when i = 8, array range(3 to 7) freq array: index: 0 1 2 3 4 5 6 7 8 value: 0 0 1 1 1 0 1 0 1 find median with this -->1--,0++(we are decrementing at expenditure 4 and again we are incrementing at expenditure 4 as arr[2] = arr[7] = 4)

    when i = 9, array range(4 to 8) freq array: index: 0 1 2 3 4 5 6 7 8 value: 1 0 1 0 1 0 1 0 1 find median with this.

    I hope you will understand now. Good Luck!!!