Write a c++ program Suppose you have a single linked list of integers, the numbe
ID: 3759534 • Letter: W
Question
Write a c++ program Suppose you have a single linked list of integers, the numbers could be repeated consecutively. Write a function that will count how many times a number is repeated, the function should printout which number was repeated the most and also printout how many times it was repeated.Write a c++ program Suppose you have a single linked list of integers, the numbers could be repeated consecutively. Write a function that will count how many times a number is repeated, the function should printout which number was repeated the most and also printout how many times it was repeated.
Suppose you have a single linked list of integers, the numbers could be repeated consecutively. Write a function that will count how many times a number is repeated, the function should printout which number was repeated the most and also printout how many times it was repeated.
Explanation / Answer
#include<iostream>
using namespace std;
// Returns maximum repeating element in arr[0..n-1].
// The array elements are in range from 0 to k-1
int maxRepeating(int* arr, int n, int k)
{
// Iterate though input array, for every element
// arr[i], increment arr[arr[i]%k] by k
for (int i = 0; i< n; i++)
arr[arr[i]%k] += k;
// Find index of the maximum repeating element
int max = arr[0], result = 0;
for (int i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
result = i;
}
}
/* Uncomment this code to get the original array back
for (int i = 0; i< n; i++)
arr[i] = arr[i]%k; */
// Return index of the maximum element
return result;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.