-
Notifications
You must be signed in to change notification settings - Fork 2
/
replace_min_max_arr.cpp
80 lines (63 loc) · 1.36 KB
/
replace_min_max_arr.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/****************************************************************************
File name: replace_min_max_arr.cpp
Author: babajr
*****************************************************************************/
/*
Replace all min elements in an array with max elements.
Input: [1, 1, 3, 4, 5]
Output: [5, 5, 3, 4, 1]
*/
#include <iostream>
#include <cassert>
using namespace std;
#define TRUE 1
#define FALSE 0
int getMax(int arr[], int size)
{
int max = arr[0];
for(int i = 1; i < size; i++)
{
if(arr[i] > max)
max = arr[i];
}
return max;
}
int getMin(int arr[], int size)
{
int min = arr[0];
for(int i = 1; i < size; i++)
{
if(arr[i] < min)
min = arr[i];
}
return min;
}
void replaceMinMax(int arr[], int size)
{
int min = getMin(arr, size);
int max = getMax(arr, size);
for(int i = 0; i < size; i++)
{
if(arr[i] == min)
arr[i] = max;
else if(arr[i] == max)
arr[i] = min;
}
}
void printArray(int arr[], int size)
{
for(int i = 0; i < size; ++i)
cout<<arr[i]<<" ";
cout<<"\n";
}
int main()
{
const int size = 5;
int arr[size] = {1, 1, 3, 5, 5};
cout<<"Original Array: \n";
printArray(arr, size);
cout<<"Modified Array: \n";
replaceMinMax(arr, size);
printArray(arr, size);
return 0;
}