We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
@kumar_streamnet thanks for your suggestion. I implemented an array of size 201. For 2 test cases, I still get the timeout error.
Please help me out,bro. I tried implementing a hashmap instead of the array (the commented code lines) but that didn't work out. The array method was the best results I've got so far with just 2 timeouts and rest of them being passed.
If there's a better way to use the hashmap, please let me know.
importjava.lang.*;importjava.util.*;publicclassFraudulentActivityNotifications{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);intn=in.nextInt();intd=in.nextInt();int[]expenditures=collectInput(in,n);intnotifications=computeNotifications(expenditures,d);printResult(notifications);}publicstaticint[]collectInput(Scannerin,intn){int[]expenditures=newint[n];for(inti=0;i<n;i++)expenditures[i]=in.nextInt();in.close();returnexpenditures;}publicstaticintcomputeNotifications(int[]expenditures,intd){intn=expenditures.length;intnotifications=0;Queue<Integer>medianArray=newLinkedList<Integer>();int[]countArray=newint[201];//Map<Integer,Integer> countMap = new HashMap<Integer,Integer>();inti=0;while(i<n){if(medianArray.size()<d){medianArray.add(expenditures[i]);countArray[expenditures[i]]++;/* if(countMap.containsKey(expenditures[i])) countMap.put(expenditures[i],countMap.get(expenditures[i]) + 1); else countMap.put(expenditures[i],1);*/}else{int[]medianSortedArray=sortMedianArray(countArray,d);//int[] medianSortedArray = sortMedianArray(countMap,d);doublemedian=getMedian(medianSortedArray);booleantoNotify=check(expenditures[i],median);if(toNotify)notifications++;intbeginElem=(Integer)medianArray.remove();countArray[beginElem]--;/*countMap.put(beginElem,countMap.get(beginElem) - 1); if((Integer) countMap.get(beginElem) == 0) countMap.remove(beginElem);*/medianArray.add(expenditures[i]);countArray[expenditures[i]]++;/*if(countMap.containsKey(expenditures[i])) countMap.put(expenditures[i],countMap.get(expenditures[i] + 1)); else countMap.put(expenditures[i],1);*/}i++;}returnnotifications;}publicstaticvoidprintResult(intnotifications){System.out.println(notifications);}privatestaticint[]sortMedianArray(int[]countArray,intd)//private static int[] sortMedianArray(Map<Integer,Integer> countMap, int d){int[]sortedMedianArray=newint[d];intindex=0;for(inti=0;i<201;i++){intrepeat=countArray[i];while(repeat>0){sortedMedianArray[index++]=i;repeat--;}}/* Set set = countMap.entrySet(); Iterator iter = set.iterator(); while(iter.hasNext()) { Map.Entry m = (Map.Entry) iter.next(); int expenditure = (Integer) m.getKey(); int repeat = (Integer) m.getValue(); while(repeat > 0) { sortedMedianArray[index++] = expenditure; repeat--; } } */returnsortedMedianArray;}privatestaticdoublegetMedian(int[]medianArray){intd=medianArray.length;doublemedian;if(d%2==1)// odd{intmedianPosition=(d+1)/2;intmedianIndex=medianPosition-1;median=(double)medianArray[medianIndex];}else// even{intmedianPosition1=d/2;intmedianPosition2=d/2+1;intmedianIndex1=medianPosition1-1;intmedianIndex2=medianPosition2-1;median=computeAverage(medianArray[medianIndex1],medianArray[medianIndex2]);}returnmedian;}privatestaticdoublecomputeAverage(intnum1,intnum2){return((double)num1+(double)num2)/2;}privatestaticbooleancheck(intexpenditure,doublemedian){if(expenditure>=(2*median))returntrue;returnfalse;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Fraudulent Activity Notifications
You are viewing a single comment's thread. Return to all comments →
@kumar_streamnet thanks for your suggestion. I implemented an array of size 201. For 2 test cases, I still get the timeout error.
Please help me out,bro. I tried implementing a hashmap instead of the array (the commented code lines) but that didn't work out. The array method was the best results I've got so far with just 2 timeouts and rest of them being passed.
If there's a better way to use the hashmap, please let me know.