{"id":"5598044a-d6cd-42af-bf80-d5c1d75939a7","name":"First Bad Version","description":"1. You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.\r\n2. Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.\r\n3. You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.\r\n","inputFormat":"Input is managed for you\r\n","outputFormat":"Output is managed for you\r\n","constraints":"1 &lt;= bad &lt;= n &lt;= 2^31 - 1\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint static bad=0;\nbool isBad(int val)\n{\n if(val>=bad)\n return true;\n else \n return false;\n}\nint fbv(int n)\n{\n //write your code here\n \n return 0;\n \n}\n\nint main()\n{\n int n;\n cin>>n;\n int p;\n cin>>p;\n bad=p;\n \n int x=fbv(n);\n cout<<x;\n \n}"},"java":{"code":"import java.util.*;\r\nimport java.io.*;\r\n\r\npublic class Main {\r\n\r\n public static int firstBadVersion(int n) {\r\n //write your code here\r\n return 0;\r\n }\r\n\r\n static int bad = 0;\r\n public static boolean isBadVersion(int val) {\r\n if (val >= bad) {\r\n return true;\r\n }\r\n else {\r\n return false;\r\n }\r\n }\r\n\r\n public static void solve(int n, int fbv) {\r\n bad = fbv;\r\n System.out.println(firstBadVersion(n));\r\n }\r\n\r\n public static void main(String[]args) {\r\n //input work\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int fbv = scn.nextInt();\r\n\r\n solve(n, fbv);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"10 3","sampleOutput":"3","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"79eda40b-c2c2-45a6-9d38-49d66acc7707","name":"First Bad Version","slug":"first-bad-version","type":1}],"next":{"id":"e9ee3c89-902a-4cc3-b498-4cb6a2217160","name":"First Bad Version","type":3,"slug":"first-bad-version"},"prev":{"id":"bc7d0a1a-2880-464f-b50b-769d2f31765e","name":"Guess Number Higher Or Lower MCQ","type":0,"slug":"guess-number-higher-or-lower-mcq"}}}

First Bad Version

1. You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. 2. Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad. 3. You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

{"id":"5598044a-d6cd-42af-bf80-d5c1d75939a7","name":"First Bad Version","description":"1. You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.\r\n2. Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.\r\n3. You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.\r\n","inputFormat":"Input is managed for you\r\n","outputFormat":"Output is managed for you\r\n","constraints":"1 &lt;= bad &lt;= n &lt;= 2^31 - 1\r\n","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint static bad=0;\nbool isBad(int val)\n{\n if(val>=bad)\n return true;\n else \n return false;\n}\nint fbv(int n)\n{\n //write your code here\n \n return 0;\n \n}\n\nint main()\n{\n int n;\n cin>>n;\n int p;\n cin>>p;\n bad=p;\n \n int x=fbv(n);\n cout<<x;\n \n}"},"java":{"code":"import java.util.*;\r\nimport java.io.*;\r\n\r\npublic class Main {\r\n\r\n public static int firstBadVersion(int n) {\r\n //write your code here\r\n return 0;\r\n }\r\n\r\n static int bad = 0;\r\n public static boolean isBadVersion(int val) {\r\n if (val >= bad) {\r\n return true;\r\n }\r\n else {\r\n return false;\r\n }\r\n }\r\n\r\n public static void solve(int n, int fbv) {\r\n bad = fbv;\r\n System.out.println(firstBadVersion(n));\r\n }\r\n\r\n public static void main(String[]args) {\r\n //input work\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int fbv = scn.nextInt();\r\n\r\n solve(n, fbv);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"10 3","sampleOutput":"3","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"79eda40b-c2c2-45a6-9d38-49d66acc7707","name":"First Bad Version","slug":"first-bad-version","type":1}],"next":{"id":"e9ee3c89-902a-4cc3-b498-4cb6a2217160","name":"First Bad Version","type":3,"slug":"first-bad-version"},"prev":{"id":"bc7d0a1a-2880-464f-b50b-769d2f31765e","name":"Guess Number Higher Or Lower MCQ","type":0,"slug":"guess-number-higher-or-lower-mcq"}}}
plane

Editor


Loading...

First Bad Version

easy

1. You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. 2. Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad. 3. You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Constraints

1 <= bad <= n <= 2^31 - 1

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

10 3

Sample Output

3

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode