`{"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"}}}`

`{"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"}}}`

Editor

easy

Constraints

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

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

`.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}10 3`

Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}3`

Discussions

Show Discussion

Related Resources