Find the smallest number in the first array that is not in the second one I have solved the given assignment below is the question and getting correct answer. but my question is is there any way to reduce the complexity of my code or there is any other efficient way to write the code?


  Given two arrays of integers output the smallest number in the first
  array not present in the second one.
  
  Input Specification:
  
  The first line contains the size N1 of the first array.   Next line
  give the contents of the first array.   Next line contains the size N2
  of the second array.   Next line give the contents of the second
  array.  
  
  Output Format:
  
  Output must be a single number which is the smallest number occurring
  in the first array that does not occur in the second. In case there is
  no such number, output NO.
  
  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 3 5 7

  
  Output: 2
  
  Example Input:

1
1
2
1 2

  
  Output: NO


Here is the code.

#include&lt;stdio.h&gt;
int main(void)
{
    int a[10],b[10];
    int small=0,l1,l2,i,flag=0;
    scanf("%d",&amp;l1);
    for(i=0;i&lt;l1;i++)
    {
        scanf("%d",&amp;a[i]);
    }
    scanf("%d",&amp;l2);
    for(i=0;i&lt;l2;i++)
    {
        scanf("%d",&amp;b[i]);
    }
    small=a[0];
    for(i=1;i&lt;l1;i++)
    {
        if(small&gt;a[i])
          small=a[i];
    }
    for(i=0;i&lt;l2;i++)
    {
        if(small==b[i])
          flag=1;
    }
        if(flag==1)
        {
            printf("NO");
        }
        else
        {
            printf("%d",small);
        } 
   return 0;
}
Why do you make your arrays only 10 elements large when the task explicitly says 20?

Even worse, you don't protect the program from writing outside the array. The C programming language doesn't protect against this wrong behavior, therefore you as the programmer must do this.

Before every write to an array you must check that the array index is valid. For example:

assert(0 &lt;= idx &amp;&amp; idx &lt; arr_len);
arr[idx] = value;


Only if you can prove mathematically that the assert condition is always true, can you leave out the assert. Follow this advice, and your programs will be better than 80% of the existing C programs.



To reduce the complexity of the main function, you should define a function like this:

bool intarray_contains(const int *arr, size_t arr_len, int value) {
    ...
}


Then you don't need to write this code in main.



After that, to ensure that your program works correctly (it currently doesn't), you should extract the calculation into a separate function:

int min_not_present_in(const int *arr1, size_t arr1_len, const int *arr2, size_t arr2_len) {
    ...
}


This function definition allows you to do the calculation without entering the values every time. Like this:

int positive[] = { 1, 2, 3 };
int odd[] = { 1, 3, -1 };
int even[] = { -2, 0, 2 };

assert(min_not_present_in(positive, 3, odd, 3) == 2);
assert(min_not_present_in(positive, 3, even, 3) == 1);


To make all this work, you need the following lines at the top of your file:

#include &lt;assert.h&gt;
#include &lt;stdbool.h&gt;