Advertisement
saurav_kalsoor

Maths Assignment - JAVA

Dec 22nd, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. // Author : Saurav Kalsoor
  2. // Maths Assignment - JAVA
  3.  
  4. import java.util.*;
  5.  
  6. public class Test {
  7.  
  8.     static Scanner sc = new Scanner(System.in);
  9.     static ArrayList<Long> list = new ArrayList<Long>();
  10.     public static void main(String[] args) {
  11.  
  12.         long n = sc.nextLong();
  13.         long result = mathsAssignment(n);
  14.         System.out.println(result);
  15.  
  16.     }
  17.  
  18.     public static void init(){
  19.         BitSet b = new BitSet(36);
  20.         for(int i=0; i < 36; i++){
  21.             b.set(i);
  22.             for(int j = i+1; j < 36; j++){
  23.                 b.set(j);
  24.                 long[] longArray = b.toLongArray();  
  25.                 list.add(longArray[0]);
  26.                 b.clear(j);
  27.             }
  28.             b.clear(i);
  29.         }
  30.         Collections.sort(list);
  31.     }
  32.  
  33.     public static long lessThan(long n){
  34.         int low = 0, high = list.size() - 1, index = 0;
  35.         while(low <= high){
  36.             int mid = low + (high - low) / 2;
  37.             if(list.get(mid) <= n){
  38.                 index = mid;
  39.                 low = mid + 1;
  40.             }else{
  41.                 high = mid-1;
  42.             }
  43.         }
  44.         return list.get(index);
  45.     }
  46.  
  47.     public static long greaterThan(long n){
  48.         int low = 0, high = list.size() - 1, index = high;
  49.         while(low <= high){
  50.             int mid = low + (high - low) / 2;
  51.             if(list.get(mid) >= n){
  52.                 index = mid;
  53.                 high = mid - 1;
  54.             }else{
  55.                 low = mid + 1;
  56.             }
  57.         }
  58.         return list.get(index);
  59.     }
  60.  
  61.     public static long mathsAssignment(long n){
  62.         init();
  63.         long a = lessThan(n), b = greaterThan(n);
  64.         long res = Math.min(Math.abs(n - a), Math.abs(n - b));
  65.         return res;
  66.     }
  67.  
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement