Saturday 12 March 2022

NPTEL - WEEK 4 - ASSIGNMENT 4 - QUESTION 01

WEEK 4 - ASSIGNMENT 4 - QUESTION 01

QUESTION :

Given two arrays of integers output the largest number in the
first array not present in the second one.

Input: 

The first line contains the size of the first array.
Next line give the contents of the first array.
Next line contains the size of the second array.
Next line give the contents of the second array.

Output Format:
Output must be a single number which is the largest number occurring
in the first array that does not occur in the second. In case there is
no such number, output 0.

Variable Constraints:
The sizes of the arrays are smaller than 20.
Each array entry is an integer which fits an int data type.

Example:
Input:
3
2 3 4
4
1 4 5 7

Output: 3

Input
1
1
2
1 2

Output: 0

SOLUTION :

#include<stdio.h>

int main()
{
    int arr1[10], max, count, arr2[10], n=0, m=0;

    scanf("%d", &n);
    for(int i=0; i<n; i++)
    {
        scanf("%d", &arr1[i]);
    }

    scanf("%d", &m);
    for(int j=0; j<m; j++)
    {
        scanf("%d", &arr2[j]);
    }

    while(1)
    {
        max = 0;
        count = 0;
        for(int i=0; i<n; i++)
        {
            if(max < arr1[i])
            {
                max = arr1[i];
                count = i;
            }
        }
        for(int j=0; j<m; j++)
        {
            if(max == arr2[j])
            {
               arr1[count] = 0;
              
            }
           
        }
        if(arr1[count] != 0)
        {
            break;
        }
        if(max == 0)
        {
           break;
        }
    }
   
    printf("%d", max);
    return 0;
}

« iCode Buzz »

No comments:

Post a Comment