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

# Category: Shell Script

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

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

## Append elements of an Array to another Array

In this blog, sharing code of appending elements of one Array to another. arr=(1 2 3 4) ## Array of 4 Elementsarr_2=(5 10 12 1) ## Another Array of 4 Elementsi=0len=${#arr_2[@]} ## It gives the length of an Array arr_2while [ $i -lt $len ]doarr+=(${arr_2[i]}) ## Appending elements of arr_2 into array arr.i=$(( $i + 1))doneecho ${arr[*]}