Advertisement
ProgrammingNoob1234

Untitled

Nov 29th, 2022 (edited)
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. public class ContainsDuplicatesII_219 {
  2.     public static void main(String[] args) {
  3.         int[] arr = new int[] {99,99};
  4.         int k = 2;
  5.  
  6.         System.out.println(ContainsDuplicatesII_219.containsNearbyDuplicate(arr, k));
  7.        
  8.     }
  9.  
  10.     public static boolean containsNearbyDuplicate(int[] nums, int k) {
  11.         ArrayList<Integer> result = new ArrayList<Integer>();
  12.         int windowStart = 0;
  13.  
  14.         for (int windowEnd = 0; windowEnd < nums.length; windowEnd++) {
  15.             result.add(nums[windowEnd]);
  16.             if (result.size() >= k) {
  17.                 if (result.get(windowStart) == result.get(result.size() - 1) && Math.abs(result.get(windowStart) - result.get(result.size() - 1)) <= k) {
  18.                     return true;
  19.                 }
  20.                 result.remove(windowStart);
  21.             }
  22.  
  23.            
  24.         }
  25.  
  26.         return false;
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement