please convert the code to C language // C++ program to find maximum distance between // two occurrences of same element in array #include using namespace std; int maximum_distance_bw_occurences(vector arr, int n) { //create a hash table where for each key the //hash function is h(arr[i])=arr[i] //we will use stl map as hash table and //will keep i stored(i of arr[i]) //so say two keys arr[0] and arr[5] are mapping //to the same location, then the location will have value 0,5 //instead of the keys itself map > hash; //for each number for (int i = 0; i < arr.size(); i++) { hash[arr[i]].push_back(i); } //now to find max distance b/w two occurrences //we need to check difference b/w first and //last position for each unique keys //maxdiff=max(last-first) for each unique key int maxdiff = 0; for (auto it = hash.begin(); it != hash.end(); it++) { int first = it->second[0]; int last = it->second[it->second.size() - 1]; if (last - first > maxdiff) { maxdiff = last - first; } } //so ans will be updated maxdiff return maxdiff; } int main() { int n; cout << "Enter number of elements\n"; cin >> n; vector arr(n, 0); cout << "Input the array elements\n"; for (int i = 0; i < n; i++) { cin >> arr[i]; } cout << "Minimum number of deletion required to make all elements same is: "; cout << maximum_distance_bw_occurences(arr, n) << endl; return 0; }
please convert the code to C language
// C++ program to find maximum distance between
// two occurrences of same element in array
#include <bits/stdc++.h>
using namespace std;
int maximum_distance_bw_occurences(
{
//create a hash table where for each key the
//hash function is h(arr[i])=arr[i]
//we will use stl map as hash table and
//will keep i stored(i of arr[i])
//so say two keys arr[0] and arr[5] are mapping
//to the same location, then the location will have value 0,5
//instead of the keys itself
map<int, vector<int> > hash;
//for each number
for (int i = 0; i < arr.size(); i++) {
hash[arr[i]].push_back(i);
}
//now to find max distance b/w two occurrences
//we need to check difference b/w first and
//last position for each unique keys
//maxdiff=max(last-first) for each unique key
int maxdiff = 0;
for (auto it = hash.begin(); it != hash.end(); it++) {
int first = it->second[0];
int last = it->second[it->second.size() - 1];
if (last - first > maxdiff) {
maxdiff = last - first;
}
}
//so ans will be updated maxdiff
return maxdiff;
}
int main()
{
int n;
cout << "Enter number of elements\n";
cin >> n;
vector<int> arr(n, 0);
cout << "Input the array elements\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Minimum number of deletion required to make all elements same is: ";
cout << maximum_distance_bw_occurences(arr, n) << endl;
return 0;
}
Output:
Step by step
Solved in 3 steps with 2 images