Open In App

Find K elements whose absolute difference with median of array is maximum

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] and an integer K, the task is to find the K elements of the array whose absolute difference with median of array is maximum. 
Note: If two elements have equal difference then the maximum element is taken into consideration.

Examples: 

Input : arr[] = {1, 2, 3, 4, 5}, k = 3 
Output : {5, 1, 4} 
Explanation : 
Median m = 3, 
Difference of each array elements from median, 
1 ==> diff(1-3) = 2 
2 ==> diff(2-3) = 1 
3 ==> diff(3-3) = 0 
4 ==> diff(4-3) = 1 
5 ==> diff(5-3) = 2 
First K elements are 5, 1, 4 in this array.

Input: arr[] = {1, 2, 3}, K = 2 
Output: {3, 1} 

Approach:  

  • Sort the array and find the median of the array
  • Create a difference array to store the difference of each element with the median of the sorted array.
  • Highest difference elements will be the corner elements of the array. Therefore, initialize the two pointers as both the corner elements of the array that is 0 and N – 1.
  • Finally include the elements of the array one by one with the maximum difference with the median.

Below is the implementation of the above approach: 

C++




// C++ implementation to find first K
// elements whose difference with the 
// median of array is maximum
  
#include <bits/stdc++.h>
using namespace std;
  
// Function for calculating median 
double findMedian(int a[], int n) 
{
    // check for even case 
    if (n % 2 != 0) 
       return (double)a[n/2]; 
        
    return (double)(a[(n-1)/2] + a[n/2])/2.0; 
  
// Function to find the K maximum absolute
// difference with the median of the array
void kStrongest(int arr[], int n, int k)
{
    // Sort the array.
    sort(arr, arr + n);
  
    // Store median
    double median = findMedian(arr, n);
    int diff[n];
  
    // Find and store difference
    for (int i = 0; i < n; i++) {
        diff[i] = abs(median - arr[i]);
    }
  
    int i = 0, j = n - 1;
    while (k > 0) {
          
        // If diff[i] is greater print it
        // Else print diff[j]
        if (diff[i] > diff[j]) {
            cout << arr[i] << " ";
            i++;
        }
        else {
            cout << arr[j] << " ";
            j--;
        }
        k--;
    }
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int k = 3;
    int n = sizeof(arr) / sizeof(arr[0]);
  
    kStrongest(arr, n, k);
    return 0;
}


Java




// Java implementation to find first K
// elements whose difference with the 
// median of array is maximum
import java.util.*;
class GFG{
   
// Function for calculating median 
static double findMedian(int a[], int n) 
{
    // check for even case 
    if (n % 2 != 0
       return (double)a[n / 2]; 
         
    return (double)(a[(n - 1) / 2] + 
                    a[n / 2]) / 2.0
   
// Function to find the K maximum absolute
// difference with the median of the array
static void kStrongest(int arr[], int n, int k)
{
    // Sort the array.
    Arrays.sort(arr);
   
    // Store median
    double median = findMedian(arr, n);
    int []diff = new int[n];
   
    // Find and store difference
    for (int i = 0; i < n; i++) 
    {
        diff[i] = (int)Math.abs(median - arr[i]);
    }
   
    int i = 0, j = n - 1;
    while (k > 0
    {
           
        // If diff[i] is greater print it
        // Else print diff[j]
        if (diff[i] > diff[j])
        {
            System.out.print(arr[i] + " ");
            i++;
        }
        else 
        {
            System.out.print(arr[j] + " ");
            j--;
        }
        k--;
    }
}
   
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int k = 3;
    int n = arr.length;
   
    kStrongest(arr, n, k);
}
}
// This code is contributed by sapnasingh4991


Python3




# Python3 program to find first K
# elements whose difference with the
# median of array is maximum
  
# Function for calculating median
def findMedian(a, n):
      
    # Check for even case
    if (n % 2 != 0):
        return a[int(n / 2)]
          
    return (a[int((n - 1) / 2)] + 
            a[int(n / 2)]) / 2.0
  
# Function to find the K maximum 
# absolute difference with the 
# median of the array
def kStrongest(arr, n, k):
      
    # Sort the array
    arr.sort()
      
    # Store median
    median = findMedian(arr, n)
    diff = [0] * (n)
      
    # Find and store difference
    for i in range(n):
        diff[i] = abs(median - arr[i])
          
    i = 0
    j = n - 1
      
    while (k > 0):
          
        # If diff[i] is greater print 
        # it. Else print diff[j]
        if (diff[i] > diff[j]):
            print(arr[i], end = " ")
            i += 1
        else:
            print(arr[j], end = " ")
            j -= 1
          
        k -= 1
      
# Driver code
arr = [ 1, 2, 3, 4, 5 ]
k = 3
n = len(arr)
  
kStrongest(arr, n, k)
  
# This code is contributed by sanjoy_62


C#




// C# implementation to find first K
// elements whose difference with the 
// median of array is maximum
using System;
  
class GFG{
  
// Function for calculating median 
static double findMedian(int []a, int n) 
{
    // Check for even case 
    if (n % 2 != 0) 
        return (double)a[n / 2]; 
          
    return (double)(a[(n - 1) / 2] + 
                    a[n / 2]) / 2.0; 
  
// Function to find the K maximum absolute
// difference with the median of the array
static void kStrongest(int []arr, int n,
                                  int k)
{
      
    // Sort the array.
    Array.Sort(arr);
      
    int i = 0;
      
    // Store median
    double median = findMedian(arr, n);
    int []diff = new int[n];
  
    // Find and store difference
    for(i = 0; i < n; i++) 
    {
       diff[i] = (int)Math.Abs(median - arr[i]);
    }
  
    int j = n - 1;
    i = 0;
    while (k > 0) 
    {
          
        // If diff[i] is greater print it
        // Else print diff[j]
        if (diff[i] > diff[j])
        {
            Console.Write(arr[i] + " ");
            i++;
        }
        else
        {
            Console.Write(arr[j] + " ");
            j--;
        }
        k--;
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4, 5 };
    int k = 3;
    int n = arr.Length;
  
    kStrongest(arr, n, k);
}
}
  
// This code is contributed by Rohit_ranjan


Javascript




<script>
  
// JavaScript implementation to find first K
// elements whose difference with the 
// median of array is maximum
  
// Function for calculating median 
function findMedian(a, n) 
{
      
    // Check for even case 
    if (n % 2 != 0) 
       return a[(Math.floor(n / 2))]; 
         
    return (a[(Math.floor((n - 1) / 2))] + 
            a[(Math.floor(n / 2))]) / 2.0; 
   
// Function to find the K maximum absolute
// difference with the median of the array
function kStrongest(arr, n, k)
{
      
    // Sort the array.
    arr.sort();
   
    // Store median
    let median = findMedian(arr, n);
    let diff = Array.from({length: n}, (_, i) => 0);
   
    // Find and store difference
    for(let i = 0; i < n; i++) 
    {
        diff[i] = Math.abs(median - arr[i]);
    }
   
    let i = 0, j = n - 1;
    while (k > 0) 
    {
           
        // If diff[i] is greater print it
        // Else print diff[j]
        if (diff[i] > diff[j])
        {
            document.write(arr[i] + " ");
            i++;
        }
        else 
        {
           document.write(arr[j] + " ");
            j--;
        }
        k--;
    }
}
    
// Driver Code
let arr = [ 1, 2, 3, 4, 5 ];
let k = 3;
let n = arr.length;
  
kStrongest(arr, n, k);
  
// This code is contributed by susmitakundugoaldanga    
  
</script>


Output

5 1 4 

Time Complexity: O(nlogn), where nlogn is the time complexity required to sort the given array
Auxiliary Space: O(n), extra space used to create a diff array

Another Approach:- In this approach we will se how we can reduce the space complexity to O(N)->O(1)

  • There is no need to take difference array as in the above approach
  • As we came to know that the difference will be maximum with corner values so no need to store the difference just take 2 pointer on start and end.
  • Print the one which have greater difference and move that pointer

Implementation:-

C++




// C++ implementation to find first K
// elements whose difference with the 
// median of array is maximum
  
#include <bits/stdc++.h>
using namespace std;
  
// Function for calculating median 
double findMedian(int a[], int n) 
{
    // check for even case 
    if (n % 2 != 0) 
       return (double)a[n/2]; 
        
    return (double)(a[(n-1)/2] + a[n/2])/2.0; 
  
// Function to find the K maximum absolute
// difference with the median of the array
void kStrongest(int arr[], int n, int k)
{
    // Sort the array.
    sort(arr, arr + n);
  
    // Store median
    double median = findMedian(arr, n);
  
    int i = 0, j = n - 1;
    while (k) {
        
          //if difference of element at i with K is greater than element at j
        if (abs(median-arr[i]) > abs(median-arr[j])) {
            cout << arr[i] << " ";
            i++;
        }
        else {
            cout << arr[j] << " ";
            j--;
        }
        k--;
    }
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int k = 3;
    int n = sizeof(arr) / sizeof(arr[0]);
  
    kStrongest(arr, n, k);
    return 0;
}


Java




// Java implementation to find first K
// elements whose difference with the
// median of array is maximum
  
import java.util.*;
  
public class GFG {
  
    // Function for calculating median
    public static double findMedian(int[] a, int n)
    {
        // check for even case
        if (n % 2 != 0) {
            return (double)a[n / 2];
        }
  
        return (double)(a[(n - 1) / 2] + a[n / 2]) / 2.0;
    }
  
    // Function to find the K maximum absolute
    // difference with the median of the array
    public static void kStrongest(int[] arr, int n, int k)
    {
        // Sort the array.
        Arrays.sort(arr);
  
        // Store median
        double median = findMedian(arr, n);
  
        int i = 0;
        int j = n - 1;
        while (k != 0) {
  
            // if difference of element at i with K is
            // greater than element at j
            if (Math.abs(median - arr[i])
                > Math.abs(median - arr[j])) {
                System.out.print(arr[i]);
                System.out.print(" ");
                i++;
            }
            else {
                System.out.print(arr[j]);
                System.out.print(" ");
                j--;
            }
            k--;
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int k = 3;
        int n = arr.length;
  
        kStrongest(arr, n, k);
    }
}
  
// this code is contributed by bhardwajji


Python3




# C++ implementation to find first K
# elements whose difference with the
# median of array is maximum
  
# Function for calculating median
def findMedian(a, n):
    
      # check for even case
    if n % 2 != 0:
        return a[n//2]
    return (a[(n-1)//2] + a[n//2])/2
  
# Function to find the K maximum absolute
# difference with the median of the array
def kStrongest(arr, n, k):
    
      # sort array
    arr.sort()
      
    # store median
    median = findMedian(arr, n)
    i = 0
    j = n - 1
    while k:
        
          # if difference of element at i with K is greater than element at j
        if abs(median-arr[i]) > abs(median-arr[j]):
            print(arr[i], end=" ")
            i += 1
        else:
            print(arr[j], end=" ")
            j -= 1
        k -= 1
  
#driver code
arr = [1, 2, 3, 4, 5]
k = 3
n = len(arr)
kStrongest(arr, n, k)


C#




// C# implementation to find first K
// elements whose difference with the 
// median of array is maximum
  
using System;
  
public class GFG
{
    // Function for calculating median 
    static double FindMedian(int[] a, int n) 
    {
        // check for even case 
        if (n % 2 != 0) 
            return (double)a[n / 2]; 
        return (double)(a[(n - 1) / 2] + a[n / 2]) / 2.0; 
    
  
    // Function to find the K maximum absolute
    // difference with the median of the array
    static void KStrongest(int[] arr, int n, int k)
    {
        // Sort the array.
        Array.Sort(arr);
  
        // Store median
        double median = FindMedian(arr, n);
  
        int i = 0, j = n - 1;
        while (k > 0) {
            // if difference of element at i with K is greater than element at j
            if (Math.Abs(median - arr[i]) > Math.Abs(median - arr[j])) {
                Console.Write(arr[i] + " ");
                i++;
            }
            else {
                Console.Write(arr[j] + " ");
                j--;
            }
            k--;
        }
    }
      
    // Driver Code
    static void Main(string[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int k = 3;
        int n = arr.Length;
  
        KStrongest(arr, n, k);
    }
}
  
//this code is contributed by bhardwajji


Javascript




// JavaScript implementation to find first K
// elements whose difference with the
// median of array is maximum
  
function findMedian(a, n) {
// check for even case
if (n % 2 !== 0) {
return a[(Math.floor(n / 2))];
}
return (a[(n - 1) / 2] + a[n / 2]) / 2.0;
}
  
function kStrongest(arr, n, k) {
// Sort the array.
arr.sort(function(a, b) {
return a - b;
});
// Store median
var median = findMedian(arr, n);
  
var i = 0;
var j = n - 1;
while (k !== 0) {
  
    // if difference of element at i with K is
    // greater than element at j
    if (Math.abs(median - arr[i])
        > Math.abs(median - arr[j])) {
        console.log(arr[i] + " ");
        i++;
    }
    else {
        console.log(arr[j] + " ");
        j--;
    }
    k--;
}
}
  
// Driver Code
var arr = [ 1, 2, 3, 4, 5 ];
var k = 3;
var n = arr.length;
  
kStrongest(arr, n, k);
  
// this code is contributed by shivamsharma215


Output

5 1 4 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads