C Program To Find Maximum Difference Between Two Elements in an Array

«
»
This C Program checks 2 elements in the array such that difference between them is largest. This program finds maximum differnce between the 2 array elements.

Here is source code of the C Program to find 2 elements in the array such that difference between them is largest.. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Find 2 Elements in the Array such that Difference between them is Largest
  3.  */
  4.  #include <stdio.h>
  5.  
  6. int maximum_difference(int array[], int arr_size)
  7. {
  8.     int max_diff = array[1] - array[0];
  9.     int i, j;
  10.     for (i = 0; i < arr_size; i++)
  11.     {
  12.         for (j = i + 1; j < arr_size; j++)
  13.         {
  14.             if (array[j] - array[i] > max_diff)
  15.                 max_diff = array[j] - array[i];
  16.         }
  17.     }
  18.     return max_diff;
  19. }
  20.  
  21. int main()
  22. {
  23.     int array[] = {10, 15, 90, 200, 110};
  24.     printf("Maximum difference is %d",  maximum_difference(array, 5));
  25.     getchar();
  26.     return 0;
  27. }

$ cc pgm97.c
$ a.out
Maximum difference is 190

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.