-
Notifications
You must be signed in to change notification settings - Fork 1
/
Array220.cpp
36 lines (25 loc) · 1.03 KB
/
Array220.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
/*
Given an array of ints, compute recursively if the array contains somewhere a value followed in the array by that value times 10. We'll use the convention of considering only the part of the array that begins at the given index. In this way, a recursive call can pass index+1 to move down the array. The initial call will pass in index as 0.
array220([1, 2, 20], 0) → true
array220([3, 30], 0) → true
array220([3], 0) → false
*/
#include <iostream>
#include <vector>
using namespace std;
bool array220(vector<int>& arr, int index){
if(index > arr.size() || arr.size() == 1)
return false;
if(arr[index]*10 == arr[index+1])
return true;
else
return array220(arr, index+1);
}
int main(){
vector<int> arr1{1, 2, 20};
cout << "array220([1, 2, 20], 0)" << " -> " << array220(arr1, 0) << endl;
vector<int> arr2{3, 30};
cout << "array220([3, 30], 0)" << " -> " << array220(arr2, 0) << endl;
vector<int> arr3{3};
cout << "array220([3], 0)" << " -> " << array220(arr3, 0) << endl;
}