Product of Array Except Self

In this tutorial, I am going to discuss a very interesting problem Product of array except self or product of every integer except the integer at that index.

Given an array of N integers where N > 1. Write a code to print the result/output such that result[i] is equal to the product of all the elements of an array except self (result[i]).

For example –

Input: {4, 2, 1, 7}

Output: {14, 28, 56, 8}

NOTE: We have to solve this problem without division and in linear time O(n).

Find Top K Frequent Elements |3 Approaches

Find top k frequent elements in an array of integers. Let’s first understand the problem statement and the we will solve this problem using multiple approaches.

Given a non-empty array of integers, return the k most frequent elements.

Example 1:

Input: arr = {3, 4, 4, 4, 7, 7}, k = 2

Output: {4, 7}

Two most frequent elements are 4 and 7.

Example 2:

Input: arr = {3}, k = 1

Output: {3}

NOTE:

* k is always valid, 1 ≤ k ≤ number of unique elements.

* The time complexity must be better than O(nlogn), where n is the array’s size.

* It’s guaranteed that the answer is unique, in other words, the set of the top k frequent elements is unique.

* Answer can be returned in any order.

Reverse a Linked List

Given a singly linked list, write a code to reverse a linked list. In this tutorial, you are going to learn how to reverse a linked list in java using iterative approach.

For example –

Input linked list.

15 -> 9 -> 8 -> 5 -> NULL

When we reverse this linked list then the output is given below.

5 -> 8 -> 9 -> 15 -> NULL

In input linked list, head points to a node whose value is 15 but when it’s reversed the head points to a node whose value is 5.