Skip to content

Commit 6907df8

Browse files
committed
Create problem25.c
1 parent ebd15fe commit 6907df8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

problem25.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//By T.Poovarasan , ECE
2+
3+
#include<stdio.h>
4+
5+
/*Function to return max sum such that no two elements
6+
are adjacent */
7+
int FindMaxSum(int arr[], int n)
8+
{
9+
int incl = arr[0];
10+
int excl = 0;
11+
int excl_new;
12+
int i;
13+
14+
for (i = 1; i < n; i++)
15+
{
16+
/* current max excluding i */
17+
excl_new = (incl > excl)? incl: excl;
18+
19+
/* current max including i */
20+
incl = excl + arr[i];
21+
excl = excl_new;
22+
}
23+
24+
/* return max of incl and excl */
25+
return ((incl > excl)? incl : excl);
26+
}
27+
28+
/* Driver program to test above function */
29+
int main()
30+
{
31+
int arr[] = {5, 5, 10, 100, 10, 5};
32+
printf("%d \n", FindMaxSum(arr, 6));
33+
getchar();
34+
return 0;
35+
}
36+
//Time Complexity: O(n)
37+

0 commit comments

Comments
 (0)