Problem statement:- Program to Maximum difference between two elements in an array.
Example:-
Input : Given
size=5
arr[]=[9,1,8,11,13]
Output: Maximum difference between two Element is 12 //(13-1=12)
Data requirement:-
Input Data:- size, arr
Output Data:-Max_diff
Additional Data:- i,j
Program in C
Here is the source code of the C Program to Maximum difference between two elements in an array.
Code:
#include<stdio.h>
#include<limits.h>
#include<math.h>
main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size];
int i,j;
printf("Enter the Element of the array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
int Max_diff=INT_MIN;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(abs(arr[j]-arr[i])>Max_diff)
{
Max_diff=abs(arr[j]-arr[i]);
}
}}
printf("Maximum difference between two Element is %d",Max_diff);
}
Enter the size of the array:5
Enter the Element of the array:
9 1 8 11 13
Maximum difference between two Element is 12
Program in C++
Here is the source code of the C++ Program to Maximum difference between two elements in an array.
Code:
#include<iostream>
#include<cmath>
#include<climits>
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size];
int i,j;
cout<<"Enter the Element of the array:\n";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
int Max_diff=INT_MIN;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(abs(arr[j]-arr[i])>Max_diff)
{
Max_diff=abs(arr[j]-arr[i]);
}
}}
cout<<"Maximum difference between two Element is "<<Max_diff;
}
Enter the size of the array:4
Enter the Element of the array:
10 5 3 4
Maximum difference between two Element is 7
Program in Java
Here is the source code of the Java Program to Maximum difference between two elements in an array.
Code:
import java.util.Scanner;
public class MaximumDifference {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size,i,j;
size=sc.nextInt();
int arr[ ]=new int[size];
System.out.println("Enter the Element of the array:");
for(i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
int Max_diff=Integer.MIN_VALUE;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(Math.abs(arr[j]-arr[i])>Max_diff)
{
Max_diff=Math.abs(arr[j]-arr[i]);
}
}}
System.out.print("Maximum difference between two Element is "+Max_diff);
sc.close();
}
}
Enter the size of the array:
8
Enter the Element of the array:
10 5 20 13 14 6 2 1
Maximum difference between two Element is 19
Program in Python
Here is the source code of the Python Program to Maximum difference between two elements in an array.
Code:
import sys
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
Max_diff=-sys.maxsize-1
for i in range(0,size-1):
for j in range(i+1, size):
if abs(arr[j]-arr[i])>Max_diff:
Max_diff = abs(arr[j] - arr[i])
print("Maximum difference between two Element is ",Max_diff)
Enter the size of the array: 4
Enter the Element of the array:
33
3
34
4
Maximum difference between two Element is 31
0 Comments
Please do not Enter any spam link in the comment box