{"id":1211,"date":"2020-06-10T16:19:59","date_gmt":"2020-06-10T16:19:59","guid":{"rendered":"https:\/\/blog.prepbytes.com\/?p=1211"},"modified":"2022-06-17T06:33:01","modified_gmt":"2022-06-17T06:33:01","slug":"find-the-inversion-count","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/","title":{"rendered":"Find the Inversion Count"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100175465-Article_322.png\" alt=\"\" \/><\/p>\n<h3>Concepts Used<\/h3>\n<blockquote>\n<p>Sorting<\/p>\n<\/blockquote>\n<h3>Difficulty Level<\/h3>\n<blockquote>\n<p>Hard<\/p>\n<\/blockquote>\n<h3>Problem Statement (Simplified):<\/h3>\n<blockquote>\n<p>Find number of pairs such that <code>a[i]<\/code> &gt; <code>a[j]<\/code> and <code>i<\/code> &lt; <code>j<\/code>.<\/p>\n<\/blockquote>\n<h4>Test Case<\/h4>\n<pre><code>    Input:\n    1\n    5\n    10 50 20 40 30\n\n    Output:\n    4\n\n    Explanation:\n    Such required pairs are (50,20),(50,40),(50,30) and (40,30). So, 4 is our answer.<\/code><\/pre>\n<p><a href=\"https:\/\/mycode.prepbytes.com\/problems\/sorting\/INVRSCNT1\" title=\"Go to mycode.prepbytes.com\" target=\"_blank\" rel=\"noopener noreferrer\"><u><strong><\/strong><\/u><\/a><\/p>\n<h3>Solving Approach :<\/h3>\n<p><strong>Bruteforce Approach<\/strong>:<\/p>\n<blockquote>\n<p>We can count for each element, such a number of elements on it&#8217;s right which are smaller to the current element, this gives us the number of inversions for the current element, hence it can be calculated for all elements in the array. But this approach takes O(n<sup>2<\/sup>) time, which is not efficient for larger array sizes. Hence we can solve it more efficiently by following approach which takes <code>O(n\\times{log(n)})<\/code> time.<\/p>\n<\/blockquote>\n<p><strong>Efficient Approach<\/strong>:<\/p>\n<blockquote>\n<p>1) We can count the total number of inversions during sorting array using <strong><em>Merge Sort Algorithm<\/em><\/strong>.<br \/>\n2) In the merge sort algorithm when we merge two halves of the array, we copy elements from two halves into the auxiliary array. If the element from the left half is greater than the current right half element, that is also our one inversion as the left element has a lower index than the element in the right half.<br \/>\n3) Total number of inversions in above step would be <code>(mid-1)<\/code> because left and right subarrays are sorted, so all the remaining elements in left-subarray (a[i+1], a[i+2] \u2026 a[mid]) will be greater than a[j].<br \/>\nSo we count all these inversions and print them as the answer. <\/p>\n<\/blockquote>\n<h3>Example<\/h3>\n<blockquote>\n<p>As we saw in, the efficient approach we can count inversions during the merge sort itself. Let array <code>A<\/code> be,<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1646635046355-Find%20the%20Inversion%20Count1-01.png\" alt=\"\" \/><\/p>\n<blockquote>\n<p>Drawing recursion tree for given array, explains a lot about it,<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1646635105122-Find%20the%20Inversion%20Count2-02.png\" alt=\"\" \/><\/p>\n<blockquote>\n<p>Here, while merging, we can see <code>right<\/code> <code>child<\/code> have a inversion <code>(40,30)<\/code>, we add this to our inversion count and move to next step.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1646635269979-Find%20the%20Inversion%20Count3-03.png\" alt=\"\" \/><\/p>\n<p>Here again, while merging on the <code>left<\/code> <code>child<\/code>, we observe an inversion <code>(50,20)<\/code>, we add this to our inversion count and move further.<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1646635383443-Find%20the%20Inversion%20Count4-04.png\" alt=\"\" \/><\/p>\n<blockquote>\n<p>In this step, we see that right <code>child<\/code> have two elements which are less than last element of <code>left<\/code> <code>child<\/code>, hence both will be counted as inversions <code>(50,30),(50,40)<\/code>. We count both and move further.<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1646635161382-Find%20the%20Inversion%20Count5-05.png\" alt=\"\" \/><\/p>\n<blockquote>\n<p>Now, we have our array sorted, which means there exists no inversion anymore, so we print count and terminate the program.<\/p>\n<\/blockquote>\n<h3>Solutions<\/h3>\n<p>\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_1229 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_1229 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_1229 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_1229 .wpsm_nav-tabs > li.active > a, #tab_container_1229 .wpsm_nav-tabs > li.active > a:hover, #tab_container_1229 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_1229 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_1229 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_1229 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_1229 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_1229 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_1229 .wpsm_nav-tabs > li > a:hover , #tab_container_1229 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_1229 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_1229 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_1229 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_1229 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_1229 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_1229 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_1229 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1229 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_1229 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1229 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_1229 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_1229\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_1229\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1229_1\" aria-controls=\"tabs_desc_1229_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1229_2\" aria-controls=\"tabs_desc_1229_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1229_3\" aria-controls=\"tabs_desc_1229_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_1229\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_1229_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include <stdio.h>\r\n\r\nlong long merge(int arr[], int temp[], int left, int mid, int right){ \r\n        int i, j, k; \r\n        long long inv_count = 0; \r\n\r\n        i = left;\r\n        j = mid;\r\n        k = left;\r\n        while ((i <= mid - 1) && (j <= right)) { \r\n            if (arr[i] <= arr[j]) { \r\n                temp[k++] = arr[i++]; \r\n            } \r\n            else { \r\n                temp[k++] = arr[j++]; \r\n\r\n                inv_count = inv_count + (mid - i); \r\n            } \r\n        } \r\n\r\n        while (i <= mid - 1) \r\n            temp[k++] = arr[i++]; \r\n\r\n        while (j <= right) \r\n            temp[k++] = arr[j++]; \r\n\r\n        for (i = left; i <= right; i++) \r\n            arr[i] = temp[i]; \r\n\r\n        return inv_count; \r\n} \r\n\r\nlong long _mergeSort(int arr[], int temp[], int left, int right){ \r\n        int mid;\r\n        long long inv_count = 0; \r\n        if (right > left) { \r\n            mid = (right + left) \/ 2; \r\n\r\n            inv_count = _mergeSort(arr, temp, left, mid); \r\n            inv_count += _mergeSort(arr, temp, mid + 1, right); \r\n\r\n            inv_count += merge(arr, temp, left, mid + 1, right); \r\n        } \r\n        return inv_count; \r\n}\r\n\r\nlong long mergeSort(int arr[], int array_size){ \r\n        int temp[array_size]; \r\n        return _mergeSort(arr, temp, 0, array_size - 1); \r\n}\r\n\r\nint main()\r\n{\r\n  int test;\r\n  scanf(\"%d\",&test);\r\n\r\n  while(test--){\r\n    int n;\r\n    scanf(\"%d\",&n);\r\n\r\n    int arr[n];\r\n    for(int i=0; i<n; i++)\r\n      scanf(\"%d\",&arr[i]);\r\n\r\n    printf(\"%lld&#92;n\",mergeSort(arr,n));\r\n\r\n  }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\r\n\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_1229_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include <bits\/stdc++.h>\r\nusing namespace std;\r\n\r\nlong long merge(int arr[], int temp[], int left, int mid, int right){ \r\n        int i, j, k; \r\n        long long inv_count = 0; \r\n\r\n        i = left;\r\n        j = mid;\r\n        k = left;\r\n        while ((i <= mid - 1) && (j <= right)) { \r\n            if (arr[i] <= arr[j]) { \r\n                temp[k++] = arr[i++]; \r\n            } \r\n            else { \r\n                temp[k++] = arr[j++]; \r\n\r\n                inv_count = inv_count + (mid - i); \r\n            } \r\n        } \r\n\r\n        while (i <= mid - 1) \r\n            temp[k++] = arr[i++]; \r\n\r\n        while (j <= right) \r\n            temp[k++] = arr[j++]; \r\n\r\n        for (i = left; i <= right; i++) \r\n            arr[i] = temp[i]; \r\n\r\n        return inv_count; \r\n} \r\n\r\nlong long _mergeSort(int arr[], int temp[], int left, int right){ \r\n        int mid;\r\n        long long inv_count = 0; \r\n        if (right > left) { \r\n            mid = (right + left) \/ 2; \r\n\r\n            inv_count = _mergeSort(arr, temp, left, mid); \r\n            inv_count += _mergeSort(arr, temp, mid + 1, right); \r\n\r\n            inv_count += merge(arr, temp, left, mid + 1, right); \r\n        } \r\n        return inv_count; \r\n}\r\n\r\nlong long mergeSort(int arr[], int array_size){ \r\n        int temp[array_size]; \r\n        return _mergeSort(arr, temp, 0, array_size - 1); \r\n}\r\n\r\nint main()\r\n{\r\n  int test;\r\n  cin>>test;\r\n\r\n  while(test--){\r\n    int n;\r\n    cin>>n;\r\n\r\n    int arr[n];\r\n    for(int i=0; i<n; i++)\r\n      cin>>arr[i];\r\n\r\n    cout<<mergeSort(arr,n)<<endl;\r\n\r\n  }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_1229_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.*;\r\nimport java.io.*;\r\n\r\nclass Test { \r\n\r\n    static long mergeSort(int arr[], int array_size) \r\n    { \r\n        int temp[] = new int[array_size]; \r\n        return _mergeSort(arr, temp, 0, array_size - 1); \r\n    } \r\n\r\n    static long _mergeSort(int arr[], int temp[], int left, int right) \r\n    { \r\n        int mid;\r\n        long inv_count = 0; \r\n        if (right > left) { \r\n            mid = (right + left) \/ 2; \r\n\r\n            inv_count = _mergeSort(arr, temp, left, mid); \r\n            inv_count += _mergeSort(arr, temp, mid + 1, right); \r\n\r\n            inv_count += merge(arr, temp, left, mid + 1, right); \r\n        } \r\n        return inv_count; \r\n    } \r\n\r\n    static long merge(int arr[], int temp[], int left, int mid, int right) \r\n    { \r\n        int i, j, k; \r\n        long inv_count = 0; \r\n\r\n        i = left;\r\n        j = mid;\r\n        k = left;\r\n        while ((i <= mid - 1) && (j <= right)) { \r\n            if (arr[i] <= arr[j]) { \r\n                temp[k++] = arr[i++]; \r\n            } \r\n            else { \r\n                temp[k++] = arr[j++]; \r\n\r\n                inv_count = inv_count + (mid - i); \r\n            } \r\n        } \r\n\r\n        while (i <= mid - 1) \r\n            temp[k++] = arr[i++]; \r\n\r\n        while (j <= right) \r\n            temp[k++] = arr[j++]; \r\n\r\n        for (i = left; i <= right; i++) \r\n            arr[i] = temp[i]; \r\n\r\n        return inv_count; \r\n    } \r\n\r\n    public static void main(String[] args) \r\n    { \r\n        Scanner sc = new Scanner(System.in);\r\n        int t=sc.nextInt();\r\n        while(t-->0)\r\n        {\r\n            int n = sc.nextInt();\r\n            int arr[] =new int[n]; \r\n            for(int i=0;i<n;i++)\r\n            {\r\n                arr[i]=sc.nextInt();\r\n            }\r\n        System.out.println(mergeSort(arr, n)); \r\n    } \r\n} \r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_1229 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_1229 a\"),jQuery(\"#tab-content_1229\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t<br \/>\n<strong>Space Complexity<\/strong><\/p>\n<blockquote>\n<p>O(<code>n<\/code>) for an auxiliary array of the same size as our input array.<\/p>\n<\/blockquote>\n<p>[forminator_quiz id=&quot;1230&quot;]<br \/>\nThis article tried to discuss the concept of <strong>Sorting<\/strong>. Hope this blog helps you understand the concept of Sorting. To practice more problems you can check out <a href=\"#\">MYCODE|competitive programming<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Concepts Used Sorting Difficulty Level Hard Problem Statement (Simplified): Find number of pairs such that a[i] &gt; a[j] and i &lt; j. Test Case Input: 1 5 10 50 20 40 30 Output: 4 Explanation: Such required pairs are (50,20),(50,40),(50,30) and (40,30). So, 4 is our answer. Solving Approach : Bruteforce Approach: We can count [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[92],"tags":[36,93,91],"class_list":["post-1211","post","type-post","status-publish","format-standard","hentry","category-sorting-interview-programming","tag-interview-coding","tag-inversion-count","tag-sorting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Find the Inversion Count | Sorting Interview Programming | Prepbytes<\/title>\n<meta name=\"description\" content=\"We Can Count the Total Number of Inversions During Sorting Array Using Merge Sort Algorithm. in the Merge Sort Algorithm When We Merge Two Halves of the Array.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Find the Inversion Count | Sorting Interview Programming | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"We Can Count the Total Number of Inversions During Sorting Array Using Merge Sort Algorithm. in the Merge Sort Algorithm When We Merge Two Halves of the Array.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-10T16:19:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-06-17T06:33:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100175465-Article_322.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Find the Inversion Count\",\"datePublished\":\"2020-06-10T16:19:59+00:00\",\"dateModified\":\"2022-06-17T06:33:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/\"},\"wordCount\":400,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100175465-Article_322.png\",\"keywords\":[\"interview-coding\",\"Inversion count\",\"Sorting\"],\"articleSection\":[\"Sorting interview programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/\",\"name\":\"Find the Inversion Count | Sorting Interview Programming | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100175465-Article_322.png\",\"datePublished\":\"2020-06-10T16:19:59+00:00\",\"dateModified\":\"2022-06-17T06:33:01+00:00\",\"description\":\"We Can Count the Total Number of Inversions During Sorting Array Using Merge Sort Algorithm. in the Merge Sort Algorithm When We Merge Two Halves of the Array.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100175465-Article_322.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100175465-Article_322.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sorting interview programming\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/sorting-interview-programming\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Find the Inversion Count\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Find the Inversion Count | Sorting Interview Programming | Prepbytes","description":"We Can Count the Total Number of Inversions During Sorting Array Using Merge Sort Algorithm. in the Merge Sort Algorithm When We Merge Two Halves of the Array.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/","og_locale":"en_US","og_type":"article","og_title":"Find the Inversion Count | Sorting Interview Programming | Prepbytes","og_description":"We Can Count the Total Number of Inversions During Sorting Array Using Merge Sort Algorithm. in the Merge Sort Algorithm When We Merge Two Halves of the Array.","og_url":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2020-06-10T16:19:59+00:00","article_modified_time":"2022-06-17T06:33:01+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100175465-Article_322.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Find the Inversion Count","datePublished":"2020-06-10T16:19:59+00:00","dateModified":"2022-06-17T06:33:01+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/"},"wordCount":400,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100175465-Article_322.png","keywords":["interview-coding","Inversion count","Sorting"],"articleSection":["Sorting interview programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/","url":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/","name":"Find the Inversion Count | Sorting Interview Programming | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100175465-Article_322.png","datePublished":"2020-06-10T16:19:59+00:00","dateModified":"2022-06-17T06:33:01+00:00","description":"We Can Count the Total Number of Inversions During Sorting Array Using Merge Sort Algorithm. in the Merge Sort Algorithm When We Merge Two Halves of the Array.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100175465-Article_322.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645100175465-Article_322.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/find-the-inversion-count\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Sorting interview programming","item":"https:\/\/prepbytes.com\/blog\/category\/sorting-interview-programming\/"},{"@type":"ListItem","position":3,"name":"Find the Inversion Count"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=1211"}],"version-history":[{"count":12,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1211\/revisions"}],"predecessor-version":[{"id":8289,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1211\/revisions\/8289"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=1211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=1211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=1211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}