Java and Algorithm problem Please show all the steps and com
Java and Algorithm problem. Please show all the steps and comments to solve it.
Given a sorted array of integer, A, with possible duplicate elements. Implement an efficient function in Java to find in A, the numbers of occurrences of the input value k. For example, in an array A = {-1, 2, 3, 5, 6, 6, 6, 9, 10} and k = 6, your program should return 3. What is the running time complexity of your function? Justify.Solution
a) function to find duplicate elememts count :
public int A()
{
int k = 0; //k is avariable which counts the duplicates
//the code to get the number of duplicates in the list
for (int i = 0; i < list.size()-1;i++)
{
boolean found = false;//boolean variable which returns true when it finds a duplicate eliment in an array
for (int j = i+1; !found && j < list.size(); j++)
{
if (list.get(i).equals(list.get(j)));
found = true;
k++;//duplicates count
}
}
return k;//returning duplicates count
}
algorithm for that function to find the count of duplicate elements:
step1: define a variable which stores duplicate values
step2: for loop for verifying length of the array
step 3:inner for lopp of verifying duplicate values
a)define a boolean variable with default value as \"false\"
b)verify if any duplicate value found then the boolean variable returns \"true\"
c)any new element found as duplicate then count increases until the end of the array
step:return the count of the duplicate variable\"k\"
b)the running time complexity for this function is O(n^2)
why because in this function we are using two for loops
each time the lopp is verifying all the mandatory checks and this process is repeated till the end
