# Algorithm and Flowchart to find the Maximum Absolute Difference Among the given Array

[375 views]

## Algorithm for Maximum Absolute Difference

Declare int a, i, n, res, j; Read n; Read array a; for (i = 0 to i = n) implement by 1 { for (j = i to j = n) implement by 1 { check if (absolute of a[i] - a[j] > res) { res = abs(a[i] - a[j]); } } } print(res)

In this algorithm, first we declare an array of int a to store array elements and the variable i and j to run the loop, n for getting the no. of elements in the array.

Res is for storing the result. We Read variable n and array a. We run for loop i=0 to i=n and inside that another loop for which will run from j=i to j=n.

We will check abs(a[i]-a[j]>res), it means that when we are subtracting a-a then a-a ,a-a ,a-a....a-a[n].So we can see that when i=0 j will run from 0 to n then i=1 j will run from 1 to n. Likewise it will happen for all the numbers. So we are checking the subtraction of two elements and it's absolute value whether is greater than res or not. If it is greater it will get store in res otherwise it will pass.

## Flowchart to find the Maximum Absolute Difference: ## Implementation of Program:

#include<stdio.h> #include<stdlib.h> int maxv(int a[], int n); int main() { int a, i, n, res; printf("Enter the no. of inputs:"); scanf("%d", & n); printf("Enter the elements of array:"); for (i = 0; i < n; i++) { scanf("%d", & a[i]); } for (i = 0; i < n; i++) { for (j = i; j < n; j++) { if (abs(a[i] - a[j]) > res) { res = abs(a[i] - a[j]); } } } printf(" Maximum absolute difference is %d", res); }

### Output: ## Struggling to Understand Algorithm and Flowchart? Try our Notes

#### Comments

##### Recommended Deals End in

Top Java Interview Questions PDF Guide with 30+ Pages ##### Play 2048 Game Online and Relax. 