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

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 7PE
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning