Certainly! Below is a C function that calculates the maximum sum of non-adjacent integers in an array. The function uses dynamic programming to efficiently compute the result in linear time.
Algorithm Explanation
The problem is a classic example of the "Maximum Subset Sum with No Adjacent Elements" problem. To solve it, we'll use dynamic programming with the following approach:
-
Initialization:
- Include: The maximum sum including the first element.
- Exclude: The maximum sum excluding the first element.
-
Iteration:
- For each element in the array from the second element onwards:
- New Include: The previous
exclude plus the current element (since we cannot include adjacent elements).
- New Exclude: The maximum of the previous
include and exclude.
-
Result:
- The maximum of the final
include and exclude will be the desired result.
Implementation
Here's the complete C program implementing the above logic:
#include <stdio.h>
#include <stdlib.h>
// Function to find the maximum sum with no adjacent elements
int maxNonAdjacentSum(int arr[], int n) {
if (n == 0)
return 0;
if (n == 1)
return arr[0];
// Initialize include and exclude
int include = arr[0];
int exclude = 0;
int new_include, new_exclude;
for (int i = 1; i < n; i++) {
// Current max excluding i
new_exclude = (include > exclude) ? include : exclude;
// Current max including i
new_include = exclude + arr[i];
// Update include and exclude for next iteration
include = new_include;
exclude = new_exclude;
}
// Return the maximum of include and exclude
return (include > exclude) ? include : exclude;
}
// Helper function to print the array
void printArray(int arr[], int n) {
printf("[");
for(int i=0; i<n; i++) {
printf("%d", arr[i]);
if(i != n-1)
printf(", ");
}
printf("]");
}
// Main function to test the maxNonAdjacentSum function
int main() {
// Example array
int arr[] = {1, 7, 3, 91, 12, 66, 54, 60};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Array: ");
printArray(arr, n);
printf("\n");
int result = maxNonAdjacentSum(arr, n);
printf("Maximum sum of non-adjacent integers: %d\n", result);
// Additional test cases
int test1[] = {5, 5, 10, 100, 10, 5};
int n1 = sizeof(test1) / sizeof(test1[0]);
printf("\nArray: ");
printArray(test1, n1);
printf("\n");
printf("Maximum sum of non-adjacent integers: %d\n", maxNonAdjacentSum(test1, n1));
int test2[] = {3, 2, 7, 10};
int n2 = sizeof(test2) / sizeof(test2[0]);
printf("\nArray: ");
printArray(test2, n2);
printf("\n");
printf("Maximum sum of non-adjacent integers: %d\n", maxNonAdjacentSum(test2, n2));
int test3[] = {3, 2, 5, 10, 7};
int n3 = sizeof(test3) / sizeof(test3[0]);
printf("\nArray: ");
printArray(test3, n3);
printf("\n");
printf("Maximum sum of non-adjacent integers: %d\n", maxNonAdjacentSum(test3, n3));
return 0;
}
Explanation of the Code
-
Function maxNonAdjacentSum:
- Parameters: An integer array
arr and its size n.
- Edge Cases:
- If the array is empty (
n == 0), return 0.
- If the array has only one element, return that element.
- Variables:
include: Maximum sum including the current element.
exclude: Maximum sum excluding the current element.
- Loop: Iterate through the array starting from the second element.
- For each element, calculate
new_include and new_exclude based on the previous include and exclude.
- Update
include and exclude for the next iteration.
- Result: After the loop, the maximum of
include and exclude is the desired maximum sum.
-
Function printArray:
- A helper function to print the array elements in a readable format.
-
main Function:
- Tests the
maxNonAdjacentSum function with multiple test cases, including the one provided [1,7,3,91,12,66,54,60].
- Prints the array and the corresponding maximum sum of non-adjacent integers.
Sample Output
When you run the above program, it will produce the following output:
Array: [1, 7, 3, 91, 12, 66, 54, 60]
Maximum sum of non-adjacent integers: 224
Array: [5, 5, 10, 100, 10, 5]
Maximum sum of non-adjacent integers: 110
Array: [3, 2, 7, 10]
Maximum sum of non-adjacent integers: 13
Array: [3, 2, 5, 10, 7]
Maximum sum of non-adjacent integers: 15
Explanation of the Sample Output
-
First Test Case [1,7,3,91,12,66,54,60]:
- The selected non-adjacent elements are
1, 3, 12, 54, 60 which sum up to 130. However, the optimal selection is 7, 91, 66 which sum up to 224.
-
Second Test Case [5,5,10,100,10,5]:
- The optimal selection is
5, 100, 5 which sums up to 110.
-
Third Test Case [3,2,7,10]:
- The optimal selection is
3, 10 which sums up to 13.
-
Fourth Test Case [3,2,5,10,7]:
- The optimal selection is
3, 5, 7 which sums up to 15.
This function efficiently calculates the maximum possible sum without selecting any two adjacent numbers in the array.