Samkit5025

character of nth string

May 11th, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.    
  5.     static Scanner sc = new Scanner(System.in);
  6.  
  7.     public static Character findingTheCharacter(int n, int k){
  8.         if(n==1 && k==1)return '0';
  9.        
  10.         int mid = 1<<(n-1);
  11.        
  12.         if(k==mid){
  13.             return '1';
  14.         }
  15.        
  16.         if(k<mid){
  17.             return findingTheCharacter(n-1,k);
  18.         }
  19.        
  20.         if(findingTheCharacter(n-1,2*mid-k)=='0'){
  21.             return '1';
  22.         }
  23.         else{
  24.             return '0';
  25.         }
  26.     }
  27.    
  28.     public static void main(String[] args) {
  29.         int n = sc.nextInt();
  30.         int k = sc.nextInt();
  31.        
  32.         System.out.println(findingTheCharacter(n, k));
  33.     }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment