Debug School

rakesh kumar
rakesh kumar

Posted on

Algorithms:Array in Data structure

Every beginner’s programming journey starts with solving the questions of Array. Being a programmer you might have surely used this data structure a lot in your application. This data structure is used in every possible situation where you need to collect the object in one place. From simple to complex software or web application array is mostly used to store and display the data dynamically at web pages. Let’s take one of the good examples of using an array in a real-world application…

We all must have used the online ticket booking system at least once. It might be for booking tickets for train or maybe bus or flights or movies or any other shows. If we want to book any seat, then it’s just a matter of clicking on a square and it will be booked.

Arrays
An array is a structure of fixed-size, which can hold items of the same data type. It can be an array of integers, an array of floating-point numbers, an array of strings or even an array of arrays (such as 2-dimensional arrays). Arrays are indexed, meaning that random access is possible.

Image description

Array operations

  1. Traverse: Go through the elements and print them.
  2. Search: Search for an element in the array. You can search the element by its value or its index
  3. Update: Update the value of an existing element at a given index Inserting elements to an array and deleting elements from an array cannot be done straight away as arrays are fixed in size. If you want to insert an element to an array, first you will have to create a new array with increased size (current size + 1), copy the existing elements and add the new element. The same goes for the deletion with a new array of reduced size.

Applications of arrays

  1. Used as the building blocks to build other data structures such as array lists, heaps, hash tables, vectors and matrices.
  2. Used for different sorting algorithms such as insertion sort, quick sort, bubble sort and merge sort.

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Problems on Array

Image description

Image description

while searching element it always takes o(n) complexity
Enter fullscreen mode Exit fullscreen mode

In an array we can not store different kinds of values It will store only the same data type of values at a time.
Array size is fixed.
So to overcome these problems of array new data structure came into the picture which is known as LinkedList. (Next post)

Some Common coding interviews Problems related to array:

  1. Reverse an array.
  2. Sum of array elements.
  3. Find maximum and minimum elements of an array.
  4. Search an element in an array.
  5. Find the kth max and min element in an array.
  6. Sort array of 0, 1, 2. with and without extra space.
  7. Rotate array by one.
  8. Find missing numbers from an array.
  9. Move all negative numbers to one side of the array.
  10. Union and Intersection of two arrays
  11. Longest consecutive subsequence in an array.

Top comments (0)