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.
publicstaticintpairs(intk,List<Integer>arr){// Create a HashSet for quick lookupsHashSet<Integer>set=newHashSet<>(arr);intnumOfpairs=0;// Loop through each element in the setfor(intnum:set){// Check if the pair (num + k) exists in the setif(set.contains(num-k)){numOfpairs++;}}returnnumOfpairs;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Pairs
You are viewing a single comment's thread. Return to all comments →
Java: