The Wayback Machine - https://web.archive.org/web/20240706112636/https://www.geeksforgeeks.org/interquartile-range-iqr/
Open In App

Interquartile Range (IQR)

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The quartiles of a ranked set of data values are three points that divide the data into exactly four equal parts, each part comprising quarter data. 

  1. Q1 is defined as the middle number between the smallest number and the median of the data set.
  2. Q2 is the median of the data.
  3. Q3 is the middle value between the median and the highest value of the data set.
The interquartile range IQR tells us the range 
where the bulk of the values lie. The interquartile
range is calculated by subtracting the first quartile
from the third quartile.
IQR = Q3 - Q1

Uses 
1. Unlike range, IQR tells where the majority of data lies and is thus preferred over range. 
2. IQR can be used to identify outliers in a data set. 
3. Gives the central tendency of the data. 

Examples: 

Input : 1, 19, 7, 6, 5, 9, 12, 27, 18, 2, 15
Output : 13
The data set after being sorted is
1, 2, 5, 6, 7, 9, 12, 15, 18, 19, 27
As mentioned above Q2 is the median of the data.
Hence Q2 = 9
Q1 is the median of lower half, taking Q2 as pivot.
So Q1 = 5
Q3 is the median of upper half talking Q2 as pivot.
So Q3 = 18
Therefore IQR for given data=Q3-Q1=18-5=13

Input : 1, 3, 4, 5, 5, 6, 7, 11
Output : 3

Below is the implementation of the above approach:

C++




#include <iostream>
#include <algorithm>
 
using namespace std;
 
// Function to give
// index of the median
int median(int a[], int l, int r)
{
    int n = r - l + 1;
    n = (n + 1) / 2 - 1;
    return n + l;
}
 
// Function to
// calculate IQR
int IQR(int a[], int n)
{
    sort(a, a + n);
 
    // Index of median
    // of entire data
    int mid_index = median(a, 0, n - 1);
 
    // Median of first half
    int Q1;
    if (n % 2 == 0)
        Q1 = a[median(a, 0, mid_index)];
    else
        Q1 = a[median(a, 0, mid_index - 1)];
 
    // Median of second half
    int Q3;
    if (n % 2 == 0)
        Q3 = a[median(a, mid_index + 1, n - 1)];
    else
        Q3 = a[median(a, mid_index + 1, n)];
 
    // IQR calculation
    return (Q3 - Q1);
}
 
// Driver Code
int main()
{
    int a[] = {1, 19, 7, 6, 5, 9, 12, 27, 18, 2, 15};
    int n = sizeof(a) / sizeof(a[0]);
    cout << IQR(a, n) << endl;
    return 0;
}


Java




// Java program to find
// IQR of a data set
import java.io.*;
import java.util.*;
 
class GFG
{
     
    // Function to give
    // index of the median
    static int median(int a[],
                    int l, int r)
    {
        int n = r - l + 1;
        n = (n + 1) / 2 - 1;
        return n + l;
    }
 
    // Function to
    // calculate IQR
    static int IQR(int [] a, int n)
    {
        Arrays.sort(a);
 
        // Index of median
        // of entire data
        int mid_index = median(a, 0, n - 1);
 
        // Median of first half
        int Q1;
        if (n % 2 == 0)
            Q1 = a[median(a, 0, mid_index)];
        else
            Q1 = a[median(a, 0, mid_index - 1)];
 
        // Median of second half
        int Q3;
        if (n % 2 == 0)
            Q3 = a[median(a, mid_index + 1, n - 1)];
        else
            Q3 = a[median(a, mid_index + 1, n)];
 
        // IQR calculation
        return (Q3 - Q1);
    }
 
    // Driver Code
    public static void main (String[] args)
    {
        int []a = {1, 19, 7, 6, 5, 9,
                12, 27, 18, 2, 15};
        int n = a.length;
        System.out.println(IQR(a, n));
    }
}
 
// This code is contributed
// by anuj_67.


Python3




# Python3 program to find IQR of
# a data set
 
# Function to give index of the median
def median(a, l, r):
    n = r - l + 1
    n = (n + 1) // 2 - 1
    return n + l
 
# Function to calculate IQR
def IQR(a, n):
 
    a.sort()
 
    # Index of median of entire data
    mid_index = median(a, 0, n - 1)
 
    # Median of first half
    Q1 = a[median(a, 0, mid_index)]
 
    # Median of second half
    if n % 2 == 0:
        Q3 = a[median(a, mid_index + 1, n - 1)]
    else:
        Q3 = a[median(a, mid_index + 1, n - 1)]
 
    # IQR calculation
    return (Q3 - Q1)
 
# Driver Function
if __name__=='__main__':
    a = [1, 19, 7, 6, 5, 9, 12, 27, 18, 2, 15]
    n = len(a)
    print(IQR(a, n))
 
# This code is contributed by
# Sanjit_Prasad


C#




// C# program to find
// IQR of a data set
using System;
 
class GFG
{
     
    // Function to give
    // index of the median
    static int median(int []a,
                      int l, int r)
    {
        int n = r - l + 1;
        n = (n + 1) / 2 - 1;
        return n + l;
    }
 
    // Function to
    // calculate IQR
    static int IQR(int [] a, int n)
    {
        Array.Sort(a);
 
        // Index of median
        // of entire data
        int mid_index = median(a, 0, n - 1);
 
        // Median of first half
        int Q1 = a[median(a, 0, mid_index)];
 
        // Median of second half
        int Q3;
        if (n % 2 == 0)
            Q3 = a[median(a, mid_index + 1, n - 1)];
        else
            Q3 = a[median(a, mid_index + 1, n)];
 
        // IQR calculation
        return (Q3 - Q1);
    }
 
    // Driver Code
    public static void Main ()
    {
        int []a = {1, 19, 7, 6, 5, 9,
                12, 27, 18, 2, 15};
        int n = a.Length;
        Console.WriteLine(IQR(a, n));
    }
}
 
// This code is contributed
// by anuj_67.


Javascript




// JavaScript program to find
// IQR of a data set
 
// Function to give
// index of the median
function median(a, l, r) {
    let n = r - l + 1;
    n = Math.floor((n + 1) / 2) - 1;
    return n + l;
}
 
// Function to
// calculate IQR
function IQR(a, n) {
    a.sort((x, y) => x - y);
 
    // Index of median
    // of entire data
    let mid_index = median(a, 0, n - 1);
 
    // Median of first half
    let Q1 = a[median(a, 0, mid_index)];
 
    // Median of second half
    let Q3;
    if (n % 2 === 0)
        Q3 = a[median(a, mid_index + 1, n - 1)];
    else
        Q3 = a[median(a, mid_index + 1, n)];
 
    // IQR calculation
    return Q3 - Q1;
}
 
// Driver Code
let a = [1, 19, 7, 6, 5, 9, 12, 27, 18, 2, 15];
let n = a.length;
console.log(IQR(a, n));


PHP




<?php
// PHP program to find
// IQR of a data set
 
// Function to give
// index of the median
function median($a, $l, $r) {
    $n = $r - $l + 1;
    $n = floor(($n + 1) / 2) - 1;
    return $n + $l;
}
 
// Function to
// calculate IQR
function IQR($a, $n) {
    sort($a);
 
    // Index of median
    // of entire data
    $mid_index = median($a, 0, $n - 1);
 
    // Median of first half
    $Q1 = $a[median($a, 0, $mid_index)];
 
    // Median of second half
    $Q3 = ($n % 2 === 0) ? $a[median($a, $mid_index + 1, $n - 1)] : $a[median($a, $mid_index + 1, $n)];
 
    // IQR calculation
    return $Q3 - $Q1;
}
 
// Driver Code
$a = [1, 19, 7, 6, 5, 9, 12, 27, 18, 2, 15];
$n = count($a);
echo IQR($a, $n);
?>


Output

13

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



Previous Article
Next Article

Similar Reads

Range and Coefficient of range of Array
Given an array arr of integer elements, the task is to find the range and coefficient of range of the given array where: Range: Difference between the maximum value and the minimum value in the distribution. Coefficient of Range: (Max – Min) / (Max + Min). Examples: Input: arr[] = {15, 16, 10, 9, 6, 7, 17} Output: Range : 11 Coefficient of Range :
7 min read
Count of pairs in range [P, Q] with numbers as multiple of R and their product lie in range [P*Q/4, P*Q]
Given 3 positive integers P, Q, and R, the task is to find the number of pairs such that both the elements are in the range [P, Q] and the numbers should be multiple of R, and the product of numbers should lie in the range [P × Q / 4, P × Q]. If no such pair exists, print -1. Examples: Input: P = 14, Q = 30, R = 5Output:15 20 15 25Explanation: Mult
9 min read
Number of ways in which the substring in range [L, R] can be formed using characters out of the range
Given a string S and a range [L, R]. The task is to find the number of ways in which the sub-string in the range S[L, R] can be constructed using the characters that exist in the string but do not lie in the range S[L, R]. Examples: Input: s = "cabcaab", l = 1, r = 3 Output: 2 The substring is "abc" s[4] + s[6] + s[0] = 'a' + 'b' + 'c' = "abc" s[5]
8 min read
Maximum range length such that A[i] is maximum in given range for all i from [1, N]
Given an array arr[] consisting of N distinct integers. For each i (0 ? i &lt; n), find a range [l, r] such that A[i] = max(A[l], A[l+1], ..., A[r]) and l ? i ? r and r-l is maximized. Examples: Input: arr[] = {1, 3, 2}Output: {0 0}, {0 2}, {2 2}Explanation: For i=0, 1 is maximum in range [0, 0] only. For i=1, 3 is maximum in range [0, 2] and for i
8 min read
Range Queries to count elements lying in a given Range : MO's Algorithm
Given an array arr[] of N elements and two integers A to B, the task is to answer Q queries each having two integers L and R. For each query, find the number of elements in the subarray arr[L…R] which lies within the range A to B (inclusive). Examples: Input: arr[] = {7, 3, 9, 13, 5, 4}, A = 4, B = 7query = {1, 5}Output: 2Explanation:Only 5 and 4 l
15+ min read
Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
Given three integers a, b and c. You need to select one integer from the range [a, b] and one integer from the range [b, c] and add them. The task to calculate the number of ways to obtain the sum for all the numbers in the range [1, b+c]. Examples: Input: a = 1, b = 2, c = 2 Output: 0, 0, 1, 1 Explanation: The numbers to be obtained are [1, b+c] =
10 min read
Generate a pair of integers from a range [L, R] whose LCM also lies within the range
Given two integers L and R, the task is to find a pair of integers from the range [L, R] having LCM within the range [L, R] as well. If no such pair can be obtained, then print -1. If multiple pairs exist, print any one of them. Examples: Input: L = 13, R = 69Output: X =13, Y = 26Explanation: LCM(x, y) = 26 which satisfies the conditions L ? x &lt;
4 min read
Count pairs from a given range whose sum is a Prime Number in that range
Given two integers L and R, the task is to count the number of pairs from the range [L, R] whose sum is a prime number in the range [L, R]. Examples: Input: L = 1, R = 5Output: 4Explanation: Pairs whose sum is a prime number and in the range [L, R] are { (1, 1), (1, 2), (1, 4), (2, 3) }. Therefore, the required output is 4. Input: L = 1, R = 100Out
14 min read
Count numbers from a given range that can be visited moving any number of steps from the range [L, R]
Given two integers X, Y and a range [L, R], the task is to count the number of integers from the range X and Y (inclusive) that can be visited in any number of steps, starting from X. In each step, it is possible to increase by L to R. Examples: Input: X = 1, Y = 10, L = 4, R = 6Output: 6Explanation: A total of six points can be visited between [1,
6 min read
Find the range [L, R] such that sum of numbers in this range equals to N
Given an integer N (N ≠ 0), the task is to find a range [L, R] (−10⁻¹⁸ &lt; L &lt; R &lt; 10¹⁸) such that sum of all integers in this range is equal to N. L + (L+1) + … + (R−1) + R = N Examples: Input : N = 3Output: -2 3Explanation: For L = -2 and R = -3 the sum becomes -2 + (-1) + 0 + 1 + 2 + 3 = 3 Input : N = -6Output: -6 5Explanation: The sum fo
5 min read
Practice Tags :
three90RightbarBannerImg