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[0] < arr[1]  > arr[2] < arr[3] > arr[4] < . . . . 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…

Sum all Sub Array in Linux

Consider Array (1 2 3) , Below are the Sub-Array for this Array and their sum.(1): Sum of this sub-array is 1(2): Sum of this sub-array is 2(3): Sum of this sub-array is 3(1 2): Sum of this sub-array is 3(2 3): Sum of this sub-array is 5(1 2 3): Sum of this sub-array is 6So, there are 6 Sub Array in this Particular Array.Now, we need to find out sum of all these Array using shell scripting.NOTE : You can remove the outer most while loop and just pass…

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…

Occurance of an element in an Array

In this blog, sharing code to find the occurrence of an element in an Array. In other words, find how many times a elements comes in an array.Explaination of Shell Script:1. Here we use dictionary which has key and value pair. 2. keys are mapped to the element of Array here.3. Setting value of each Key(which is element) equal to 1.4. Increment the value of key if it comes again by 1. arr=(4 3 1 1) ## First Array of 4 elementslen_arr=${#arr[@]} ## Length of Array Arr.i=0declare -A dict ##…

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…