KTU Max Activity Points
The CSE dept is organizing a tech fest with so many exciting events. By participating in an event,you can claim for activity points as stipulated by KTU. Each event i gives you A[i] activity points where A is an array.If you are not allowed to participate in more than k events, what’s the max number of points that you can earn?
📌 Problem Statement
-
You have
n
events. -
Each event
i
givesA[i]
activity points. -
You can participate in at most
k
events. -
Goal → maximize activity points earned.
👉 This means: pick the top k largest values from A.
📌 Algorithm
-
Input number of events
n
and arrayA
. -
Sort
A
in descending order. -
Take sum of first
k
elements. -
Print maximum points.
C Program Implementation
#include <stdio.h>
// Function to sort array in descending order using bubble sort
void sortDescending(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int n, k;
printf("Enter number of events: ");
scanf("%d", &n);
int A[n];
printf("Enter activity points for each event:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &A[i]);
}
printf("Enter maximum events you can participate in (k): ");
scanf("%d", &k);
// Step 1: Sort in descending order
sortDescending(A, n);
// Step 2: Sum top k elements
int maxPoints = 0;
for (int i = 0; i < k && i < n; i++) {
maxPoints += A[i];
}
printf("Maximum Activity Points: %d\n", maxPoints);
return 0;
}
Comments
Post a Comment