-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindtheMinimumelementinaSortedandRotatedArray.cpp
More file actions
147 lines (113 loc) · 3.15 KB
/
FindtheMinimumelementinaSortedandRotatedArray.cpp
File metadata and controls
147 lines (113 loc) · 3.15 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
Find the minimum element in a sorted and rotated array using Linear Search:
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum value
int findMin(int arr[], int n)
{
int min_ele = arr[0];
// Traversing over array to
// find minimum element
for (int i = 0; i < n; i++) {
if (arr[i] < min_ele) {
min_ele = arr[i];
}
}
return min_ele;
}
// Driver code
int main()
{
int arr[] = { 5, 6, 1, 2, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << findMin(arr, N) << endl;
return 0;
}
THE EASY WAY USING STL
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
vector<int>v{5, 6, 1, 2, 3, 4};
cout<<"The Minimum Element in the vector is: ";
cout<<*min_element(v.begin(),v.end())<<endl;
return 0;
}
Find the minimum element in a sorted and rotated array using Binary Search:
// C++ program to find minimum
// element in a sorted and rotated array
#include <bits/stdc++.h>
using namespace std;
int findMin(int arr[], int low, int high)
{
// This condition is needed to
// handle the case when array is not
// rotated at all
if (high < low)
return arr[0];
// If there is only one element left
if (high == low)
return arr[low];
// Find mid
int mid = low + (high - low) / 2; /*(low + high)/2;*/
// Check if element (mid+1) is minimum element. Consider
// the cases like {3, 4, 5, 1, 2}
if (mid < high && arr[mid + 1] < arr[mid])
return arr[mid + 1];
// Check if mid itself is minimum element
if (mid > low && arr[mid] < arr[mid - 1])
return arr[mid];
// Decide whether we need to go to left half or right
// half
if (arr[high] > arr[mid])
return findMin(arr, low, mid - 1);
return findMin(arr, mid + 1, high);
}
// Driver program to test above functions
int main()
{
int arr[] = { 5, 6, 1, 2, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << "The minimum element is "
<< findMin(arr, 0, N - 1) << endl;
return 0;
}
java solution
import java.lang.*;
class Minimum {
static int solution(int[] a, int low, int high)
{
// This condition is needed to handle the case when
// array is not rotated at all
if (high < low)
return a[0];
// If there is only one element left
if (high == low)
return a[low];
// Find mid
int mid
= low + (high - low) / 2; /*(low + high)/2;*/
// Check if element (mid+1) is minimum element.
// Consider the cases like {3, 4, 5, 1, 2}
if (mid < high && a[mid + 1] < a[mid])
return a[mid + 1];
// Check if mid itself is minimum element
if (mid > low && a[mid] < a[mid - 1])
return a[mid];
// Decide whether we need to go to left half or
// right half
if (a[high] > a[mid])
return solution(a, low, mid - 1);
return solution(a, mid + 1, high);
}
// Driver Program
public static void main(String[] args)
{
int[] a = { 5, 6, 1, 2, 3, 4 };
int N = a.length;
System.out.println("The minimum element is "
+ solution(a, 0, N - 1));
}
}