According to wikipedia, a cache is a hardware or software component that stores data so that future requests for that data can be served faster.

- Faster access of data in O(1)
- Computation complexity once for the first time

- Memory cache
- Database cache
- Disk cache, etc

In this post, we’ll go through the steps to create a in memory cache in java.

To create a cache, we can simply use a map / dictionary data structure and we can get the expected result of O(1) for both get and put operation.

But, we can’t store everything in our cache. …

There are multiple ways to find the pair of numbers in a given array. The numbers in the array can be in two ways.

- Sorted
- Unsorted

If the numbers are sorted then we can get easily with the complexity of O(n). The brute force approach will be easily applicable for both the sorted and unsorted array of numbers.

Brute force approach will check each and every number to determine if it equals to the given sum. The complexity of this approach is O(n²).

`public void findPair(int[] array, int sum) { for(int i = 0; i < array.length - 1; i++)…`

HashMap is a dictionary data structure provided by java. It’s a Map-based collection class that is used to store data in Key & Value pairs. In this article, we’ll be creating our own hashmap implementation.

The benefit of using this data structure is faster data retrieval. It has data access complexity of O(1) in the best case.

In layman’s terms, a for each key we get the associated value.

To implement this structure,

- We need a list to store all the keys
- Key — Value relationship to get value based on key

We can have a list containing all the…

Given an array `nums`

of *n* integers where *n* > 1, return an array `output`

such that `output[i]`

is equal to the product of all the elements of `nums`

except `nums[i]`

.

**Example:**

**Input:** [1,2,3,4]

**Output:** [24,12,8,6]

Problem link: Product of Array Except Self

Solution:

Let’s start with a brute-force solution.

`public int[] productExceptSelf(int[] nums) {`

int[] arr = new int[nums.length];

for(int i = 0; i < nums.length; i++) {

int mul = 1;

for (int j = 0; j < nums.length; j++) {

if(i == j) {

continue;

}

mul *= nums[j];

}

arr[i] = mul;

}

return arr;

}

…

Software developer, passionate learner. www.mahfuz.info