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 gives A[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

  1. Input number of events n and array A.

  2. Sort A in descending order.

  3. Take sum of first k elements.

  4. 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

Popular posts from this blog

Data Structures Lab PCCSL307 KTU 2024 Scheme - Dr Binu V P

Lab Assignments

Singly Linked List - Operations