Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author : Saurav Kalsoor
- // Maths Assignment - JAVA
- import java.util.*;
- public class Test {
- static Scanner sc = new Scanner(System.in);
- static ArrayList<Long> list = new ArrayList<Long>();
- public static void main(String[] args) {
- long n = sc.nextLong();
- long result = mathsAssignment(n);
- System.out.println(result);
- }
- public static void init(){
- BitSet b = new BitSet(36);
- for(int i=0; i < 36; i++){
- b.set(i);
- for(int j = i+1; j < 36; j++){
- b.set(j);
- long[] longArray = b.toLongArray();
- list.add(longArray[0]);
- b.clear(j);
- }
- b.clear(i);
- }
- Collections.sort(list);
- }
- public static long lessThan(long n){
- int low = 0, high = list.size() - 1, index = 0;
- while(low <= high){
- int mid = low + (high - low) / 2;
- if(list.get(mid) <= n){
- index = mid;
- low = mid + 1;
- }else{
- high = mid-1;
- }
- }
- return list.get(index);
- }
- public static long greaterThan(long n){
- int low = 0, high = list.size() - 1, index = high;
- while(low <= high){
- int mid = low + (high - low) / 2;
- if(list.get(mid) >= n){
- index = mid;
- high = mid - 1;
- }else{
- low = mid + 1;
- }
- }
- return list.get(index);
- }
- public static long mathsAssignment(long n){
- init();
- long a = lessThan(n), b = greaterThan(n);
- long res = Math.min(Math.abs(n - a), Math.abs(n - b));
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement