Arrays

An array is a data structure that can hold a fixed number of elements, all of the same type. It is like a collection or a list where each element has a specific position or index. The elements in an array are stored in contiguous memory locations, meaning they are placed one after the other in the computer's memory.

Arrays are used to store and organize related data, such as a list of numbers, names, or any other type of information. The index of an element in an array represents its position, starting from zero for the first element, then one for the second, and so on.

Arrays.PNG

Properties of Arrays:

  1. Fixed Size: Arrays have a fixed size, meaning they can hold a specific number of elements. Once an array is created, its size cannot be changed dynamically. You need to define the size of an array before using it.
  2. Ordered Elements: Elements in an array are ordered, which means they have a specific position or index. The order of elements is typically based on their insertion into the array.
  3. Indexing: Each element in an array can be accessed using its index. The index represents the element's position in the array, starting from zero for the first element. For example, the first element of an array is accessed using index 0, the second element with index 1, and so on.
  4. Homogeneous Elements: Arrays store elements of the same type. This means that all elements in an array must have the same data type, such as integers, floating-point numbers, characters, or custom objects.
  5. Random Access: Arrays allow for efficient random access to elements. Since elements are stored in contiguous memory locations, accessing an element by its index is a constant-time operation. This means that regardless of the size of the array, accessing any element takes the same amount of time.
  6. Iterating: It is easy to iterate over all the elements in an array using loops. By using a loop, you can sequentially access each element of the array and perform operations on them.
  7. Memory Efficiency: Arrays are memory-efficient because they store elements in a contiguous block of memory. This makes them suitable for situations where direct and efficient memory access is required.
  8. Mutability: The elements of an array can be modified, added, or removed. You can change the value of an element by assigning a new value to its corresponding index.

Where can arrays be used?

Disadvantages of arrays

  1. Fixed Size: Arrays have a fixed size, meaning that once you define the size of an array, it cannot be easily changed. If you need to store more elements than the predefined size, you would have to create a new, larger array and copy the elements from the old array to the new one. This can be inefficient and time-consuming.
  2. Wasted Memory: Arrays may waste memory if they are not fully utilized. Since arrays have a fixed size, if you allocate an array with a larger size than necessary, you may end up with unused or empty elements. This can lead to inefficient memory usage.
  3. Insertion and Deletion: Inserting or deleting elements in an array can be inefficient. If you want to insert an element at the beginning or middle of an array, you would need to shift all the subsequent elements to make space. Similarly, deleting an element would require shifting all the elements after it. These operations can be time-consuming, especially for large arrays.
  4. Contiguous Memory Requirement: Arrays require contiguous memory locations to store elements. This means that if there is no continuous block of memory available to accommodate the array's size, creating an array can fail. In situations where memory fragmentation occurs or when working with large arrays, finding a contiguous block of memory can be a challenge.
  5. Lack of Flexibility: Arrays are not very flexible in terms of accommodating different data types or changing data sizes. As mentioned earlier, arrays can only store elements of the same type, and their size is fixed. If you need to store different types of data or have dynamic sizing requirements, other data structures like lists or dynamic arrays may be more suitable.

Time Complexity of various Operations

  1. Accessing an Element: O(1) - Constant time complexity. Accessing an element in an array by its index takes the same amount of time, regardless of the size of the array.
  2. Searching: O(n) - Linear time complexity. In the worst case, you may need to iterate through the entire array to find the desired element. The time it takes to search grows linearly with the size of the array.
  3. Insertion/Deletion at the End: O(1) - Constant time complexity. If you are inserting/deleting an element at the end of the array, it can be done in constant time since you know the index where the new element should go.
  4. Insertion/Deletion at the Beginning: O(n) - Linear time complexity. If you insert or delete an element at the beginning of an array, all the subsequent elements need to be shifted to accommodate the change. This requires iterating through and shifting elements, resulting in a time complexity that grows linearly with the size of the array.
  5. Insertion/Deletion in the Middle: O(n) - Linear time complexity. Similar to insertion/deletion at the beginning, inserting or deleting an element in the middle of an array requires shifting subsequent elements. This operation also has a time complexity that grows linearly with the size of the array.
  6. Appending: O(1) amortized - When appending elements to an array, the time complexity is usually considered amortized O(1). This means that although occasional reallocations and copying of the array may occur to accommodate a larger size, on average, the time complexity of appending elements remains constant.
  7. Sorting: O(n log n) - The most efficient sorting algorithms have a time complexity of O(n log n) when sorting an entire array. Examples of such algorithms include quicksort, mergesort, and heapsort. However, some specialized sorting algorithms, like counting sort or radix sort, can achieve better time complexities for specific data distributions or constraints.

arrayPractice.py

Top 50 Array Coding Problems for Interviews

Kadane’s Algorithm

Let's break down the Kadane's algorithm step by step in a way that is easy to understand:

Let's go through the steps for our example:

That's it! Kadane's algorithm is a simple yet effective technique to solve the maximum subarray sum problem.

What are some easy to hard level problems where we can use kadane's algorithms?

Kadane's algorithm is commonly used to solve a variety of problems related to finding the maximum subarray sum. Here are some examples of problems of varying difficulty levels where Kadane's algorithm can be applied:

  1. Easy Level:

  2. Medium Level:

  3. Hard Level:

These are just a few examples to give you an idea of the types of problems where Kadane's algorithm can be applied. However, there are many other variations and extensions of the problem that require different approaches or modifications to Kadane's algorithm.