Given an integer n, you can do the following replacement operations: 1. Replace n with n/2 if n is even 2. Replace n with either n - 1 or n + 1 if nis odd Given an integer n, determine the minimum number of replacements needed to reach 1? For example, if n = 50, then we need a minimum of 7 replacements, as follows: divide by 2 50 add 1 23- 50 25 Another example, if n = 23, then we need a minimum of 6 replacements, as follows: divide by 2 24 Output: subtract 1 24 12 divide by 2 divide by 2 12 6 Please use recursion to solve this task and do NOT use loops. Sample Testcase 0: Input: divi divide by

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Given an integer n, you can do the following
replacement operations:
1. Replace n with n/2 if n is even
2. Replace n with either n - 1 or n + 1 if n is
odd
Given an integer n, determine the minimum
number of replacements needed to reach 1?
For example, if n = 50, then we need a
minimum of 7 replacements, as follows:
50
divide by 2
→25
50
add 1
23-
Another example, if n = 23, then we need a
minimum of 6 replacements, as follows:
divide by 2
subtract 1
24
Output:
24
12
divide by 2
divide by 2
12
6
Please use recursion to solve this task and do
NOT use loops.
Sample Testcase 0:
Input:
divide
divide by 2
Transcribed Image Text:Given an integer n, you can do the following replacement operations: 1. Replace n with n/2 if n is even 2. Replace n with either n - 1 or n + 1 if n is odd Given an integer n, determine the minimum number of replacements needed to reach 1? For example, if n = 50, then we need a minimum of 7 replacements, as follows: 50 divide by 2 →25 50 add 1 23- Another example, if n = 23, then we need a minimum of 6 replacements, as follows: divide by 2 subtract 1 24 Output: 24 12 divide by 2 divide by 2 12 6 Please use recursion to solve this task and do NOT use loops. Sample Testcase 0: Input: divide divide by 2
23
50
Output:
7
add 1
23
24
Please use recursion to solve this task and do
NOT use loops.
Sample Testcase 0:
Input:
Sample Testcase 1:
Input:
Output:
6
divide by 2
12
CodeBox1
divide by 2
6
divide by 2
import java.util.Scanner;
Transcribed Image Text:23 50 Output: 7 add 1 23 24 Please use recursion to solve this task and do NOT use loops. Sample Testcase 0: Input: Sample Testcase 1: Input: Output: 6 divide by 2 12 CodeBox1 divide by 2 6 divide by 2 import java.util.Scanner;
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Topological Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education