-
Notifications
You must be signed in to change notification settings - Fork 0
/
35_array_problem_1.cpp
72 lines (58 loc) · 1.25 KB
/
35_array_problem_1.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
/* Max still i
Given an array arr[] of size n. For every i from 0 to n-1 output max (arr[0],arr[1],....,arr[i])
example:
arr[] = {1,0,5,4,6,8}
max still i = 1 1 5 5 6 8
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[]={5,-4,8,14,6,28};
int n = (sizeof(arr)/sizeof(arr[0]));
int max =arr[0];
for(int i =0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
if(max < arr[j])
{
max = arr[j];
}
}
cout<<max <<" ";
}
}
//2nd method
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[]={5,-4,8,14,6,28};
int n = (sizeof(arr)/sizeof(arr[0]));
int mx =-1999999;
for(int i =0;i<n;i++)
{
mx = max(mx,arr[i]);
cout<< mx<<" ";
}
}
//another way......................................
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[]= {5,-4,8,14,6,28};
int n = (sizeof(arr)/sizeof(arr[0]));//8
int maxi = arr[0]; // 2
cout<<maxi<<" ";//2
for(int i =1; i<n; i++)//i = 1 2 3 4 5 6 7
{
int j = i-1;//j = 0
if(maxi<arr[i])//11 < 18 arr[3]
{
maxi = arr[i];//maxi = 18
}
cout<<maxi<<" ";// 11 11 18
}
}