Implement a function that receives an array of integers "arr" and an integer "int", which returns the number of occurrences of element "int" in array "arr". For instance, given arr = [2,3,4,3,2,1] and int=3, the function should return 2.
Attachments:
Answers
Answered by
9
Answer:
Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn)
Examples:
Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2
Output: 4 // x (or 2) occurs 4 times in arr[]
Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 3
Output: 1
Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 1
Output: 2
Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 4
Output: -1 // 4 doesn't occur in arr[]
Answered by
1
Hi! I am Mridul
I am 14 years old
But I can answer this
Here is function =>
Int arr[] = [2,3,4,3,2,1];
//let us have number that you are finding
int x = 4;
//then loop through the array and compare them with y
for(int i=0 ; i if(arr[i]==x){
System.out.println(i);
}
}
I am 14 years old
But I can answer this
Here is function =>
Int arr[] = [2,3,4,3,2,1];
//let us have number that you are finding
int x = 4;
//then loop through the array and compare them with y
for(int i=0 ; i if(arr[i]==x){
System.out.println(i);
}
}
Similar questions