## Count Inversions in Array

Consider an Array (2 4 1 3 5). Inversions are those pairs where a[i]>a[j] and i<j . For above mentioned array, below are the pairs of Inversions. So, total Count Inversions are 3 for this Array.(2 1)(4 1)(4 3)Below is the code for the same using shell scripting. a=(2 4 1 3 5)len=\${#a[@]}lngthminusone=\$(( \$len -1))i=0## This while loop will run inner loop for each element.while [ \$i -lt \$len ] doj=0## This while loop will compare one element with rest of the elements.while [ \$j -lt \$lngthminusone ] doj=\$(( \$j…

## kth smallest element in array

This is continuation of sorting of array elements. Please go through this blog . In that blog, sorting of array elements is explained along with code. In order to find out kth smallest or kth largest element in array having all distinct elements , we need to sort this array first .Once sorting is done , then it is straight forward to get kth element (smallest or largest element) using a[k-1]Here I am sharing code for smallest kth element of an array. For kth largest element is achieved through descending…

## Groups in Array

Consider an array of length N (1 2 4 5 10) and k=3. Below are the groups of Arrays(1 2 4) and (5 10)There is a difference between sub-array and groups in array. They are not at all same.Sub Array of k=3 are following.(1 2 4) , (2 4 5) and (4 5 10) In this blog, we are discussing about groups in array not sub-array. a=(1 2 4 5 10)len1=\${#a[@]}j=0i=1##Set the value of K herek=3##Get the number of Groups of Array a(( NoOflps=(len1+k-1)/k ))echo \$NoOflps##Run the innner while loop…

## Rearrange array alternatively

Consider an array (for example (1 3 2 4 6 12))of even number of elements like 2,4 or any number which is even . Need to arrange array in below order.First maximum First Minimum Second Max Second Min Third Max Third Min…. I have divided code into 4 parts.## Arrange elements in Descending order and create a blank array newlist of same length with value 0## arr=(1 3 2 4 6 12)len=\${#arr[@]}k=0newlist=()lngthminusone=\$(( \$len -1))echo \$lngthminusonewhile [ \$k -lt \$len ]doi=0value=0newlist+=\$valuewhile [ \$i -lt \$lngthminusone ]doj=\$(( \$i + 1))if [ \${arr[i]}…

## All Sub Array having sum=0

In this blog, first we need to understand sub array definition and code to find sum of all Sub-Array in an Array. For that, please refer to this blog. Now, we need to find all Sub-Array whose Sum is equal to zero. Consider an array (1 -1 2 1 -3) and there are three sub-array whose sum is zero mentioned below.( 1 -1)(2 1 -3)(1 -1 2 1 -3) Below is the Shell Script which will give all sub-array whose sum is 0 . arr=(1 -1 2 1 -3) ##…

## zig-zag arrangement in Array

Consider an array a= (1 4 3 -1 10 6) of length N, so zig-zag arrangement of elements is arr < arr  > arr < arr > arr < . . . . arr[n-2] < arr[n-1] > arr[n] Here, first we need to run code which will sort the data in descending order .For this you can refer to this blog. Once you arranged the data in descending order, then use the below code to bring elements in zig-zag order. i=0DescArr=(10 6 4 3 1 -1) ## Array in desc…

## Missing element in an Array

Consider a Array of N-1 elements which has values from 1 to N which means one of the element is always missing. So, In this Blog, sharing the code for the same.For Example a=( 4 3 2 5) , here number of elements is N-1 which means 4 and values ranges from 1 to N(which means 5) and hence we need to print that missing element or number is 1. arr=(4 3 2 5) ## Array of 4 elements(N-1)len_arr_1=\$(( \${#arr[@]} +1)) ##Making length equal to N (5)len_arr=\${#arr[@]} ## Traversing all…

## TWO ARRAYS ARE EQUAL OR NOT

In this blog, sharing code to check whether two arrays are equal or not . Below is the code explanation in 4 steps. 1. Check the length of each Array, if length is same , then we can compare , else exit.2. If length is same for both the Arrays, then arrange them in either ascending or descending order.3. Once arrays are sorted in an order, then compare element of each Array one by one and if elements are same, increment a dummy counter.4. If the value of counter is…

## Check whether length of two array is same or not in Linux

In this blog, sharing code to check whether length of two arrays is same or not. arr=(4 3 2 1) ## First Array of 4 elementsarr_2=(4 3 1 10 1) ## Second Array of 5 elementslen_arr=\${#arr[@]}len_arr_2=\${#arr_2[@]}if [ \$len_arr -eq \$len_arr_2 ]thenecho “Length of both Array is same”fiif [ \$len_arr -ne \$len_arr_2 ]thenecho “Length of both Array is not same”fi

## SORT ELEMENTS OF AN ARRAY IN LINUX

In this Blog, sharing code to sort the elements of an array in ascending and descending order as well.ASCENDING ORDER arr=(4 3 2 1) ## Array of 4 Elementslen=\${#arr[@]} ## It gives the length of an Array arrk=0lngthminusone=\$(( \$len – 1))while [ \$k -lt \$len ] ##Outer Loop which will run len-1 times where len is length of Arraydoi=0 ## This variable set to 0 every time inner loop startswhile [ \$i -lt \$lngthminusone ] ## Inner loop which will arrange an element of an array in ascending order. Outer…