Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Print elements that can be added to form a given sum
This program finds and prints elements from a given array that can be added together to form a specified sum. The algorithm uses a greedy approach where it iterates through the array and selects elements that don't exceed the remaining sum value.
Algorithm
The greedy algorithm works as follows −
START
STEP 1 ? Take number of elements and array values from user
STEP 2 ? Take the target sum from user
STEP 3 ? For i = 0 to n-1
STEP 4 ? If (current_sum - array[i]) >= 0 then
STEP 4.1 ? current_sum = current_sum - array[i]
STEP 4.2 ? Print array[i]
END If
END For
STOP
Syntax
Following is the basic syntax for dynamic memory allocation in C −
int *ptr = (int*)malloc(sizeof(int) * n);
Following is the syntax for accessing array elements using pointer arithmetic −
*(ptr + i) // Access element at index i
Example
Following C program implements the greedy algorithm to find elements that sum to a target value −
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr, n, i, sum;
printf("Enter number of elements: ");
scanf("%d", &n);
// Dynamically allocate memory for array
ptr = (int*)malloc(sizeof(int) * n);
printf("Enter %d elements: ", n);
for(i = 0; i < n; i++) {
scanf("%d", (ptr + i));
}
printf("Enter the target sum: ");
scanf("%d", &sum);
printf("Elements that can form the sum: ");
// Greedy approach to find elements
for(i = 0; i < n; i++) {
if(sum - *(ptr + i) >= 0) {
sum -= *(ptr + i);
printf("%d ", *(ptr + i));
}
}
printf("<br>");
free(ptr); // Free allocated memory
return 0;
}
The output of the above code is −
Enter number of elements: 5 Enter 5 elements: 3 1 6 5 7 Enter the target sum: 10 Elements that can form the sum: 3 1 6
How It Works
The algorithm processes elements sequentially and makes locally optimal choices −
Step 1 − Start with target sum = 10, check element 3. Since 10 - 3 = 7 ? 0, select 3. Remaining sum = 7.
Step 2 − Check element 1. Since 7 - 1 = 6 ? 0, select 1. Remaining sum = 6.
Step 3 − Check element 6. Since 6 - 6 = 0 ? 0, select 6. Remaining sum = 0.
Step 4 − Check element 5. Since 0 - 5 = -5
Step 5 − Check element 7. Since 0 - 7 = -7
Different Input Example
Following example shows the program behavior with different input values −
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[] = {2, 4, 1, 8, 3};
int n = 5;
int sum = 9;
int i;
printf("Array: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nTarget sum: %d<br>", sum);
printf("Selected elements: ");
for(i = 0; i < n; i++) {
if(sum - arr[i] >= 0) {
sum -= arr[i];
printf("%d ", arr[i]);
}
}
printf("\nRemaining sum: %d<br>", sum);
return 0;
}
The output shows how different arrays produce different results −
Array: 2 4 1 8 3 Target sum: 9 Selected elements: 2 4 1 Remaining sum: 2
Key Points
Greedy Selection − The algorithm doesn't find the optimal subset sum solution, but rather selects elements in order if they fit within the remaining sum.
Sequential Processing − Elements are processed in the order they appear in the array, not by value optimization.
Memory Management − Uses dynamic memory allocation with
malloc()and should includefree()to prevent memory leaks.Pointer Arithmetic − Demonstrates accessing array elements using
*(ptr + i)notation.
Limitations
This greedy approach has important limitations −
Not Optimal − May not find the optimal combination. For array [4, 3, 2, 1] with sum 6, it selects [4, 2] instead of optimal [3, 2, 1].
Order Dependent − Results change based on input order of elements.
Incomplete Solutions − May leave remaining sum > 0 even when exact solutions exist.
Conclusion
This program demonstrates a simple greedy approach to subset selection where elements are chosen sequentially if they fit within the remaining target sum. While not optimal for subset sum problems, it provides a clear example of dynamic memory allocation, pointer arithmetic, and greedy algorithm implementation in C.
