PHP Program For Counting Inversions In An Array - Set 1 (Using Merge Sort) Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Inversion Count for an array indicates - how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum. Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j.Examples: Input: arr[] = {8, 4, 2, 1} Output: 6 Explanation: Given array has six inversions: (8, 4), (4, 2), (8, 2), (8, 1), (4, 1), (2, 1). Input: arr[] = {3, 1, 2} Output: 2 Explanation: Given array has two inversions: (3, 1), (3, 2) Simple Method Traverse through the array, and for every index, find the number of smaller elements on it's right side of the array. This can be done using a nested loop. Sum up the counts for all index in the array and print the sum.AlgorithmTraverse through the array from start to endFor every element, find the count of elements smaller than the current number up to that index using another loop.Sum up the count of inversion for every index.Print the count of inversions.Implementation PHP <?php // PHP program to Count Inversions // in an array function getInvCount(&$arr, $n) { $inv_count = 0; for ($i = 0; $i < $n - 1; $i++) for ($j = $i + 1; $j < $n; $j++) if ($arr[$i] > $arr[$j]) $inv_count++; return $inv_count; } // Driver Code $arr = array(1, 20, 6, 4, 5 ); $n = sizeof($arr); echo "Number of inversions are " . getInvCount($arr, $n); ?> OutputNumber of inversions are 5Complexity AnalysisTime Complexity: O(n^2), Two nested loops are needed to traverse the array from start to end, so the Time complexity is O(n^2)Space Complexity:O(1), No extra space is required.Please refer complete article on Count Inversions in an array | Set 1 (Using Merge Sort) for more details! Comment More infoAdvertise with us Next Article PHP Program For Counting Inversions In An Array - Set 1 (Using Merge Sort) kartik Follow Improve Article Tags : Divide and Conquer Sorting Web Technologies PHP Programs DSA Arrays Microsoft Amazon Adobe Flipkart Myntra BankBazaar inversion Merge Sort +10 More Practice Tags : AdobeAmazonBankBazaarFlipkartMicrosoftMyntraArraysDivide and ConquerMerge SortSorting +6 More Similar Reads PHP Program to Count Inversions of size three in a given array Given an array arr[] of size n. Three elements arr[i], arr[j] and arr[k] form an inversion of size 3 if a[i] > a[j] >a[k] and i < j < k. Find total number of inversions of size 3.Example : Input: {8, 4, 2, 1}Output: 4The four inversions are (8,4,2), (8,4,1), (4,2,1) and (8,2,1).Input: {9 4 min read PHP Program to Count 1's in a sorted binary array Given a binary array sorted in non-increasing order, count the number of 1's in it. Examples: Input: arr[] = {1, 1, 0, 0, 0, 0, 0}Output: 2Input: arr[] = {1, 1, 1, 1, 1, 1, 1}Output: 7Input: arr[] = {0, 0, 0, 0, 0, 0, 0}Output: 0A simple solution is to linearly traverse the array. The time complexit 2 min read How to Sort an Array containing 1 to n values in PHP ? In this article, we will see how to sort an array that contains 1 to n values in PHP. There are various approaches through which we can sort an array with custom values. Below is an example for a better understanding of the problem statement.Example:Input: arr = [3, 1, 4, 5, 2, 8, 9, 5]Output: Sorte 6 min read PHP Program to Sort an Array Elements in Descending Order Given an array containing some elements, the task is to sort the array elements in descending order in PHP. Sorting elements of an array is a common operation in programming, and PHP provides several methods to accomplish this task.Table of ContentUsing rsort() FunctionUsing array_reverse() with sor 3 min read PHP Program for Check if an array is sorted and rotated Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 }Output : YESThe above arra 3 min read PHP Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples:Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input : arr[] = {2, 5, 6, 7, 8, 8, 9}; 3 min read PHP Program to Count Sets of 1s and 0s in a Binary Matrix Given a N x M Binary Matrix, count the number of sets where a set can be formed one or more same values in a row or column. Examples: Input: [[ 1, 0, 1 ], [ 0, 1, 0 ]] Output: 8 Explanation: There are six one-element sets (three 1s and three 0s). There are two two - element sets, the first one consi 2 min read PHP Program for Search an element in a sorted and rotated array An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time.Exampl 6 min read How to Get All Possible Pairs in Array using PHP ? Given an array containing some elements, the task is to find all possible pairs of array elements using PHP.Examples:Input: arr = [1, 2, 3]Output: [[1, 2], [1, 3]]Input: arr = [1, 2, 3, 5]Output: [[1, 2], [1, 3], [1, 5], [2, 3], [2, 5], [3, 5]]Table of ContentUsing Nested for LoopsUsing array_reduce 4 min read PHP Program to Split Array and Add First Part to the End | Set 2 Given an array, the task is to split it from a specified position, and move the first part of array and add to the end. Examples: Input : Arr = [ 12, 10, 5, 6, 52, 36 ] k = 2 Output : Arr = [ 5, 6, 52, 36, 12, 10 ] Explanation : Split from index 2 and first part {12, 10} add to the end. Input : Arr 2 min read Like