
#include <stdio.h>

int main() {
    int n, h, i, p[10], choice;

    printf("DINING PHILOSOPHER PROBLEM\n");
    printf("Enter the total no. of philosophers: ");
    scanf("%d", &n);

    printf("How many are hungry: ");
    scanf("%d", &h);

    for (i = 0; i < h; i++) {
        printf("Enter philosopher %d position: ", i + 1);
        scanf("%d", &p[i]);
    }

    do {
        printf("\n1. One can eat at a time");
        printf("\n2. Two can eat at a time");
        printf("\n3. Exit");
        printf("\nEnter your choice: ");
        scanf("%d", &choice);

        if (choice == 1) {
            for (i = 0; i < h; i++)
                printf("P %d is granted to eat\n", p[i]);
        }
        else if (choice == 2) {
            for (i = 0; i < h; i++) {
                if (i + 1 < h &&
                    (p[i] % n != p[i + 1] % n) &&
                    (p[i] % n != (p[i + 1] + 1) % n) &&
                    (p[i] + 1) % n != p[i + 1] % n) {
                    printf("P %d and P %d are granted to eat\n",
                           p[i], p[i + 1]);
                }
            }
        }
        else if (choice != 3) {
            printf("Invalid choice!\n");
        }

    } while (choice != 3);

    return 0;
}
