Sort by

recency

|

336 Discussions

|

  • + 0 comments
    public class Solution {
    
        public static void main(String[] args) {
          Scanner sc=new Scanner(System.in);
          int n=sc.nextInt();
          int m=sc.nextInt();
          Deque<Integer> deq=new ArrayDeque<>();
          Map<Integer,Integer> hm=new HashMap<>();
          int res=0;
          for(int i=0;i<n;i++){
            int ele=sc.nextInt();
            deq.offerLast(ele);
            hm.put(ele,hs.getOrDefault(ele, 0)+1);
            if(deq.size()==m){
                res=Math.max(res, hm.size());
                int out=deq.pollFirst();
                int count=hm.get(out);
                if(count==1){
                    hm.remove(out);
                }else{
                    hm.put(out,hs.get(out)-1);
                }
                
            }
          }
          System.out.println(res);
        }
    }
    
  • + 0 comments

    Set took too much time.

    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            try (BufferedReader bIn = new BufferedReader(new InputStreamReader(System.in))) {
                // get max length;
                String line = bIn.readLine();
                int setLength = Integer.valueOf(line.split(" ")[1]).intValue();
                
                // find max unique number
                line = bIn.readLine();
                Deque<Long> l = new LinkedList<>();
                Map<Long, Integer> m = new HashMap<>();
                int maxUnique = 0;
                
                for (String str : line.split(" ")) {
                    Long val = Long.valueOf(str);
                    l.add(val);
                    
                    if (m.containsKey(val)) {
                        m.put(val, m.get(val) + 1);
                    } else {
                        m.put(val, 1);
                    }
                    
                    if (l.size() == setLength) {
                        maxUnique = Math.max(maxUnique, m.size());
                        Long clearVal = l.poll();
                        int sum = m.get(clearVal) - 1;
                        if (sum == 0) {
                            m.remove(clearVal);
                        } else {
                            m.put(clearVal, sum);
                        }
                    }
                }
                
                System.out.println(maxUnique);
            } catch (Exception e) {
                //
            }
        }
    }
    
  • + 0 comments

    my solution: import java.util.*; public class test { public static void main(String[] args) { Scanner in = new Scanner(System.in); Deque deque = new ArrayDeque<>(); Set set = new HashSet<>(); int n = in.nextInt(); int m = in.nextInt(); int max = 0;

            for (int i = 0; i < n; i++) {
                int num = in.nextInt();
                deque.addLast(num);
                set.add(num);
    
            if(deque.size() == m){
                max = Math.max(max, set.size());
                int first = deque.removeFirst();
                if(!deque.contains(first)){
                    set.remove(first);
                }
            }
        }
       System.out.println(max); 
     }
    
    }
    
  • + 0 comments

    Here is Java Dequeue solution - https://programmingoneonone.com/hackerrank-java-dequeue-problem-solution.html

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {  
    List<Integer> list1 = new ArrayList<>(Arrays.asList(6, 5));
    

    List list2 = new ArrayList<>(Arrays.asList(5, 3, 5, 2, 3, 2)); List> list3 = new ArrayList<>(); list3.add(list1); list3.add(list2); maxUniqeNumbers(list3); System.out.println (maxUniqeNumbers(list3)); } public static int maxUniqeNumbers (List> list3) { int max = 3; int x = 3; for (List innerList : list3) { if (innerList.size () < 3) { continue ; } else { int i = 0 ; // int n = 0 ;

    for (i = 0 ; i < innerList.size() - 2 ; i++) {
            int m = 0;
        if (innerList.get (i) == innerList.get (i+1)) {
           m++; 
        }
        if (innerList.get (i) == innerList.get (i+2)) {
           m++; 
        }
        if (innerList.get (i+2) == innerList.get (i+1)) {
           m++; 
        }
    

    if (m < x) { x = m ;
    } }

    }
     }
     return max - x ;
    

    } }