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...


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="">
    cin >> n;


    vector arr(n, 0);


    cout < "input="" the="" array="">


    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)=""><>

    return 0;
}




Output:



Enter number of elements<br>10<br>Input the array elements<br>1 2 3 1 2 4 5 6 2 3<br>Minimum number of deletion required to make all elements same is: 7<br>

Extracted text: Enter number of elements 10 Input the array elements 1 2 3 1 2 4 5 6 2 3 Minimum number of deletion required to make all elements same is: 7

Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here