Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You have an array of integers with length N. (For testing use 10, 20, and 30). The numbers in thearray are consecutive starting with 1.
You have an array of integers with length N. (For testing use 10, 20, and 30). The numbers in thearray are consecutive starting with 1. Graders will use other values for grading, so your solution must work for other test cases. int N = 12; int[]data ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; int mod = 5;//total is 78, 78 mod 5 is 3 Your goal is to minimize the total of all array elements in the list when modded by a number (variable name mod). For instance, if N is 12 and mod is 5, you would add all elements in the array and mod the total by 5. Initially (with no alterations to the array) the total would be 78 and the modded value would be 3. Your approach to finding the lowest modded value (regardless of the actual total) will need to be recursive. If possible, memoize for 5 extra points. You will be working on adjacent pairs. You will start with the first two pairs. Then you will move your index up two places and work with the next pair. Both values in a pair can have a difference of at most 2. For each pair, you subtract or add to both numbers. That gives you four possibilities: [index]++, [index + 1]++[index]--, [index+1]++[index]++, [index+1]-- or instance, for the pair you are currently indexing, you might add one to the first element and subtract one the second element (index, index+1) You have an array of integers with length N. (For testing use 10, 20, and 30). The numbers in thearray are consecutive starting with 1. Graders will use other values for grading, so your solution must work for other test cases. int N = 12; int[]data ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; int mod = 5;//total is 78, 78 mod 5 is 3 Your goal is to minimize the total of all array elements in the list when modded by a number (variable name mod). For instance, if N is 12 and mod is 5, you would add all elements in the array and mod the total by 5. Initially (with no alterations to the array) the total would be 78 and the modded value would be 3. Your approach to finding the lowest modded value (regardless of the actual total) will need to be recursive. If possible, memoize for 5 extra points. You will be working on adjacent pairs. You will start with the first two pairs. Then you will move your index up two places and work with the next pair. Both values in a pair can have a difference of at most 2. For each pair, you subtract or add to both numbers. That gives you four possibilities: [index]++, [index + 1]++[index]--, [index+1]++[index]++, [index+1]-- or instance, for the pair you are currently indexing, you might add one to the first element and subtract one the second element (index, index+1)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started