-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path10_Consecutive.cpp
64 lines (51 loc) · 1.77 KB
/
10_Consecutive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// For this challenge you will determine how to order an array of numbers consecutively.
// have the function Consecutive(arr) take the array of integers stored in arr and return the minimum number of integers needed to make the contents of arr consecutive from the lowest number to the highest number. For example: If arr contains [4, 8, 6] then the output should be 2 because two numbers need to be added to the array (5 and 7) to make it a consecutive array of numbers from 4 to 8. Negative numbers may be entered as parameters and no array will have less than 2 elements.
#include <iostream>
#include <string>
using namespace std;
// Function for sorting the array
void bubbleSort(int arr[], int size)
{
int temp;
bool swap;
do
{
swap = false;
for (int x = 0; x < size - 1; x++)
{
if (arr[x] > arr[x + 1])
{
temp = arr[x];
arr[x] = arr[x + 1];
arr[x + 1] = temp;
swap = true;
}
}
} while (swap);
}
int Consecutive(int arr[], int size) {
int difference;
int total = 0;
// Sort the array in ascending order
bubbleSort(arr, size);
// Step through the array and analyze the difference between each value
// Since it is in order we just compare the values next to each other and find the gap
for (int x = 0; x < size-1; x++)
{
difference = arr[x + 1] - arr[x];
total += (difference - 1);
}
return total;
}
int main() {
// keep this function call here
/* Note: In C++ you first have to initialize an array and set
it equal to the stdin to test your code with arrays. */
int A[] = { 5, 10, 15 };
int B[] = { -2, 10, 4 };
int C[] = {4,8,6};
cout << Consecutive(A, sizeof(A) / sizeof(A[0])) << endl; // 8
cout << Consecutive(B, sizeof(B) / sizeof(B[0])) << endl; // 10
cout << Consecutive(C, sizeof(C) / sizeof(C[0])) << endl; // 2
return 0;
}