Skip to content

Commit 4d0eb79

Browse files
authored
ARRAYS-Min_&_Max (#3)
* Add files via upload * Delete Min_Max_array.cpp * Delete README.md * Delete image1.PNG * Delete image2.PNG * Delete image3.PNG * Delete input.PNG * Delete output.PNG * Add files via upload * Delete Min_Max_array.cpp * Delete README.md * Delete image1.PNG * Delete image2.PNG * Delete image3.PNG * Delete input.PNG * Delete output.PNG * Create ARRAYS-Min_&_Max * Delete ARRAYS-Min_&_Max * ARRAYS-Min_&_Max
1 parent cee684f commit 4d0eb79

8 files changed

Lines changed: 259 additions & 13 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// C++ program of above implementation
2+
#include<iostream>
3+
using namespace std;
4+
5+
// Structure is used to return
6+
// two values from minMax()
7+
struct Pair
8+
{
9+
int min;
10+
int max;
11+
};
12+
13+
struct Pair getMinMax(int arr[], int n)
14+
{
15+
struct Pair minmax;
16+
int i;
17+
18+
// If array has even number of elements
19+
// then initialize the first two elements
20+
// as minimum and maximum
21+
if (n % 2 == 0)
22+
{
23+
if (arr[0] > arr[1])
24+
{
25+
minmax.max = arr[0];
26+
minmax.min = arr[1];
27+
}
28+
else
29+
{
30+
minmax.min = arr[0];
31+
minmax.max = arr[1];
32+
}
33+
34+
// Set the starting index for loop
35+
i = 2;
36+
}
37+
38+
// If array has odd number of elements
39+
// then initialize the first element as
40+
// minimum and maximum
41+
else
42+
{
43+
minmax.min = arr[0];
44+
minmax.max = arr[0];
45+
46+
// Set the starting index for loop
47+
i = 1;
48+
}
49+
50+
// In the while loop, pick elements in
51+
// pair and compare the pair with max
52+
// and min so far
53+
while (i < n - 1)
54+
{
55+
if (arr[i] > arr[i + 1])
56+
{
57+
if(arr[i] > minmax.max)
58+
minmax.max = arr[i];
59+
60+
if(arr[i + 1] < minmax.min)
61+
minmax.min = arr[i + 1];
62+
}
63+
else
64+
{
65+
if (arr[i + 1] > minmax.max)
66+
minmax.max = arr[i + 1];
67+
68+
if (arr[i] < minmax.min)
69+
minmax.min = arr[i];
70+
}
71+
72+
// Increment the index by 2 as
73+
// two elements are processed in loop
74+
i += 2;
75+
}
76+
return minmax;
77+
}
78+
79+
// Driver code
80+
int main()
81+
{
82+
// int arr[] = { 1000, 11, 445,
83+
// 1, 330, 3000 };
84+
// int arr_size = 6;
85+
int arr_size;
86+
cout<< "Enter the size";
87+
cin>>arr_size;
88+
int arr[arr_size];
89+
cout<< "Enter the array";
90+
for(int a =0;a<arr[arr_size-1];a++)
91+
{
92+
cin>>arr[a];
93+
94+
}
95+
96+
97+
Pair minmax = getMinMax(arr, arr_size);
98+
99+
cout << "Minimum element is "
100+
<< minmax.min << endl;
101+
cout << "Maximum element is "
102+
<< minmax.max;
103+
104+
return 0;
105+
}
106+
107+
// This code is contributed by Ananthakrishnan

β€ŽARRAYS-Min_&_Max/README.mdβ€Ž

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Maximum and minimum of an array using minimum number of comparisons✨
2+
3+
- Here I have created a structure named **Pair** (which contains min and max) and ***arr*** is the array elements and ***n*** is the size of array.I have used a method called **Comparison in Pairs** ie,
4+
- If n is odd then initialize min and max as first element.
5+
- If n is even then initialize min and max as minimum and maximum of the first two elements respectively.
6+
- For rest of the elements, pick them in pairs and compare their maximum and minimum with max and min respectively.
7+
# Code ✍
8+
```cpp
9+
// C++ program of above implementation
10+
#include<iostream>
11+
using namespace std;
12+
13+
// Structure is used to return
14+
// two values from minMax()
15+
struct Pair
16+
{
17+
int min;
18+
int max;
19+
};
20+
21+
struct Pair getMinMax(int arr[], int n)
22+
{
23+
struct Pair minmax;
24+
int i;
25+
26+
// If array has even number of elements
27+
// then initialize the first two elements
28+
// as minimum and maximum
29+
if (n % 2 == 0)
30+
{
31+
if (arr[0] > arr[1])
32+
{
33+
minmax.max = arr[0];
34+
minmax.min = arr[1];
35+
}
36+
else
37+
{
38+
minmax.min = arr[0];
39+
minmax.max = arr[1];
40+
}
41+
42+
// Set the starting index for loop
43+
i = 2;
44+
}
45+
46+
// If array has odd number of elements
47+
// then initialize the first element as
48+
// minimum and maximum
49+
else
50+
{
51+
minmax.min = arr[0];
52+
minmax.max = arr[0];
53+
54+
// Set the starting index for loop
55+
i = 1;
56+
}
57+
58+
// In the while loop, pick elements in
59+
// pair and compare the pair with max
60+
// and min so far
61+
while (i < n - 1)
62+
{
63+
if (arr[i] > arr[i + 1])
64+
{
65+
if(arr[i] > minmax.max)
66+
minmax.max = arr[i];
67+
68+
if(arr[i + 1] < minmax.min)
69+
minmax.min = arr[i + 1];
70+
}
71+
else
72+
{
73+
if (arr[i + 1] > minmax.max)
74+
minmax.max = arr[i + 1];
75+
76+
if (arr[i] < minmax.min)
77+
minmax.min = arr[i];
78+
}
79+
80+
// Increment the index by 2 as
81+
// two elements are processed in loop
82+
i += 2;
83+
}
84+
return minmax;
85+
}
86+
87+
// Driver code
88+
int main()
89+
{
90+
// int arr[] = { 1000, 11, 445,
91+
// 1, 330, 3000 };
92+
// int arr_size = 6;
93+
int arr_size;
94+
cout<< "Enter the size";
95+
cin>>arr_size;
96+
int arr[arr_size];
97+
cout<< "Enter the array";
98+
for(int a =0;a<arr[arr_size-1];a++)
99+
{
100+
cin>>arr[a];
101+
102+
}
103+
104+
105+
Pair minmax = getMinMax(arr, arr_size);
106+
107+
cout << "Minimum element is "
108+
<< minmax.min << endl;
109+
cout << "Maximum element is "
110+
<< minmax.max;
111+
112+
return 0;
113+
}
114+
```
115+
116+
117+
118+
119+
120+
121+
# Analysis πŸ‘
122+
<img src="https://github.com/akrish4/DSA/blob/main/dsa-cp-2/ARRAYS-Min_%26_Max/images/image1.PNG" alternate="input">
123+
124+
# Aproach & Example
125+
## If the size of the array is even (n=even)πŸ‘‡
126+
<img src="https://github.com/akrish4/DSA/blob/main/dsa-cp-2/ARRAYS-Min_%26_Max/images/image2.PNG" alternate="input">
127+
128+
## If the size of the array is odd (n=odd)πŸ‘‡
129+
<img src="https://github.com/akrish4/DSA/blob/main/dsa-cp-2/ARRAYS-Min_%26_Max/images/image3.PNG" alternate="input">
130+
131+
# Output Images πŸ’»
132+
## Input :
133+
<img src="https://github.com/akrish4/DSA/blob/main/dsa-cp-2/ARRAYS-Min_%26_Max/images/input.PNG" alternate="input">
134+
135+
## Output :
136+
<img src="https://github.com/akrish4/DSA/blob/main/dsa-cp-2/ARRAYS-Min_%26_Max/images/output.PNG" alternate="input">
137+
138+
# Time Complexity: O(n)
139+
140+
```
141+
If n is odd: 3*(n-1)/2
142+
If n is even: 1 Initial comparison for initializing min and max,
143+
and 3(n-2)/2 comparisons for rest of the elements
144+
= 1 + 3*(n-2)/2 = 3n/2 -2
145+
```
146+
147+
148+
Contributed by [Ananthakrishnan](https://github.com/akrish4) 😊 , If you find it helpful , don't forget to drop a like πŸ’–
149+
##### connect with Ananthakrishnan πŸ§‘βœŒ
150+
[Github](https://github.com/akrish4)
151+
[Linkedin](https://in.linkedin.com/in/Ananthakrishnan-Nair-RS")
152+
254 KB
Loading
216 KB
Loading
197 KB
Loading
7.51 KB
Loading
13.9 KB
Loading

β€ŽREADME.mdβ€Ž

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
Β (0)