Find the smallest after deleting given elements using C++

In this problem, we are given two arrays arr[] and del[]. Our task is to find the smallest after deleting given elements.

We will be deleting values from the array arr[] that are present in del[]. And then print the smallest value after deletion.

Let’s take an example to understand the problem,

Input

arr[] = {2, 5, 6, 9, 1}
del[] = {1, 5, 9}

Output

2

Solution Approach

A simple solution to the problem is using hashing. We will insert all the values of del[] array in the hash table. Then we will traverse the array arr[] and check if the values in the hash table. If it is smaller than minVal. If yes, print minVal.

Example

Program to illustrate the working of our solution

#include 
using namespace std;
int findSmallestVal(int arr[], int m, int del[], int n){
   unordered_map delVals;
   for (int i = 0; i 

Output

The smallest value after the deleting element is 12
Updated on: 2022-02-14T09:21:12+05:30

213 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements