Posts

Search

Accept of Reject

QUESTION DESCRIPTION

Raju is newly joined employee in the company. His boss has assigned the set of jobs to him. As he is new, he get confused to decide whether to accept or not. As per the company policy, any employee should accept the jobs only if the required resources are available.

Help Raju to take decision whether to accept or not and the order how the jobs can be completed.

TEST CASE 1

INPUT
2
2
3
1
0
1
0
2
3
1
3
4
1
4
OUTPUT
3 1 0 2 
0 1 3 1 
0 0 0 0 
0 1 3 1 
Accept It
1 2

TEST CASE 2

INPUT
2
2
3
4
5
6
2
1
1
0
2
1
3
4
OUTPUT
3 4 2 1 
5 6 1 0 
Dont Accept

Code :

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
    int p, r, i, j, process, count;
    count = 0;

    scanf("%d", &p);

    for(i = 0; i< p; i++)
        completed[i] = 0;

  
    scanf("%d", &r);

 
    for(i = 0; i < p; i++)
    {
        
        for(j = 0; j < r; j++)
            scanf("%d", &Max[i][j]);
    }

   
    for(i = 0; i < p; i++)
    {
      
        for(j = 0; j < r; j++)
            scanf("%d", &alloc[i][j]);
    }

  
    for(i = 0; i < r; i++)
        scanf("%d", &avail[i]);

    for(i = 0; i < p; i++)

        for(j = 0; j < r; j++)
            need[i][j] = Max[i][j] - alloc[i][j];

        do
        {
          

            for(i = 0; i < p; i++)
            {
                for( j = 0; j < r; j++)
                    printf("%d ", Max[i][j]);
               
                for( j = 0; j < r; j++)
                    printf("%d ", alloc[i][j]);
                printf("\n");
            }

            process = -1;

            for(i = 0; i < p; i++)
            {
                if(completed[i] == 0)//if not completed
                {
                    process = i ;
                    for(j = 0; j < r; j++)
                    {
                        if(avail[j] < need[i][j])
                        {
                            process = -1;
                            break;
                        }
                    }
                }
                if(process != -1)
                    break;
            }

            if(process != -1)
            {
                
                safeSequence[count] = process + 1;
                count++;
                for(j = 0; j < r; j++)
                {
                    avail[j] += alloc[process][j];
                    alloc[process][j] = 0;
                    Max[process][j] = 0;
                    completed[process] = 1;
                }
            }
        }
        while(count != p && process != -1);

        if(count == p)
        {
          
            printf("Accept It\n");
            for( i = 0; i < p; i++)
                printf("%d ", safeSequence[i]);
         
        }
        else
            printf("Dont Accept");
  return 0;

}