Find the Min Even Integer and Max Even Integer in an Array

Related searches
int  i,max,min;
int  A[11];
    min = A[1];
    max = A[1];
    for(i=1;i<=10;i++)
    {
        if(min > A[i] && A[i]%2 ==0 )  min = A[i];
        if(max < A[i] && A[i]%2 ==0 ) max = A[i];
    }
    printf("Minimum  Even : %d\n",min);
    printf("Maximum  Even : %d\n",max);
    getch();
}

When I fill my array with 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Why is the minimum even number equal to 1?

This test case exposes one of the dark sides of your code where you have initialized your min and max to A[1] regardless of whether A[1] is odd or even.

Problem:

There may be cases where the array does not contain any even number. In such cases, you may wish to print so instead of printing -1 or INT_MIN or INT_MAX.

If you are new to INT_MIN and INT_MAX, this will serve as a starter before proceeding to the solution.

Solution:

This solution modifies your code in such a way that it handles all the cases given that you provide the inputs without any error.

  1. Have a flag to know whether you found your min and max:

    int foundAnswer = 0;
    
  2. Add the header file limits.h and initialize your min and max as follows:

    min = INT_MAX;
    max = INT_MIN;
    
  3. Modify your loop such that the flag serves a purpose:

    Note:

    Don't waste the zeroth index of your array without any reason. Modify your array declaration and input loop accordingly before changing this one.

    for (i = 0; i < ARRAY_SIZE; ++i)
    {
     if (A[i] % 2 == 0)
     {
      foundAnswer = 1;
      if (A[i] < min) min = A[i];
      if (A[i] > max) max = A[i];
     }
    }
    
  4. Modify your printing code slightly so that all the cases are covered.

    if (foundAnswer)
    {
     // Print min
     // Print max
    }
    else
    {
     // Print "min and max not found"
    }
    

Bonus:

You can learn from the following links in order to optimize your code:

  1. How do I check if an integer is even or odd using bitwise operators

  2. The algorithms discussed here will provide you with a better time complexity to achieve the same.

Maximum and minimum of an array using minimum number of , Write a C function to return minimum and maximum in an array. element as both max and min else if arry_size = 2 one comparison to determine max and min return that pair else If array has even number of elements then. Compute the maximum of the values passed in its argument. Lexicographically largest value if strings are passed as arguments. 1.1. Find largest integer in array >>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] >>> max( nums ) 42 #Max value in array 1.2. Find largest string in array

The problem is that you initialize min and max to an value that might not be even.

The following is an efficient solution that handles empty arrays, arrays containing only odd numbers, and arrays containing only INT_MAX:

int A[] = {1,2,3,4,5,6,7,8,9,10};
size_t n = 10;

size_t found_even = 0;
int min, max;
while (n--) {
   int val = A[n];
   if ((A[n] & 1) == 0) {  // Assumes a 2's complement machine.
      if (found_even++) {
         if      (A[n] < min) min = A[n];
         else if (A[n] > max) max = A[n];
      } else {
         min = val;
         max = val;
      }
   }
}

if (found_even) {
   printf("Minimum even: %d\n", min);
   printf("Maximum even: %d\n", max);
} else {
   printf("Minimum even: <none>\n");
   printf("Maximum even: <none>\n");
}

Slightly faster:

int A[] = {1,2,3,4,5,6,7,8,9,10};
size_t n = 10;

int found_even = 0;
int min, max;
while (n--) {
   int val = A[n];
   if ((A[n] & 1) == 0) {  // Assumes a 2's complement machine.
      min = val;
      max = val;
      found_even = 1;
      break;
   }
}

if (found_even) {
   while (n--) {
      if ((A[n] & 1) == 0) {
         if      (A[n] < min) min = A[n];
         else if (A[n] > max) max = A[n];
      }
   }

   printf("Minimum even: %d\n", min);
   printf("Maximum even: %d\n", max);
} else {
   printf("Minimum even: <none>\n");
   printf("Maximum even: <none>\n");
}

Program to find largest element in an array, Given an array, find the largest element in that given array. Initialize max as int i ;. // Initialize maximum element. int max = arr[0];. // Traverse array elements Program to find the minimum (or maximum) element of an array. Now, find the maximum and minimum numbers in the array using two different functions findmax_ and findmin_ . Save these values in the int variables maximumNo and minimumNo . find max_ method is defined as int find max(int arr[],int size)_ i.e. it takes one array and its size as input and returns one int variable.

min = A[1]; Here min is already initialized to 1. So,

if(min > A[i] && A[i]%2 ==0 ) min = A[i]; condition will never be true.

Add the header file limits.h and initialize min=INT_MAX; max=INT_MIN;

Find minimum and maximum value in an array, Given an array A[] of size n, you need to find the maximum and Your algorithm should make minimum number of comparisons. If the array size is odd, we initialize the first element as both min and max, and if it's even, we� Below is the step by step descriptive logic to find maximum or minimum in array. Input size and element in array, store it in some variable say size and arr. Declare two variables max and min to store maximum and minimum. Assume first array element as maximum and minimum both, say max = arr and min = arr.

C exercises: Find the maximum and minimum element in an array , Write a program in C to find the maximum and minimum element in an the number of elements to be stored in the array :"); scanf("%d",&n); Next: Write a program in C to separate odd and even integers in separate arrays. Find average of each array within an array JavaScript; How to create a function which returns only even numbers in JavaScript array? How can I write a MySQL stored function that calculates the factorial of a given number? How to find the average of elements of an integer array in C#? How to negate the positive elements of an integer array in C#?

C# Sharp Exercises: Separate odd and even integers in separate , Write("\n\nSeparate odd and even integers in separate arrays:\n"); Console. in C# Sharp to find maximum and minimum element in an array. Rotate all odd numbers right and all even numbers left in an Array of 1 to N; Rearrange numbers in an array such that no two adjacent numbers are same; Minimum number greater than the maximum of array which cannot be formed using the numbers in the array; Find Kth number from sorted array formed by multiplying any two numbers in the array

Wap in C to find Max and Min array elements. C Program to find Maximum and Minimum Element of an Array C Programming language tutorial, Sample C programs, C++ Programs, Java Program, Interview Questions, C graphics programming, Data Structures, Binary Tree, Linked List, Stack, Queue, Header files, Design Patterns in Java, Triangle and Star

Comments
  • Why do you waste an extra index of the array?
  • Don't put images to show output. Use proper formatting .
  • Your bug report and the first tip is based on the case where all the elements of the array are similar. I have updated my solution with respect to your suggestions. Thank you @ikegami
  • I appreciate the fact that INT_MAX is odd. I have replaced <= and >= with < and >. However, the real reason to do so is that min and max are initialized to INT_MAX and INT_MIN respectively. Re "I said that findmin and findmax were always the same": You also followed it up with adding an else before my second if into the code which is also visible in the edit history. That would fail in the case where the array size is 1 and the only element is even. Anyways, I have updated my code as needed @ikegami
  • Re: "Ah yes, that's a bug." No worries, it was a nice time working on this answer with you. I'm still a student; you gave me some teamwork experience. Your suggestions and bug reports were spot on. Thank you @ikegami
  • Using min = INT_MAX doesn't work if the array is populated entirely with values equal to INT_MAX (an odd number on 2's complement machines)
  • @ikegami corner cases like that may be handled in code..this is just snippet.
  • What is the smallest even number if all numbers are odd?