import java.util.*;
public class FirstFit {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter no. of jobs: ");
        int jobn = sc.nextInt();
        System.out.println("Enter jobs: ");
        int job[] = new int[jobn];
        for (int i = 0; i < jobn; i++) {
            job[i] = sc.nextInt();
        }

        System.out.println("Enter no. of blocks: ");
        int block = sc.nextInt();
        System.out.println("Enter Blocks: ");
        int blocks[] = new int[block];
        int total = 0;
        for (int i = 0; i < block; i++) {
            blocks[i] = sc.nextInt();
            total += blocks[i];
        }

        int sumAllocatedJobs = 0;

        for (int i = 0; i < jobn; i++) {
            int isAlloc = 0;
            for (int j = 0; j < block; j++) {
                if (job[i] <= blocks[j]) {
                    sumAllocatedJobs += job[i];
                    System.out.println(" Job " + job[i] + " allocated to block " + (j + 1));
                    blocks[j] -= job[i];  // reduce block size (not -1, so it behaves like BestFit code style)
                    isAlloc = 1;
                    break;
                }
            }
            if (isAlloc == 0) {
                System.out.println(" Job " + job[i] + " cannot be allocated.");
            }
        }

        double avg_first = (double) sumAllocatedJobs / total;
        System.out.printf("avg_first:- %.2f\\n", avg_first);
    }
}

image.png