Friday, December 27, 2024
Google search engine
HomeLanguagesJavaMaximum difference between two elements in an Array

Maximum difference between two elements in an Array

Given an array arr[] of N integers, the task is to find the maximum difference between any two elements of the array.
Examples: 

Input: arr[] = {2, 1, 5, 3} 
Output:
|5 – 1| = 4

Input: arr[] = {-10, 4, -9, -5} 
Output: 14 

Naive Approach:- As the maximum difference will be in between smallest and the largest array so we will simply sort the array and get the maximum difference.

Below is the implementation for the same:

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the maximum
// absolute difference between
// any two elements of the array
int maxAbsDiff(int arr[], int n)
{
      //Sorting the array
      sort(arr,arr+n);
   
      //returning the difference between last and first element
      return arr[n-1]-arr[0];
}
 
// Driver code
int main()
{
    int arr[] = { 2, 1, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << maxAbsDiff(arr, n);
 
    return 0;
}


Java




// Java implementation of the approach
import java.util.*;
class GFG
{
 
  // Function to return the maximum
  // absolute difference between
  // any two elements of the array
  static int maxAbsDiff(int arr[], int n)
  {
 
    // Sorting the array
    Arrays.sort(arr);
 
    // returning the difference between last and first element
    return arr[n-1] - arr[0];
  }
 
  // Driver code
  public static void main(String args[]) {
    int arr[] = {2,1,5,3};
    int n = arr.length;
    System.out.println(maxAbsDiff(arr, n));
  }
}


Python3




# python implementation of the approach
 
# Function to return the maximum
# absolute difference between
# any two elements of the array
def maxAbsDiff(arr, n):
   
      # sorting the array
    arr.sort()
     
    # returning the difference between last and first element
    return arr[n - 1] - arr[0]
 
#driver code
arr = [2, 1, 5, 3]
n = len(arr)
print(maxAbsDiff(arr, n))


C#




// C# implementation of the approach
using System;
 
class Program
{
// Function to return the maximum
// absolute difference between
// any two elements of the array
static int maxAbsDiff(int[] arr, int n)
{
//Sorting the array
Array.Sort(arr);
      //returning the difference between last and first element
    return arr[n - 1] - arr[0];
}
 
// Driver code
static void Main(string[] args)
{
    int[] arr = { 2, 1, 5, 3 };
    int n = arr.Length;
    Console.WriteLine(maxAbsDiff(arr, n));
}
}
 
//This code is contributed by shivamsharma215


Javascript




// Javascript implementation of the approach
 
// Function to return the maximum
// absolute difference between
// any two elements of the array
function maxAbsDiff(arr, n)
{
    //Sorting the array
    arr.sort();
   
    //returning the difference between last and first element
    return arr[n-1]-arr[0];
}
 
// Driver code
let arr = [ 2, 1, 5, 3 ];
let n = arr.length;
console.log(maxAbsDiff(arr, n));
 
// The code is contributed by Arushi Jindal.


Output

4

Time Complexity:- O(nlogn)
Auxiliary Space:- O(1)

Approach: The maximum absolute difference in the array will always be the absolute difference between the minimum and the maximum element from the array.

Below is the implementation of the above approach:

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the maximum
// absolute difference between
// any two elements of the array
int maxAbsDiff(int arr[], int n)
{
 
    // To store the minimum and the maximum
    // elements from the array
    int minEle = arr[0];
    int maxEle = arr[0];
    for (int i = 1; i < n; i++) {
        minEle = min(minEle, arr[i]);
        maxEle = max(maxEle, arr[i]);
    }
 
    return (maxEle - minEle);
}
 
// Driver code
int main()
{
    int arr[] = { 2, 1, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << maxAbsDiff(arr, n);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG {
 
    // Function to return the maximum
    // absolute difference between
    // any two elements of the array
    static int maxAbsDiff(int arr[], int n)
    {
 
        // To store the minimum and the maximum
        // elements from the array
        int minEle = arr[0];
        int maxEle = arr[0];
        for (int i = 1; i < n; i++) {
            minEle = Math.min(minEle, arr[i]);
            maxEle = Math.max(maxEle, arr[i]);
        }
 
        return (maxEle - minEle);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 2, 1, 5, 3 };
        int n = arr.length;
        System.out.print(maxAbsDiff(arr, n));
    }
}


Python3




# Python3 implementation of the approach
 
# Function to return the maximum
# absolute difference between
# any two elements of the array
def maxAbsDiff(arr, n):
 
    # To store the minimum and the maximum
    # elements from the array
    minEle = arr[0]
    maxEle = arr[0]
    for i in range(1, n):
        minEle = min(minEle, arr[i])
        maxEle = max(maxEle, arr[i])
 
    return (maxEle - minEle)
 
# Driver code
arr = [2, 1, 5, 3]
n = len(arr)
print(maxAbsDiff(arr, n))
 
# This code is contributed
# by mohit kumar


C#




// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the maximum
    // absolute difference between
    // any two elements of the array
    static int maxAbsDiff(int []arr, int n)
    {
 
        // To store the minimum and the maximum
        // elements from the array
        int minEle = arr[0];
        int maxEle = arr[0];
        for (int i = 1; i < n; i++)
        {
            minEle = Math.Min(minEle, arr[i]);
            maxEle = Math.Max(maxEle, arr[i]);
        }
 
        return (maxEle - minEle);
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = { 2, 1, 5, 3 };
        int n = arr.Length;
         
        Console.WriteLine(maxAbsDiff(arr, n));
    }
}
 
// This code is contributed by Ryuga


PHP




<?php
// PHP implementation of the approach
 
// Function to return the maximum
// absolute difference between
// any two elements of the array
function maxAbsDiff($arr, $n)
{
 
    // To store the minimum and the maximum
    // elements from the array
    $minEle = $arr[0];
    $maxEle = $arr[0];
    for ($i = 1; $i < $n; $i++)
    {
        $minEle = min($minEle, $arr[$i]);
        $maxEle = max($maxEle, $arr[$i]);
    }
 
    return ($maxEle - $minEle);
}
 
// Driver code
$arr = array(2, 1, 5, 3);
$n = sizeof($arr);
echo maxAbsDiff($arr, $n);
 
// This code is contributed
// by Akanksha Rai


Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function to return the maximum
// absolute difference between
// any two elements of the array
function maxAbsDiff(arr, n)
{
 
    // To store the minimum and the maximum
    // elements from the array
    let minEle = arr[0];
    let maxEle = arr[0];
    for (let i = 1; i < n; i++) {
        minEle = Math.min(minEle, arr[i]);
        maxEle = Math.max(maxEle, arr[i]);
    }
 
    return (maxEle - minEle);
}
 
// Driver code
    let arr = [ 2, 1, 5, 3 ];
    let n = arr.length;
    document.write(maxAbsDiff(arr, n));
 
</script>


Output

4

Time Complexity : O(n)
Auxiliary Space : O(1)

Another Approach ( Using STL): The maximum absolute difference in the array will always be the absolute difference between the minimum and the maximum element from the array. Below is the implementation of the above approach:

Below is the implementation of the above approach:

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the maximum
// absolute difference between
// any two elements of the array
int maxAbsDiff(int arr[], int n)
{
 
    // To find the minimum and the maximum element
    // using stl
    int maxele = *max_element(arr, arr + n);
    int minele = *min_element(arr, arr + n);
    // make variable to store answer
    int ans = abs(maxele - minele);
    return ans;
}
// Driver code
int main()
{
    int arr[] = { -10, 4, -9, -5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << maxAbsDiff(arr, n);
 
    return 0;
}


Java




//Java program for the above approach
import java.util.Arrays;
class GFG {
 
  // Function to return the maximum
  // absolute difference between
  // any two elements of the array
  public static int maxAbsDiff(int arr[], int n)
  {
 
    // To find the minimum and the maximum element
    // using stl
    int maxele = Arrays.stream(arr).max().getAsInt();
    int minele = Arrays.stream(arr).min().getAsInt();
 
    // make variable to store answer
    int ans = Math.abs(maxele - minele);
    return ans;
  }
 
  // Driver code
  public static void main (String[] args)
  {
    int arr[] = { -10, 4, -9, -5 };
    int n = arr.length;
    System.out.print(maxAbsDiff(arr, n));
  }
}
 
// This code is contributed by Shubham Singh


Python3




# Python program for the above approach
 
# Function to return the maximum
# absolute difference between
# any two elements of the array
def maxAbsDiff(arr, n):
     
    # To find the minimum and the maximum element
    maxele = max(arr)
    minele = min(arr)
     
    # make variable to store answer
    ans = abs(maxele - minele)
    return ans
 
# Driver code
arr = [ -10, 4, -9, -5 ]
n = len(arr)
print(maxAbsDiff(arr, n))
 
# This code is contributed by Shubham Singh


C#




// C# program for the above approach
using System;
using System.Linq;
 
public class GFG{
 
  // Function to return the maximum
  // absolute difference between
  // any two elements of the array
  public static int maxAbsDiff(int[] arr, int n)
  {
 
    // To find the minimum and the maximum element
    int maxele = arr.Max();
    int minele = arr.Min();
 
    // make variable to store answer
    int ans = Math.Abs(maxele - minele);
    return ans;
  }
 
  // Driver code
  static public void Main ()
  {
    int[] arr = { -10, 4, -9, -5 };
    int n = arr.Length;
    Console.Write(maxAbsDiff(arr, n));
  }
}
 
// This code is contributed by Shubham Singh


Javascript




<script>
// Javascript program for the above approach
 
// Function to return the maximum
// absolute difference between
// any two elements of the array
function maxAbsDiff(arr, n)
{
 
    // To find the minimum and the maximum element
    var maxele = Math.max(...arr);
    var minele = Math.min(...arr);
 
    // make variable to store answer
    var ans = Math.abs(maxele - minele);
    return ans;
}
 
// Driver code
var arr = [ -10, 4, -9, -5 ];
var n = arr.Length;
document.write(maxAbsDiff(arr, n));
 
// This code is contributed by Shubham Singh
</script>


Output

14

Time Complexity : O(n)
Auxiliary Space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments