Fastrail08

First Bad Version

Aug 18th, 2022
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main {
  5.  
  6.   public static int firstBadVersion(int n) {
  7.     //write your code here
  8.     int l = 1, r = n;
  9.     int bad = 0;
  10.     while(l <= r){
  11.         int mid = l + (r - l) / 2;
  12.         if(isBadVersion(mid)){
  13.             bad = mid;
  14.             r = mid - 1;
  15.         }
  16.         else{
  17.             l = mid + 1;
  18.         }
  19.     }
  20.    
  21.     return bad;
  22.   }
  23.  
  24.   static int bad = 0;
  25.   public static boolean isBadVersion(int val) {
  26.     if (val >= bad) {
  27.       return true;
  28.     }
  29.     else {
  30.       return false;
  31.     }
  32.   }
  33.  
  34.   public static void solve(int n, int fbv) {
  35.     bad = fbv;
  36.     System.out.println(firstBadVersion(n));
  37.   }
  38.  
  39.   public static void main(String[]args) {
  40.     //input work
  41.     Scanner scn = new Scanner(System.in);
  42.     int n = scn.nextInt();
  43.     int fbv = scn.nextInt();
  44.  
  45.     solve(n, fbv);
  46.   }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment