top of page
ishitajuneja01

All you need to know about Arrays and Subarray



When you first step into the world of programming, arrays are definitely one of the first concepts that you will come across!


Did you know that the concept of arrays is commonly used in different real-time situations on a regular basis that we often tend to overlook?


From arranging an egg carton to a box of juices, arrays are present anywhere and everywhere!


So, what is an Array?


Arrays are basically structures of data that are used for storing elements in specific locations of the memory.


Within arrays we also have the concept of Subarrays that are basically subsets of a bigger array that are used for finding connotations of coding problems such as Subarrays with given XOR.


In this blog, find all you need to know about Arrays and Subarrays in detail!




What are Arrays?


An array can be defined as a collection of elements/items that are stored within contiguous memory locations.


The data present in these elements or items follow the same pattern, which means that the characteristics of data stored in these memories are the same.


Since the elements are stored in contiguous memory locations, it makes it easier and efficient for the programmers to find the value of each of the elements.


Here how that can be achieved:


  • In order to calculate the value and position of the elements within an array, the programmer has to simply add an offset to its base value

  • The base value is the initial index i.e 0 and the difference between two indexes would be considered the offset.

Now, an important concept that is often discussed along with arrays is the Subarrays.




What are Subarrays?


Subarrays are also a contiguous part of the arrays. As the name suggests, the Subarrays are present within an array.


For instance let's consider that we have an array of size s. A subarray with given XOR for this array would be denoted by the following expression:


s*(s+1)/2


Now, the question arises on how you can find a subarray within a given array.


The most common approach used for finding Subarrays within an array is the nested loop approach.




Finding the Subarray within an Array


Well, since we are on the subject of Nested Loops, they can be defined as follows:


A nested loop is often described as a loop statement that is present within another loop statement. In other terms, a nested loop is also referred to as "loop inside loops".


Now, using the Nested Loop Approach is the best strategy for finding Subarrays within an Array, consider the following steps for the same:


  • Start by traversing the array from the beginning till the end.

  • At the start of each index, you need to start a loop i.e i, this loop shall continue till the end of the array.

  • Also, bear in mind to keep the variable to currentSum in order to calculate all the sum within each of the Subarray.

  • Now, start updating the sun for every index of the inner loop using currentSum = currentSum + arr[j]

  • If you find that the currentSum is equal to your given sum then you can proceed to print the Subarray.


So now we have developed a general idea of how to figure out the location of Subarrays with given XOR within an array.


We will now proceed to our previous discussion on the arrays and have a look at a few of the important concepts that are relevant to Arrays and Subarrays.




Do Arrays always have a fixed size?


In the context of C programming, the arrays always have a fixed size and structure. The size of the array in C cannot be tampered with by either the users or the programmers.


One can neither expand nor shrink an array. The main reason for this is that if we try to expand the size of the array then we will not be able to determine whether there is enough memory location to store the size of the array.


Similarly the process of shrinking will also be inapplicable because, when an array is created a specific memory location is pre-allocated to the array itself.


This memory location can only be destroyed by a compiler, thus, both the processes of expanding and shrinking an array becomes null and void.


The structure of an array is quite simple. An array mainly consists of elements and indexes that point to these elements that are stored in contiguous memory locations.


These indexes are used to point towards elements present within an array that are either numbers or fields containing a number starting from the first element.


There are different types of indexes present in an array. Let's discuss them in the following section.




Types of indexes used in an Array


In order to perform indexing in an Array you can use the following types of valid array indexes:


  • Zero based index

In this form of indexing, the first element of the array is indexed from the subscript 0.


  • One based index

As the name suggests, the first element of the array is indexed from the subscript 1.


  • N-based index

In this form of index, the base of the index remains independent. Meaning that it can be chosen randomly and freely.


Many programming languages prefer using the n-based index and also the negative index values so that they can perform functions like enumeration.


Now that we have a clear understanding on how to locate different elements within an array using indexes and the different types of indexing used in an array, let's discuss the types of arrays used in programming.




What are the different types of Arrays?


In the context of Arrays, you will find the following types:


  • One dimensional arrays

Also referred to as 1-D arrays, the one dimensional arrays contain variables that have a similar data type.


  • Two dimensional arrays

Also referred to as 2-D arrays, this form of arrays consists of lists of lists within a variable of similar data type.

A two-dimensional array is known as a multidimensional array as well




Final Thoughts


Did you know that the amount of use cases of arrays in programming is almost unlimited?


Arrays is one of the most commonly used structures in programming that can store infinite amounts of data quite like a bst iterator also known as a Binary Search Tree.


4 views0 comments

Recent Posts

See All

DBMS Mastery: An Online Learning Adventure

Introduction In the dynamic landscape of technology and data-driven industries, the mastery of Database Management Systems (DBMS) is...

Comments


bottom of page