Problem statement:- Program to Minimum difference between two elements in an array.
Example:-
Input : Given
size=5
arr[]=[9,1,8,11,13]
Output: Minimum difference between two Element is 1 //(9-8=1)
Data requirement:-
Input Data:- size, arr
Output Data:-Min_diff
Additional Data:- i,j
Program in C
Here is the source code of the C Program to Minimum 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,p;
printf("Enter the Element of the array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
int Min_diff=INT_MAX;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
if(abs(arr[j]-arr[i])<Min_diff)
{
Min_diff=abs(arr[j]-arr[i]);
}
}}
printf("Minimum difference between two Element is %d",Min_diff);
}
Enter the size of the array:5
Enter the Element of the array:
9 1 8 11 13
Minimum difference between two Element is 1
Program in C++
Here is the source code of the C++ Program to Minimum 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 Min_diff=INT_MAX;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
if(abs(arr[j]-arr[i])<Min_diff)
{
Min_diff=abs(arr[j]-arr[i]);
}
}}
cout<<"Minimum difference between two Element is "<<Min_diff;
}
Enter the size of the array:4
Enter the Element of the array:
10 5 3 4
Minimum difference between two Element is 1
Program in Java
Here is the source code of the Java Program to Minimum difference between two elements in an array.
Code:
import java.util.Scanner;
public class MinimumDifference {
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 Min_diff=Integer.MAX_VALUE;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
if(Math.abs(arr[j]-arr[i])<Min_diff)
{
Min_diff=Math.abs(arr[j]-arr[i]);
}
}}
System.out.println("Minimum difference between two Element is "+Min_diff);
sc.close();
}
}
Enter the size of the array:
6
Enter the Element of the array:
25 8 31 2 41 51
Minimum difference between two Element is 6
Program in Python
Here is the source code of the Python Program to Minimum 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)
Min_diff=sys.maxsize
for i in range(0,size-1):
for j in range(i+1, size):
if abs(arr[j]-arr[i])<Min_diff:
Min_diff = abs(arr[j] - arr[i])
print("Minimum difference between two Element is ",Min_diff)
Enter the size of the array: 3
Enter the Element of the array:
2
10
20
Minimum difference between two Element is 8
2 Comments
Not the best solution
ReplyDeleteYou can sort the array in O(nlogn) and then difference between the first two elements
comment down your solution here
DeletePlease do not Enter any spam link in the comment box