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.
importjava.io.*;importjava.math.*;importjava.security.*;importjava.text.*;importjava.util.*;importjava.util.concurrent.*;importjava.util.function.*;importjava.util.regex.*;importjava.util.stream.*;importstaticjava.util.stream.Collectors.joining;importstaticjava.util.stream.Collectors.toList;classResult{/* * Complete the 'winningLotteryTicket' function below. * * The function is expected to return a LONG_INTEGER. * The function accepts STRING_ARRAY tickets as parameter. */publicstaticlongwinningLotteryTicket(List<String>tickets){// Write your code herelong[]freq=newlong[1024];longanswer=0;for(Strings:tickets){intoutput=Freq_to_Decimal(s);freq[output]++;}for(inti=0;i<1024;i++){for(intj=i;j<1024;j++){if((i|j)==1023){if(i==j){answer+=(freq[i]*(freq[i]-1))/2;}else{answer+=freq[i]*freq[j];}}}}returnanswer;}staticintFreq_to_Decimal(Strings){intans=0;for(charc:s.toCharArray()){intbit=c-'0';ans=ans|(1<<(9-bit));}returnans;}}publicclassSolution{publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbufferedWriter=newBufferedWriter(newOutputStreamWriter(System.out));intn=Integer.parseInt(bufferedReader.readLine().trim());List<String>tickets=IntStream.range(0,n).mapToObj(i->{try{returnbufferedReader.readLine();}catch(IOExceptionex){thrownewRuntimeException(ex);}}).collect(toList());longresult=Result.winningLotteryTicket(tickets);bufferedWriter.write(String.valueOf(result));bufferedWriter.newLine();bufferedReader.close();bufferedWriter.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Winning Lottery Ticket
You are viewing a single comment's thread. Return to all comments →
Java 8 solution: