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…

# Tag: Array in linux

## 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…

## 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…

## 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…

## Reverse elements of an Array in Linux

In this blog, sharing code to reverse the array elements. arr=(1 2 3 4) ## Array of 4 Elementsarr_2=() ## Another Array of zero Elements or you can say blank Arraylen=${#arr[@]} ## It gives the length of an Array arrwhile [ $len -gt “0” ]dolen=$(( $len – 1))arr_2+=(${arr[len]})doneecho ${arr_2[*]} ##It will print (4 3 2 1)