arr[0], arr[1], arr[2],.. ,arr[8], arr[9J, ray to be special, if it satisfies either one of two cases below: : arr[0] < arr[1], arr[1] > arr[2], arr[2] < arr[3], arr[3] : arr[0] > arr[1], arr[1] < arr[2], arr[2] > arr[3], arr[3] Is, If the array satisfies one of following two conditions, it is a s se 1, each odd-index element is greater than its neighbor(s). E ly one neighbor arr[8], we only need to consider arr[8] < a

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Given an integer array (data type: int) arr[10] with the initialized values for all its elements.
For all the elements from arr[0] to arr[9], i.e.,
arr[0], arr[1], arr[2], ..., arr[8], arr[9],
we call this array to be special, if it satisfies either one of two cases below:
Case 1: arr[0] < arr[1], arr[1] > arr[2], arr[2] < arr[3], arr[3] > arr[4], ..
Case 2: arr[0] > arr[1], arr[1] < arr[2], arr[2] > arr[3], arr[3] < arr[4], ..
In other words, If the array satisfies one of following two conditions, it is a special array.
• For Case 1, each odd-index element is greater than its neighbor(s). Because arr[9]
has only one neighbor arr[8], we only need to consider arr[8] < arr[9] for arr[9].
• For Case 2, each even-index element is greater than its neighbor(s). Because arr[0]
has only one neighbor arr[1], we only need to consider arr[0] > arr[1] for arr[0].
Part-(a). Write a program to determine whether this array is special or not.
Part-(b). In this part, we can decrease any element by one each time. If the array is not special,
determine the minimum number of decreases needed to change the array to be special.
For example, if the array contains the following ten numbers "1, 2, 3, 4, 5, 6, 7, 8, 9, 10", it is
not a special array. However, by decreasing "3 to 1", "5 to 3", "7 to 5" and "9 to 7", the array
with these ten new numbers "1, 2, 1, 4, 3, 6, 5, 8, 7, 10" becomes special. The total number
of decreases is 8 (= 2 + 2 +2 + 2), which is the minimum number of decreases needed.
Note 1: for either Case 1 or Case 2, we only consider ">" and "<", not ">" and "S".
Note 2: for part-b, only decreasing (by one) can be performed, not increasing.
Expected Outputs (Inputs are underlined):
Example 1
The array is: 15 39 30 66 20 80 30 90 -50
Part-a, it is a special array
Part-b, the minimum number of decreases needed is: 0
Example 2
The array is: 50 30 40 -6 10 -80 80 0 51
Part-a, it is a special array
Part-b, the minimum number of decreases needed is: 0
Example 3
The array is: 1234 56789 10
Part-a, it is not a special array
Part-b, the minimum number of decreases needed is: 8
Transcribed Image Text:Given an integer array (data type: int) arr[10] with the initialized values for all its elements. For all the elements from arr[0] to arr[9], i.e., arr[0], arr[1], arr[2], ..., arr[8], arr[9], we call this array to be special, if it satisfies either one of two cases below: Case 1: arr[0] < arr[1], arr[1] > arr[2], arr[2] < arr[3], arr[3] > arr[4], .. Case 2: arr[0] > arr[1], arr[1] < arr[2], arr[2] > arr[3], arr[3] < arr[4], .. In other words, If the array satisfies one of following two conditions, it is a special array. • For Case 1, each odd-index element is greater than its neighbor(s). Because arr[9] has only one neighbor arr[8], we only need to consider arr[8] < arr[9] for arr[9]. • For Case 2, each even-index element is greater than its neighbor(s). Because arr[0] has only one neighbor arr[1], we only need to consider arr[0] > arr[1] for arr[0]. Part-(a). Write a program to determine whether this array is special or not. Part-(b). In this part, we can decrease any element by one each time. If the array is not special, determine the minimum number of decreases needed to change the array to be special. For example, if the array contains the following ten numbers "1, 2, 3, 4, 5, 6, 7, 8, 9, 10", it is not a special array. However, by decreasing "3 to 1", "5 to 3", "7 to 5" and "9 to 7", the array with these ten new numbers "1, 2, 1, 4, 3, 6, 5, 8, 7, 10" becomes special. The total number of decreases is 8 (= 2 + 2 +2 + 2), which is the minimum number of decreases needed. Note 1: for either Case 1 or Case 2, we only consider ">" and "<", not ">" and "S". Note 2: for part-b, only decreasing (by one) can be performed, not increasing. Expected Outputs (Inputs are underlined): Example 1 The array is: 15 39 30 66 20 80 30 90 -50 Part-a, it is a special array Part-b, the minimum number of decreases needed is: 0 Example 2 The array is: 50 30 40 -6 10 -80 80 0 51 Part-a, it is a special array Part-b, the minimum number of decreases needed is: 0 Example 3 The array is: 1234 56789 10 Part-a, it is not a special array Part-b, the minimum number of decreases needed is: 8
Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY