{"id":"390c4bd5-adf6-4505-89dc-469463a3e474","name":"Reduce N To 1","description":"1. You are given a positive number N.\r\n2. You have to find the minimum number of operations required to convert N into 1.\r\n3. Operations allowed :\r\n (i) If n is even, you have to replace n with n/2.\r\n (ii) If n is odd, you can replace n with either n-1 or n+1.","inputFormat":"A number N","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= N &lt;= 2147483647","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n\nint solution(long n){\n // write your code here\n}\n\n\nint main(){\n int n;\n cin>>n;\n \n cout<<solution(n)<<endl;\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int n) {\r\n //write your code here\r\n return 0;\r\n }\r\n \r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint n = scn.nextInt();\r\n System.out.println(solution(n));\r\n }\r\n\t\r\n\t\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"8","sampleOutput":"3","questionVideo":"https://www.youtube.com/embed/pW5fvwnZ1TA?end=72","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":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"785e6222-24de-4521-b176-9751399e5015","name":"Reduce N To 1","slug":"reduce-n-to-1","type":1}],"next":{"id":"67c73ff3-c429-4389-864f-74858a78b484","name":"Reduce N to 1 MCQ","type":0,"slug":"reduce-n-to-1-mcq"},"prev":{"id":"3f829bac-746d-4966-882e-bf4d9e42db10","name":"TRIPLETS-1","type":3,"slug":"triplets-1"}}}

Reduce N To 1

1. You are given a positive number N. 2. You have to find the minimum number of operations required to convert N into 1. 3. Operations allowed : (i) If n is even, you have to replace n with n/2. (ii) If n is odd, you can replace n with either n-1 or n+1.

{"id":"390c4bd5-adf6-4505-89dc-469463a3e474","name":"Reduce N To 1","description":"1. You are given a positive number N.\r\n2. You have to find the minimum number of operations required to convert N into 1.\r\n3. Operations allowed :\r\n (i) If n is even, you have to replace n with n/2.\r\n (ii) If n is odd, you can replace n with either n-1 or n+1.","inputFormat":"A number N","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= N &lt;= 2147483647","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\n\nint solution(long n){\n // write your code here\n}\n\n\nint main(){\n int n;\n cin>>n;\n \n cout<<solution(n)<<endl;\n \n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int n) {\r\n //write your code here\r\n return 0;\r\n }\r\n \r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint n = scn.nextInt();\r\n System.out.println(solution(n));\r\n }\r\n\t\r\n\t\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"8","sampleOutput":"3","questionVideo":"https://www.youtube.com/embed/pW5fvwnZ1TA?end=72","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":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"785e6222-24de-4521-b176-9751399e5015","name":"Reduce N To 1","slug":"reduce-n-to-1","type":1}],"next":{"id":"67c73ff3-c429-4389-864f-74858a78b484","name":"Reduce N to 1 MCQ","type":0,"slug":"reduce-n-to-1-mcq"},"prev":{"id":"3f829bac-746d-4966-882e-bf4d9e42db10","name":"TRIPLETS-1","type":3,"slug":"triplets-1"}}}
plane

Editor


Loading...

Reduce N To 1

easy

1. You are given a positive number N. 2. You have to find the minimum number of operations required to convert N into 1. 3. Operations allowed : (i) If n is even, you have to replace n with n/2. (ii) If n is odd, you can replace n with either n-1 or n+1.

Constraints

1 <= N <= 2147483647

Format

Input

A number N

Output

Check the sample ouput and question video.

Example

Sample Input

8

Sample Output

3

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode