Ticker

6/recent/ticker-posts

Program of chocolate distribution in C

 Program of chocolate distribution in C


In this program or problem array of chocolate given to 4 person but distributed in which gap between Max and min is minimum so first array is sort using bubble sort and then calculate gap b/w Max and min.

 int a[6]={2,4,5,8,9,1};

    int m=4;

First sort array using bubble sort so array is 

a[6]={1,2,4,5,8,9}

Than make window of  size 4 

1,2,4,5. gap is =4

2,4,5,8 gap is 6

4,5,8,9 gap is 5

So distribution window is {1,2,4,5} and gap is 4.


Code of chocolate distribution problem


#include <stdio.h>

int main() {

    int a[6]={2,4,5,8,9,1};

    int m=4;

    int n=6;

    int i;

    int j;

    int maxw;

    int minw;

    int temp;

    int gap;

    int ans=9999;

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

    {

        for(j=0;j<n;j++)

        {

            if(a[j]>a[j+1])

            {

                temp=a[j];

                a[j]=a[j+1];

                a[j+1]=temp;

            }

        }

    }

    for(i=0;i<=n-m;i++)

    {

        minw=a[i];

        maxw=a[i+m-1];

        gap=maxw-minw;

        if(gap<ans)

        {

            ans=gap;

        }

    }

    

    printf("%d",ans);

}


Output

4

Post a Comment

0 Comments