Thursday 25 August 2016

Dequeue in java

import java.util.*;

public class test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Deque<Integer> deque = new ArrayDeque<>();
        HashSet<Integer> set = new HashSet<>();
       
        int n = in.nextInt();
        int m = in.nextInt();
        int max = Integer.MIN_VALUE;

        for (int i = 0; i < n; i++) {
            int input = in.nextInt();
           
            deque.add(input);
            set.add(input);
           
            if (deque.size() == m) {
                if (set.size() > max) max = set.size();
                int first = deque.remove();
                if (!deque.contains(first)) set.remove(first);
            }
        }
       
        System.out.println(max);
    }
}
Sample Input
6 3
5 3 5 2 3 2
Sample Output
3

java sorting student name and CGPA

import java.util.*;

class Student implements Comparable{
    private int id;
    private String fname;
    private double cgpa;
    public Student(int id, String fname, double cgpa) {
        super();
        this.id = id;
        this.fname = fname;
        this.cgpa = cgpa;
    }
    public int getId() {
        return id;
    }
    public String getFname() {
        return fname;
    }
    public double getCgpa() {
        return cgpa;
    }
    public int compareTo(Object o){
        Student s=(Student)o;
        if(cgpa==s.cgpa){
            if(fname.equals(s.fname)){
                return id-s.id;
            }else{
                return fname.compareTo(s.fname);
            }
        }else{
            if(s.cgpa-cgpa>0)
                return 1;
            else
                return -1;
        }
    }

}


//Complete the code
public class Solution
{
   public static void main(String[] args){
      Scanner in = new Scanner(System.in);
      int testCases = Integer.parseInt(in.nextLine());

      List<Student> studentList = new ArrayList<Student>();
      while(testCases>0){
         int id = in.nextInt();
         String fname = in.next();
         double cgpa = in.nextDouble();

         Student st = new Student(id, fname, cgpa);
         studentList.add(st);

         testCases--;
      }
      Collections.sort(studentList);
         for(Student st: studentList){
         System.out.println(st.getFname());
      }
   }
}
Sample Input
5
33 Rumpa 3.68
85 Ashis 3.85
56 Samiha 3.75
19 Samara 3.75
22 Fahim 3.76
Sample Output
Ashis
Fahim
Samara
Samiha
Rumpa

Java program to accept the date and print the week name

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String month = in.next();
    String day = in.next();
    String year = in.next();

    Calendar calendar = Calendar.getInstance();
    calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));

    String dayOfWeek = calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US);

    System.out.print(dayOfWeek.toUpperCase());

}
}
Sample Input
08 05 2015
Sample Output
WEDNESDAY

Java program to read N lines of input until you reach EOF

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
   static String s;
    static int a=1;
    // For loop
public static void main(String[] args) {
   Scanner scan = new Scanner(System.in);
   for(int i = 1; scan.hasNext()== true; i++){
       System.out.println(i + " " + scan.nextLine());
   }
}

}

Java program to rad elements in to an array and display in reverse order

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


public class Solution {

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); // Scanner Input from the user
    int n = in.nextInt();   // SizeOfArray
    int[] arr = new int[n]; // initializing a variable(arr) for the array

    // get value from the user and load into the array[]
    for(int i=0; i < n; i++){  
        arr[i] = in.nextInt();
    }

    // declaring the reverse method for the "arr"
    reverse(arr);

    // for loop to print the values of array in reverse
    for(int i = 0; i < n; i++){
            System.out.printf("%d" + " ", arr[i]);
        }

 }//in.close(); /*I purposely hid this Scanner closing() to acquire the desired output, anyone have an idea to get the output without hiding the closing() plz assist me*/

    // the reverse method to swap the values inside the array

    public static void reverse(int[] arr){

        // this loop is necessary if you want to swap the values inside an array
        for(int i=0;i<arr.length/2;i++)
            {
                int temp=arr[i];
                arr[i]=arr[(arr.length-1)-i];
                arr[(arr.length-1)-i]=temp;
            }
    }
}