- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the largest after deleting the given elements in C++

In this problem, we are given an array aar[] of size n and another array del[] of size m. Our task is to find the largest after deleting the given elements. If the deletion of an element with multiple instances is required, delete the first instance of the element.

**Let's take an example to understand the problem,**

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

**Explanation** −

Array arr[] after deleting the elements : {5, 7, 2} Largest element of the array is 7

## 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 the last element of the array.

## Example

Program to illustrate the working of our solution

#include <bits/stdc++.h> using namespace std; int findMaxElementDelArray(int arr[], int n, int del[], int m){ 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); return arr[(n-m-1)]; } 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]); cout<<"The largest element after deleting the elements is "<<findMaxElementDelArray(array, m, del, n); return 0; }

## Output

The largest element after deleting the elements is 7

**Another Approach**

A solution to the problem is using hash-map for checking deletion. We will store the elements of the del[] array to a hash map and for each element of arr[] check for its presence in hash-map. If it is present delete the element from the hash-map otherwise check if it is the maximum value by comparing it with maxVal. If yes, store it to maxVal. After the whole array arr[] is traversed print maxVal which is the maximum value after deleting elements.

## Example

Program to illustrate the working of our solution

#include <bits/stdc++.h> using namespace std; int findMaxElementDelArray(int arr[], int m, int del[], int n){ unordered_map<int, int> delMap; for (int i = 0; i < n; ++i) { delMap[del[i]]++; } int maxVal = INT_MIN; for (int i = 0; i < m; ++i) { if (delMap.find(arr[i]) != delMap.end()) { delMap[arr[i]]--; if (delMap[arr[i]] == 0) delMap.erase(arr[i]); } else maxVal = max(maxVal, arr[i]); } return maxVal; } 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]); cout<<"The largest element after deleting the elements is "<<findMaxElementDelArray(array, m, del, n); return 0; }

## Output

The largest element after deleting the elements is 7

- Related Questions & Answers
- Find the k largest numbers after deleting the given elements in C++
- Find the smallest after deleting given elements using C++
- Find the k smallest numbers after deleting given elements in C++
- Program to find minimum amplitude after deleting K elements in Python
- Find largest word in dictionary by deleting some characters of given string in C++
- Find the largest twins in given range in C++
- Maximum possible middle element of the array after deleting exactly k elements in C++
- Find the Largest Cube formed by Deleting minimum Digits from a number in Python
- Find the Largest Cube formed by Deleting minimum Digits from a number in C++
- Find the largest three elements in an array in C++
- Find all possible substrings after deleting k characters in Python
- Reset the primary key to 1 after deleting all the data in MySQL?
- Program to find minimum amplitude after deleting KLength sublist in Python
- Find the largest good number in the divisors of given number N in C++
- Find the largest Perfect Subtree in a given Binary Tree in Python