[geeksforgeeks] Count the number of occurrences in a sorted array

Count the number of occurrences in a sorted array

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[] 

Method 1 (Linear Search)
Linearly search for x, count the occurrences of x and return the count.

Time Complexity: O(n)

Method 2 (Use Binary Search)
1) Use Binary search to get index of the first occurrence of x in arr[]. Let the index of the first occurrence be i.
2) Use Binary search to get index of the last occurrence of x in arr[]. Let the index of the last occurrence be j.
3) Return (j – i + 1);

 

/* if x is present in arr[] then returns the count of occurrences of x, 
otherwise returns -1. */
int count(int arr[], int x, int n)
{
int i; // index of first occurrence of x in arr[0..n-1]
int j; // index of last occurrence of x in arr[0..n-1]
    
/* get the index of first occurrence of x */
i = first(arr, 0, n-1, x, n);

/* If x doesn't exist in arr[] then return -1 */
if(i == -1)
    return i;
    
/* Else get the index of last occurrence of x. Note that we 
    are only looking in the subarray after first occurrence */
j = last(arr, i, n-1, x, n);     
    
/* return count */
return j-i+1;
}

/* if x is present in arr[] then returns the index of FIRST occurrence 
of x in arr[0..n-1], otherwise returns -1 */
int first(int arr[], int low, int high, int x, int n)
{
if(high >= low)
{
    int mid = (low + high)/2; /*low + (high - low)/2;*/
    if( ( mid == 0 || x > arr[mid-1]) && arr[mid] == x)
    return mid;
    else if(x > arr[mid])
    return first(arr, (mid + 1), high, x, n);
    else
    return first(arr, low, (mid -1), x, n);
}
return -1;
}


/* if x is present in arr[] then returns the index of LAST occurrence 
of x in arr[0..n-1], otherwise returns -1 */
int last(int arr[], int low, int high, int x, int n)
{
if(high >= low)
{
    int mid = (low + high)/2; /*low + (high - low)/2;*/
    if( ( mid == n-1 || x < arr[mid+1]) && arr[mid] == x )
    return mid;
    else if(x < arr[mid])
    return last(arr, low, (mid -1), x, n);
    else
    return last(arr, (mid + 1), high, x, n);     
}
return -1;
}

/* driver program to test above functions */
int main()
{
int arr[] = {1, 2, 2, 3, 3, 3, 3};
int x = 3; // Element to be counted in arr[]
int n = sizeof(arr)/sizeof(arr[0]);
int c = count(arr, x, n);
printf(" %d occurs %d times ", x, c);
getchar();
return 0;
}

Time Complexity: O(Logn)
Programming Paradigm: Divide & Conquer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ite a C++ program that counts the number of occurrences of each letter of an input string, case-insensitive. Here's one way you could do it: ```cpp #include <iostream> #include <string> #include <map> #include <cctype> int main() { std::string input; std::getline(std::cin, input); // read input from user std::map<char, int> letterCounts; // map to store letter counts for (char c : input) { if (std::isalpha(c)) { // ignore non-letter characters char lowercase = std::tolower(c); // convert to lowercase letterCounts[lowercase]++; // increment count of this letter } } // print out the results for (auto pair : letterCounts) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; } ``` This program starts by reading in a string from the user using `std::getline()`. It then creates a `std::map<char, int>` called `letterCounts` to store the counts of each letter. The `for` loop iterates over each character in the input string, checking if it is a letter using `std::isalpha()`. If it is a letter, it converts it to lowercase using `std::tolower()` and increments the count of that letter in the `letterCounts` map. Finally, the program prints out the results by iterating over the `letterCounts` map and printing out each letter and its count. Note that the `auto` keyword is used to simplify the loop syntax - `pair` is automatically inferred to be a `std::pair<char, int>`. This program should work for any input string, regardless of its length or the specific letters it contains.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值