Smallest Number

CONCEPTS USED:

Hashing

DIFFICULTY LEVEL:

Easy

PROBLEM STATEMENT(SIMPLIFIED):

Given an array A with N integers and an integer K, print the smallest number in the array which occurs exactly K times.

See original problem statement here

For Example:

Input : N = 5, K = 2
        arr[] = [2, 2, 1, 3, 1]

Output : 1 (both 1 & 2 occurs 2 times but 1 < 2)

SOLVING APPROACH:

  1. With reference to competitive programming course online, Store the frequency of all elements in a temporary hash array.

  2. Traverse the hash array from (1 to Size of Hash array) and whenever the frequency of an element matches with K, print it and return.

SOLUTIONS:

[TABS_R id=1606]
[forminator_quiz id=”1662″]

Space Complexity: O(N), due to additional hash array.

Previous post Small Count
Next post Next Greater Same Digit

Leave a Reply

Your email address will not be published. Required fields are marked *