-
Notifications
You must be signed in to change notification settings - Fork 1
/
maxm_difference.c
58 lines (53 loc) · 1.12 KB
/
maxm_difference.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*Given an array of integers, a, return the maximum difference of any pair of numbers such that the larger integer in the pair occurs at a higher index
(in the array) than the smaller integer. Return -1 if you cannot find a pair that satisfies this condition.*/
#include<stdio.h>
int max(int x, int y)
{
int m;
if(x>y)
{
m=x;
}
else
{
m-y;
}
return m;
}
int maximumD(int arr[], int arr_size)
{
int maxm = -1;
int i, j, k;
for (i = 0; i < arr_size-1; i++)
{
for (j = i+1; j < arr_size; j++)
{
if (arr[j] - arr[i] > maxm)
{
maxm = arr[j] - arr[i];
k=max(k,maxm);
}
}
}
return maxm;
}
void main()
{
int x, i;
printf("Enter the array size: ");
scanf("%d",&x);
int arr[x];
for(i=0;i<x;i++)
{
printf("Enter element number %d ",i+1);
scanf("%d",&arr[i]);
}
printf("Entered elements are:\n");
for(i=0;i<x;i++)
{
printf("%d\t",arr[i]);
}
int p;
p=maximumD(arr,x);
printf("\nMaximum difference=%d",p);
}