Find the k smallest numbers after deleting given elements in C++


In this problem, we are given an array arr[] of size n, array del[] of size m, and an integer k. Our task is to find the k smallest numbers after deleting the given elements.

We need to print the first k smallest elements from the array arr[] found after deleting all elements present in the del[] array. If two instances are present in the array delete the first instance.

Let's take an example to understand the problem,

Input : arr[] = {3, 5, 1, 7, 9, 2}, del[] = {1, 9, 3}, k = 2
Output : 2, 5

Explanation

Array arr[] after deleting the elements : {5, 7, 2}
2 minimum elements are 2, 5.

Solution Approach

A simple solution the problem is by deleting all elements from arr[] that are present in del[]. Then sort the array in ascending order and print first k elements of the array.

Example

Program to illustrate the working of our solution

#include <bits/stdc++.h>
using namespace std;
void findKminElementDelArray(int arr[], int n, int del[], int m, int k){
   for(int i = 0; i < m; i++){
      for(int j = 0; j < n; j++){
         if(arr[j] == del[i]){
            arr[j] = INT_MAX;
            break;
         }
      }
   }
   sort(arr, arr + n);
   for (int i = 0; i < k; ++i) {
      cout<<arr[i]<<" ";
   }
}
int main(){
   int array[] = { 3, 5, 1, 7, 9, 2 };
   int m = sizeof(array) / sizeof(array[0]);
   int del[] = { 1, 9, 3 };
   int n = sizeof(del) / sizeof(del[0]);
   int k = 2;
   cout<<k<<" smallest numbers after deleting the elements are ";
   findKminElementDelArray(array, m, del, n, k);
   return 0;
}

Output

2 smallest numbers after deleting the elements are 2 5

Another approach

Another approach to solve the problem is using hashmap and heap. We will create a min-heap and a hash map. The hashmap will contain all the elements of the array del[]. And then insert elements of the array arr[] that are not present in hash-map to the min-heap. Pop k elements from the heap and then print it.

Example

Program to illustrate the working of our solution

#include <bits/stdc++.h>
using namespace std;
void findKminElementDelArray(int arr[], int n, int del[], int m, int k){
   unordered_map<int, int> deleteElement;
   for (int i = 0; i < m; ++i) {
      deleteElement[del[i]]++;
   }
   priority_queue<int, vector<int>, greater<int> > minHeap;
   for (int i = 0; i < n; ++i) {
      if (deleteElement.find(arr[i]) != deleteElement.end()) {
         deleteElement[arr[i]]--;
         if (deleteElement[arr[i]] == 0) deleteElement.erase(arr[i]);
      }
      else
         minHeap.push(arr[i]);
   }
   for (int i = 0; i < k; ++i) {
      cout<<minHeap.top()<<" ";
      minHeap.pop();
   }
}
int main(){
   int array[] = { 3, 5, 1, 7, 9, 2 };
   int m = sizeof(array) / sizeof(array[0]);
   int del[] = { 1, 9, 3 }; int n = sizeof(del) / sizeof(del[0]);
   int k = 2;
   cout<<k<<" smallest numbers after deleting the elements are ";
   findKminElementDelArray(array, m, del, n, k);
   return 0;
}

Output

2 smallest numbers after deleting the elements are 2 5

Updated on: 28-Jan-2022

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements