-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc42.c
More file actions
32 lines (31 loc) · 817 Bytes
/
c42.c
File metadata and controls
32 lines (31 loc) · 817 Bytes
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
#include<stdio.h>//to minimize time complexity
//max sum of k consecutive elements in an array(-ve and +ve find)
int maxarraysum(int a[],int n,int k){
int winsum=0;
for(int i=0;i<k;i++)
{
winsum+=a[i];
}
int maxsum=winsum;
for(int j=k;j<n;j++)
{
winsum+=a[j]-a[j-k];
if(winsum>maxsum){
maxsum=winsum;}
}
return maxsum;
}
int main()
{
int a[7]={1,2,-4,-5,4,3,7},max,i,j,k=3,n;
int count=0,odd=0;
n= sizeof(a)/sizeof(a[0]);
int data = maxarraysum(a,n,k);
printf("Maximum sum of %d consecutive elements is %d",k,data);
for(int l=k+1,maxsum;l<n;l++){
if(a[l]<0){
count++;}
else{ odd++;}}
printf("\ncount of negative numbers is %d",count);
printf("\ncount of positive numbers is %d",odd);
}