forked from 7harshit20/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_challenges.cpp
89 lines (85 loc) · 1.66 KB
/
array_challenges.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
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;
void printArray(int arr[], int n)
{
for(int j=0; j < n; j++)
{
cout << arr[j] << " ";
}
cout << endl;
}
void maxtilli(int arr[], int n)
{
for(int i=1; i < n; i++)
{
arr[i]=max(arr[i], arr[i - 1]);
}
printArray(arr, n);
}
void sumOfSubarray(int arr[], int n)
{
int sum=0;
for(int i=0; i < n; i++)
{
for(int j=i; j < n; j++)
{
sum=sum + arr[j];
cout << sum << " ";
}
sum=0;
}
}
int longestAirthmeticSubarray(int arr[], int n)
{
int pointer=0, highestlength=1, length=1;
for(int i=0; i < n - 1; i++)
{
arr[i]=arr[i + 1] - arr[i];
}
while(pointer < n - 1)
{
while(arr[pointer + 1] == arr[pointer])
{
length++;
pointer++;
}
highestlength=max(highestlength, length);
length=1;
pointer++;
}
return highestlength + 1;
}
int recordBreaking(int arr[], int n)
{
int number=0;
for(int i=1; i < n - 1; i++)
{
if(arr[i] > arr[i - 1] && arr[i] > arr[i + 1])
{
number++;
}
arr[i]=max(arr[i], arr[i - 1]);
}
if(arr[0] > arr[1])
number++;
if(arr[n - 1] > arr[n - 2])
number++;
return number;
}
int main()
{
int n;
cout << "enter the size of array\n";
cin >> n;
int arr[n];
cout << "enter the array\n";
for(int i=0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// maxtilli(arr,n);
sumOfSubarray(arr, n);
// cout<<longestAirthmeticSubarray(arr,n)<<endl;
// cout<<recordBreaking(arr,n)<<endl;
return 0;
}