Skip to content

Commit 8d974b9

Browse files
committed
Day-71 Two divide and conquer problems
1 parent 4bada00 commit 8d974b9

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 101 |
10-
| Current Streak | 70 days |
11-
| Longest Streak | 70 ( August 17, 2015 - October 25, 2015 ) |
9+
| Total Problems | 103 |
10+
| Current Streak | 71 days |
11+
| Longest Streak | 71 ( August 17, 2015 - October 26, 2015 ) |
1212

1313
</center>
1414

@@ -150,6 +150,8 @@ Include contains single header implementation of data structures and some algori
150150
| :------------ | :----------: |
151151
| Given a sorted vector, return first index of the occurrence of a value in vector, if number does not exist, return -1 | [first_occurrence_binary_search.cpp](sort_search_problems/first_occurrence_binary_search.cpp) |
152152
| Given a list of unsorted integers, A={a<sub>1</sub>,a<sub>2</sub>,…,a<sub>N</sub>}, Find the pair of elements that have the smallest absolute difference between them? If there are multiple pairs, find them all.| [closest_numbers.cpp](sort_search_problems/closest_numbers.cpp)|
153+
| Given a sorted array, determine index of fixed point in this array. If array does not have a fixed point return -1. An array has a fixed point when index of the element is same as index i.e. i == arr[i], Expected time complexity O(logn)| [fixedPoint.cpp](sort_search_problems/fixedPoint.cpp)|
154+
| Find the maximum element in an array which is first increasing and then decreasing. Input: arr[] = {8, 10, 20, 80, 100, 200, 400, 500, 3, 2, 1}, output : 500. Array may be strictly increasing or decreasing as well. ExpectedTime complexity is O(logn).| [findMaximum.cpp](sort_search_problems/findMaximum.cpp)|
153155

154156
### Graph Problems
155157
| Problem | Solution |

sort_search_problems/findMaximum.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Find the maximum element in an array which is first increasing and then decreasing.
3+
* Input: arr[] = {8, 10, 20, 80, 100, 200, 400, 500, 3, 2, 1}
4+
* Output: 500
5+
*
6+
* Also consider cases like
7+
* Input: arr[] = {10, 20, 30, 40, 50}
8+
* Output: 50
9+
*
10+
* Input: arr[] = {120, 100, 80, 20, 0}
11+
* Output: 120
12+
*/
13+
14+
#include <iostream>
15+
#include <vector>
16+
17+
int findMaximum(std::vector<int> & arr, int low, int high) {
18+
// subset has only one element
19+
if ( high == low ) {
20+
return arr[low];
21+
}
22+
23+
//subset has 2 elements
24+
if ( high - low == 1 ) {
25+
return arr[high] > arr[low] ? arr[high] : arr[low];
26+
}
27+
28+
int mid = ( high + low )/2;
29+
if ( arr[mid-1] > arr[mid] ) {
30+
return findMaximum(arr, low, mid-1);
31+
} else {
32+
return findMaximum(arr, mid+1, high);
33+
}
34+
35+
}
36+
37+
void printVec( std::vector<int> & arr ) {
38+
std::cout << "Arr:";
39+
for ( auto & a : arr ) {
40+
std::cout << a << " ";
41+
}
42+
std::cout << std::endl;
43+
}
44+
45+
46+
int main() {
47+
std::vector<int> arr1{ 8, 10, 20, 80, 100, 200, 400, 500, 3, 2, 1 };
48+
printVec(arr1);
49+
std::cout << "Max in above arr:" << findMaximum( arr1, 0, arr1.size()-1 ) << std::endl;
50+
51+
std::vector<int> arr2{1, 3, 50, 10, 9, 7, 6};
52+
printVec(arr2);
53+
std::cout << "Max in above arr:" << findMaximum( arr2, 0, arr2.size()-1 ) << std::endl;
54+
55+
56+
std::vector<int> arr3{10, 20, 30, 40, 50};
57+
printVec(arr3);
58+
std::cout << "Max in above arr:" << findMaximum( arr3, 0, arr3.size()-1 ) << std::endl;
59+
60+
std::vector<int> arr4{120, 100, 80, 20, 0};
61+
printVec(arr4);
62+
std::cout << "Max in above arr:" << findMaximum( arr4, 0, arr4.size()-1 ) << std::endl;
63+
64+
return 0;
65+
}

sort_search_problems/fixedPoint.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Given a sorted array of numbers, determine if there exists a fixed point, if so return index, or return -1. Fixed point in array is when i = arr[i].
3+
*/
4+
5+
#include<iostream>
6+
#include<vector>
7+
8+
int binarySearchFixedPoint( const std::vector<int> & arr) {
9+
int high = arr.size() -1;
10+
int low = 0;
11+
while ( low <= high ) {
12+
int mid = (low + high)/2;
13+
if ( arr[mid] == mid ) {
14+
return mid;
15+
} else if ( mid > arr[mid] ) {
16+
low = mid + 1;
17+
} else {
18+
high = mid - 1;
19+
}
20+
}
21+
return -1;
22+
}
23+
24+
void printVec( std::vector<int> & vec ) {
25+
std::cout << "VEC:";
26+
for ( auto & v : vec ) {
27+
std::cout << v << " ";
28+
}
29+
std::cout << std::endl;
30+
}
31+
32+
int main() {
33+
std::vector<int> vec { -10, -1, 0, 3, 10, 11, 30, 50, 100 };
34+
printVec(vec);
35+
int fixedPoint = binarySearchFixedPoint(vec);
36+
if (fixedPoint != -1) {
37+
std::cout << "Above vector has a fixed point at index(0 indexed): " << fixedPoint << std::endl;
38+
} else {
39+
std::cout << "Above vector has no fixed point\n";
40+
}
41+
return 0;
42+
}

0 commit comments

Comments
 (0)