Question: Given a sorted array of n elements, possibly with duplicates, find the number of occurrences of an element.

    Input: 4, 4, 8, 8, 8, 15, 16, 23, 23, 42. Find 8
    Output: Count = 3

    The most basic methodology to solve this problem is linear search. Just scan the array and count the number of occurrences. But this takes time O(n).

    int linearSearchCount(int arr[], int n, int data)
    {
    	int count = 0;
    	int i = 0;
    
    	for(i=0;i<n;i++)
    	{
    		if(arr[i] == data)
    			count++;
    	}
    
    	return count;
    }
    

    We can improve the time complexity by using binary search. The steps involved in this method are:-

    • Do a binary search and search for the data in the array. Let us assume that the position is K.
    • Now traverse left from the position K, until we get a different element. Keep a count of the number of elements. Let it be leftCount.
    • Now traverse right from the position K, until we get a different element. Keep a count of the number of elements. Let it be rightCount.
    • Total number of occurrences = leftCount + 1 + rightCount
    #include<stdio.h>
    
    int binarySearchIterative(int arr[], int size, int data)
    {
    	int low = 0;
    	int high = size-1;
    	int mid;
    	while(low<=high)
    	{
    		mid = low + (high-low)/2;
    		if(arr[mid] == data)
    			return mid;
    		else
    			if(arr[mid] < data)
    				low = mid + 1;
    			else
    				high = mid - 1;
    	}
    	return -1;
    }
    
    int binarySearchCount(int arr[], int len, int data)
    {
    	// get the index of element
    	int indexData = binarySearchIterative(arr, len, data);
    
    	// get the leftCount
    	int left = indexData - 1;
    	int leftCount = 0;
    	while(left>-1 && arr[left] == data)
    	{
    		leftCount++;
    		left--;
    	}
    
    	// get the rightCount
    	int right = indexData + 1;
    	int rightCount = 0;
    	while(right < len && arr[right] == data)
    	{
    		rightCount++;
    		right++;
    	}
    
    	return (leftCount + 1 + rightCount);
    }
    
    int main(void)
    {
    	int arr[] = {4, 4, 8, 8, 8, 15, 15, 16, 23, 42, 42};
    
    	int count = binarySearchCount(arr, 11, 8);
    
    	printf("count = %d", count);
    
    	return 0;
    }
    

    Time Complexity:- O(log n + S), where S is the number of occurrences

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a sorted array A of n elements, possibly with duplicates, find the index of the last occurrence of a number in O(log n) time. Input: 4, 4, 8, 8, 8, 15, 16, 23, 23, 42. Find 8 Output: Index = 4 (0 based indexing) This problem is very much similar to the binary search problem. We can apply the same methodology with just an added condition. For a number to be the last occurrence in the array, the number to the right of it must be larger. We …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a sorted array A of n elements, possibly with duplicates, find the index of the first occurrence of a number in O(log n) time. Input: 4, 4, 8, 8, 8, 15, 16, 23, 23, 42. Find 8 Output: Index = 2 (0 based indexing) This problem is very much similar to the binary search problem. We can apply the same methodology with just an added condition. For a number to be the first occurrence in the array, the number to the left of it must be smaller. We …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a matrix that is sorted in row wise and column wise, find a number if present in the most efficient way. Input: Find if 54 is present in matrix. 4 8 15 16 23 6 9 20 21 44 8 11 24 26 49 9 13 25 27 54 10 17 29 30 66 Output: Present Method 1(Brute force):- Here, we use two loops search the element in entire matrix. Its a linear search so its not an efficient way to solve this problem. //K – the element …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Misc

    Find the sum of digits in a given number.

    by nikoo28
    2 minutes read

    Question: Find sum of digits in a given number. Input: 65536 Output: 25 Method 1 (Iterative): In this code sample by using modulus operator(%), we extract the individual digits of given number then we add them together #include<stdio.h> int findSum(int num) { int sum = 0, remainder; while(num != 0) { // This gives us the last digit remainder = num % 10; // Add the digit to the sum sum = sum + remainder; // This divides the number by 10 // and removes the last digit num = …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a sorted Linked List. Write a program to remove all the duplicates. Input: 4 -> 8 -> 8 -> 8 -> 15 -> 16 -> 23 -> 23 -> 42. Output: 4 -> 8 -> 15 -> 16 -> 23 -> 42 In a sorted Linked List, all the node that are duplicate will be together. To remove duplicates we need to traverse the linked list and if two adjacent nodes have same value, remove one of them. If adjacent node have different value then move forward. // …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Write a program to check if 2 linked lists are identical? Method 1(Iterative):- To Determine if both Linked lists are identical, we need to traverse both lists simultaneously, and while traversing we need to compare data of each node. Implementation //returns 1 if both lists are identical or returns 0 int areIdenticalLinkedList(struct node * list1, struct node * list2) { while(1) { // return 1 if both are NULL if(list1 == NULL && list2 == NULL) return 1; // return 0 if one of them is NULL if(list1 == …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More