-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimize_the_Heights.cpp
More file actions
55 lines (51 loc) · 1.04 KB
/
Minimize_the_Heights.cpp
File metadata and controls
55 lines (51 loc) · 1.04 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
#include <bits/stdc++.h>
using namespace std;
void printArray(int arr[], int n)
{
int i = 0;
cout << "ARRAY THUS FORMED IS : ";
while (i < n)
{
cout << arr[i++] << " ";
}
}
void minimizeTheHeight(int arr[], int n, int k)
{
int max = arr[0], min = INT_MAX;
for (int i = 0; i < n; i++)
{
if ((arr[i] - k) <= 0)
{
arr[i] += k;
}
else
{
arr[i] -= k;
}
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
}
printArray(arr, n);
cout << " max : " << max;
cout << " min : " << min;
cout << endl
<< "MINIMIZED HEIGHT : " << max - min;
}
int main()
{
int n, i = 0, k;
cout << "Enter the number of Elements : ";
cin >> n;
int arr[n];
cout << "Enter the Elements : ";
while (i < n)
{
cin >> arr[i++];
}
printArray(arr, n);
cout << "Enter Balancing Factor (i.e. +ve k ) : ";
cin >> k;
minimizeTheHeight(arr, n, k);
}