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=0
i=1
##Set the value of K here
k=3
##Get the number of Groups of Array a
(( NoOflps=(len1+k-1)/k ))
echo $NoOflps
##Run the innner while loop based on NoOflps output
while [ $i -le $NoOflps ]
do
##creating a new empty array newlist
newlist=()
##This while loop will run based on K value each time . That means it will run for each group
while [ $j -lt $((k * i)) ]
do
if [ $j -eq $len1 ]
then
break
else
newlist[j]=${a[j]}
fi
j=$(( $j + 1))
done
echo "Group Arrays are " ${newlist[*]}
i=$(( $i + 1))
done