Question 1. Round to multiple by 5 (except under 40)

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

public class Solution {
    static int[] solve(int[] grades){
        for(int i = 0; i<grades.length; i++){
            if(grades[i] >=38){
                if(grades[i]%5 != 0 && grades[i]%5 >=3){
                    grades[i] += (5-(grades[i]%5));     
                }
            }  
        }
        return grades;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] grades = new int[n];
        for(int grades_i=0; grades_i < n; grades_i++){
            grades[grades_i] = in.nextInt();
        }
        int[] result = solve(grades);
        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i] + (i != result.length - 1 ? "\n" : ""));
        }
        System.out.println("");
    }
}

Question 2. Find all cases that sum of consecutive m elements equals given sum d.

public class Solution {
    static int solve(int n, int[] s, int d, int m){
        int cnt = 0;
        for(int i =0; i<=s.length-m;i++){
            int idx = 0;
            int subSum = 0;
            while(idx < m){
                subSum += s[i+idx];
                idx++;
            }
            if(subSum == d){
                cnt++;
            }
        }
        return cnt;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] s = new int[n];
        for(int s_i=0; s_i < n; s_i++){
            s[s_i] = in.nextInt();
        }
        int d = in.nextInt();
        int m = in.nextInt();
        int result = solve(n, s, d, m);
        System.out.println(result);
    }
}

Question 3. Find all cases that sum of pair elements in given array.

public class Solution {

    static int divisibleSumPairs(int n, int k, int[] ar) {
        int ret = 0;
        for(int i =0; i< n-1;i++){
            for(int j = i+1; j<n; j++){
                if((ar[i] + ar[j])%k == 0){
                    ret++;
                }
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int[] ar = new int[n];
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextInt();
        }
        int result = divisibleSumPairs(n, k, ar);
        System.out.println(result);
    }
}

Question 4. Find the highest frequency key - lowset one

public class Solution {

    static int migratoryBirds(int n, int[] ar) {
        int[] arr = new int[n];
        for(int i = 0; i<n; i++){
            arr[ar[i]]++;
        }
        int max = 0; int pos = 0;
        for(int i =0; i< n ; i++){
            if(arr[i] > max){
                max = arr[i];
                pos = i;
            }
        }
        return pos;   
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] ar = new int[n];
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextInt();
        }
        int result = migratoryBirds(n, ar);
        System.out.println(result);
    }
}

Question 5. Leap year and find Programmer's day

public class Solution {
    static String solve(int year){
        boolean isLeap = false;
        if(year >= 1700 && year <=1917 ){
            if(year % 4 == 0){
                isLeap = true;
            }
        }else if(year >= 1919 && year <=2700){
            if(year % 4 == 0 ){
                isLeap = true;
                if(year % 100 == 0 && year % 400 != 0){
                    isLeap = false;
                }
            }
        }
        int month = 1;
        int totDays = 0;
        while(totDays<=256){
            if(totDays + 31 > 256){
                break;
            }
            switch(month){
                case 1:case 3:case 5:case 7:case 8:case 10:case 12:
                    totDays+=31;
                    break;
                case 4:case 6:case 9:case 11:
                    totDays+=30;
                    break;
                case 2:
                    if(year == 1918)    totDays+=15;
                    else if(isLeap)     totDays+=29;
                    else                totDays+=28;
                    break;
            }
            month++;
        }
        String res = "";
        res += 256-totDays +".";
        int remain = 256-totDays;
        if(month/10  <1){
            res+="0"+month;
        }else{
            res+="10";
        }
        res+="."+year;
        return res ;

    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int year = in.nextInt();
        String result = solve(year);
        System.out.println(result);
    }
}

Question 6. Bon Appetit

public class Solution {
    static int bonAppetit(int n, int k, int b, int[] ar) {
        int sum = 0;
        for(int i = 0; i< n; i++){
            if(i != k){
                sum+=ar[i];
            }
        }
        return b- sum/2  ;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int[] ar = new int[n];
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextInt();
        }
        int b = in.nextInt();
        int result = bonAppetit(n, k, b, ar);
        System.out.println(result == 0 ? "Bon Appetit" : result);
    }
}

Question 7. Find pair of socks

public class Solution {
    static int sockMerchant(int n, int[] ar) {
        int[] socks = new int[101];
        for(int color : ar){
            socks[color]++;
        }
        int res = 0;
        for(int cnt : socks){
            while(cnt>1){
                res++;
                cnt-=2;
            }
        }
        return res;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] ar = new int[n];
        for(int ar_i = 0; ar_i < n; ar_i++){
            ar[ar_i] = in.nextInt();
        }
        int result = sockMerchant(n, ar);
        System.out.println(result);
    }
}

Question 8. Decision prime number

public static boolean isPrime(int a){
    if(a<=1){
        return false;
    }
    if(a>2 && a%2 == 0){
        return false;
    }
    for(int div = 2; div*div <= a; div++){
        if(a%div == 0){
            return false;
        }
    }
    return true;
}

results matching ""

    No results matching ""