Array - Data Structure

Array - Data Structure

Concept

  • Same or similar data type at contiguous memory locations.

  • Time Complexity:

    • Access -> at O(1).

    • Deletion at O(n).

    • Searching -> O(n)

    • Insertion -> O(n)

  • Types:

    • 1-dimensional

    • Multi-dimesional

Untitled.png

Approaches

For an array problem, these are the only approaches through which we can solve it.

  1. Iteration: In this approach, we can solve the problem by just iterating over the array or storing some information while iterating using some extra space like map, set etc and comes to a solution.

eg: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

  1. Iteration after Sorting: In this technique, first we sort the array then iterate over it.

eg: https://leetcode.com/problems/merge-intervals/

  1. Two Pointer: Here, we use 2 pointers I and j or Start and end. When we are using two pointers or how to decide whether these pointers from same side or opposite side, we have to make sure the behavior of both pointers must be opposite.

    Two Pointers from Opposite side

    eg: https://leetcode.com/problems/two-sum/

Untitled (1).png

Two Pointers from Same Side (Sliding Window)

eg: [https://www.interviewbit.com/problems/diffk/](https://www.interviewbit.com/problems/diffk/)

![Untitled (2).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1668341138646/pTp1Fmqoe.png align="left")
  1. Prefix Sum : It is a technique which is used to solve many queries in constant time instead of linear time. And we can say it’s a form of Dynamic Programming.

eg: https://www.geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/

So this is it for this article. I hope it helped you somewhere. Don't forget to support us and share with other geekians.

Thank you, Have a nice day !!