Advertisement
saurav_kalsoor

Help Mocha - JAVA

Sep 28th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package com.company;
  2. import java.util.*;
  3.  
  4.  
  5. public class Problem {
  6.  
  7.     private static final int N = 300000;
  8.  
  9.     public static void main(String[] args){
  10.         Scanner sc = new Scanner(System.in);
  11.         int d = sc.nextInt();
  12.         long result = helpMocha(d);
  13.         System.out.println(result);
  14.     }
  15.  
  16.     static boolean[] isPrime = new boolean[N+1];
  17.     static ArrayList<Long> primes = new ArrayList<>();
  18.  
  19.     static void sieve(){
  20.         for(long i=2; i <= N; i++){
  21.             if(isPrime[(int)i]){
  22.                 for(long j = i*i; j <= N; j += i){
  23.                     isPrime[(int)j] = false;
  24.                 }
  25.                 primes.add(i);
  26.             }
  27.         }
  28.     }
  29.  
  30.     static long lowerBound(ArrayList<Long> list, long x){
  31.         int n = list.size();
  32.         int lo = 0, hi = n-1, res = n-1;
  33.  
  34.         while (lo <= hi){
  35.             int mid = lo + (hi - lo)/2;
  36.             if(list.get(mid) == x)
  37.                 return x;
  38.             else if(list.get(mid) > x){
  39.                 res = mid;
  40.                 hi = mid-1;
  41.             }else{
  42.                 lo = mid+1;
  43.             }
  44.         }
  45.  
  46.         return list.get(res);
  47.     }
  48.  
  49.  
  50.     static long helpMocha(int d){
  51.         Arrays.fill(isPrime, true);
  52.         sieve();
  53.  
  54.         long a = lowerBound(primes, 1 + d);
  55.         long b = lowerBound(primes, a + d);
  56.  
  57.         return a*b;
  58.     }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement