To get **O(1) insertion and O(1) search**.

You take a set of elements that are sparsely populated across large domains and you use a mathematical hash function to map these uniformly to a new but smaller spectrum of numbers.

In arrays, we do get the insertion time as O(1) but the worst case for finding any elements inside the** array** is O(N). In **Linked List**, we get the insertion time as O(1), if we are adding at the beginning and O(N) if we are adding the element at the end. In **stacks and queues**, the search time is O(N).

Using Bucket Sort/Count sort.

**Range of collection**=> The collection could be of any range, if we are storing Integer type, then it could range from -2,147,483,648 to 2,147,483,647**Size of collection**=> The size of the collection that we are pushing. 3.**Range of Bucket/Hashed Values**=> The range for the bucket. 4.**Hashing Functions**=> Hashing function that will reduce the clash of elements as low as possible.

Step 1: Value => HashFunction => Hashed Key (O(1)) Step 2: Go to the hashed key bucket, you will find the bucket collection, you need to search for the value in that collection. O(K)

No matter how good a hashing function you create, just because you are mapping a much bigger domain to a much smaller range **"Collision can always happen"**. And the collision is always costly as you need to manage the subcollection of elements. The hashing function should be defined in such a way that collision is as minimum as possible.

How we handle collision is what defines the time complexity hit we get.

Time Complexity for Linked List collision management tool = O(K) Time Complexity for Binary Search Tree Collision Management Tool=O(log K)

]]>Let's say you need to find the sum from index i to index j. You can find by iterating from index i to index j and adding all the elements. And time complexity will be O(N).

```
sum=0;
for(int m=i; m<=j;m++){
sum=sum+A[m];
}
```

Let's assume we have total q queries, How we will find the sum for each and every query?

We have to write one for loop for iterating upto q no. of queries and inside that for loop we have to find the sum of the range[i,j] of elements in the given query.

```
for(int c=0;c<q;c++){
sum=0;
for(int m=i; m<=j;m++){
sum=sum+A[m];
}
}
```

Here the time complexity will be O(Q.N).

Now the queries any user can give could be infinite. So this would become worse time complexity. In order to tackle this problem, we could use Prefix sum.

Let's take another example, you are watching the game of cricket and at every over, you're noting 📝 down total runs scored by the team. Let's say this is the score you have noted [6,8,20,24,34,44,56,72,82,100]. Now if I ask you what is score that team scored between over 2 to 5. How will you calculate? You will subtract total score of team after completion of 5th over with total score of team at 1st over. You'll give answer 34-6 = 28.

How will you calculate score after over 10 completion? It will last element that is 100.

How will you calculate score from 4 to 7th over? {Score after 7th over - score at 3rd over}

Similarly, If we need to calculate score from over i to over J. We could calculate this in O(1) by just subtracting element at j with element at (i-1).

Now, Let's consider our 1st example.

If you are given an array A with elements [-2,6,4,1,8] and you need to find the sum of subarray. How will you find?

=> We can now come with a approach where we can create another array with the sum of element till ith position. Now, We will come up with this array:

A' = [-2, 4, 8, 9, 17]

This is called prefix sum.

Now, if we need to find the sum of elements from ith position to jth position, we could just subtract element at jth position in new array A' with element at (i-1)th position in A'.

Now if we have hundred queries then we could get the answer in O(1) time complexity.

The time complexity of whole algorithm is reduced to O(q + N).

Whenever, we are given sum in the range, the first thing that should strike in our mind is prefix sum, as it could help us in solving the problem in much easier way.

-------------------------------

]]>