0% found this document useful (0 votes)
13 views

Searching Algorithm

Uploaded by

20bec044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Searching Algorithm

Uploaded by

20bec044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Searching algorithm

1) Majority Element
Approach: Insert elements in BST one by one and if an element is
already present then increment the count of the node. At any stage, if
the count of a node becomes more than n/2 then return.

1. // C++ program to find Majority element in an array


2.
3. #include <bits/stdc++.h>
4. using namespace std;
5.
6. // Function to find Majority element in an array
7.
8. void findMajority(int arr[], int n)
9. {
10. int maxCount = 0;
11. int index = -1; // sentinels
12. for (int i = 0; i < n; i++) {
13. int count = 0;
14. for (int j = 0; j < n; j++) {
15. if (arr[i] == arr[j])
16. count++;
17. }
18. // update maxCount if count of current element
is greater
19. if (count > maxCount) {
20. maxCount = count;
21. index = i;
22. }
23. }
24. // if maxCount is greater than n/2 return the coresp
element
25. if (maxCount > n / 2)
26. cout << arr[index] << endl;
27.
28. else
29. cout << "No Majority Element" << endl;
30. }
31. // Driver code
32.

2) Square root of an integer


The idea is to find the largest integer i whose square is less than or equal to
the given number. The idea is to use Binary Search to solve the problem.
The values of i * i is monotonically increasing, so the problem can be solved
using binary search.

1. #include <iostream>
2.
3. using namespace std;
4. int floorSqrt(int x)
5. {
6. // Base cases
7. if (x == 0 || x == 1)
8. return x;
9.
10. // Do Binary Search for floor(sqrt(x))
11. int start = 1, end = x/2, ans;
12. while (start <= end) {
13. int mid = (start + end) / 2;
14.
15. // If x is a perfect square
16. int sqr = mid * mid;
17. if (sqr == x)
18. return mid;
19.
20. // Since we need floor, we update answer when
21. // mid*mid is smaller than x, and move closer to
22. // sqrt(x)
23.
24. /*
25. if(mid*mid<=x)
26. {
27. start = mid+1;
28. ans = mid;
29. }
30. Here basically if we multiply mid with itsel
so
31. there will be integer overflow which will throw
32. tle for larger input so to overcome this
33. situation we can use long or we can just divide
34. the number by mid which is same as checking
35. mid*mid < x
36. */
37. if (sqr <= x) {
38. start = mid + 1;
39. ans = mid;
40. }
41. else // If mid*mid is greater than x
42. end = mid - 1;
43. }
44. return ans;
45. }
46.
47. // Driver program
48. int main()
49. {
50. int x = 20221;
51. cout << floorSqrt(x) << endl;
52. return 0;
53. }

3) Missing Number in array


Approach: The approach remains the same but there can be an overflow if n
is large. In order to avoid integer overflow, pick one number from the known
numbers and subtract that one number from the given numbers. This way
there won't be any Integer Overflow.

1. class Solution{
2. public:
3. int MissingNumber(vector<int>& array, int n) {
4. long long sum=n*(n+1)/2;
5. for(auto n: array)
6. sum-=n;
7. return sum;
8. } };
4) Find a peak element
Efficient Approach: Divide and Conquer can be used to find a peak in
O(Logn) time. The idea is based on the technique of Binary Search to check
if the middle element is the peak element or not. If the middle element is not
the peak element, then check if the element on the right side is greater than
the middle element then there is always a peak element on the right side. If
the element on the left side is greater than the middle element then there is
always a peak element on the left side. Form a recursion and the peak
element can be found in log n time.

1. #include <bits/stdc++.h>
2. using namespace std;
3.
4. // A binary search based function that returns index
of a peak element
5. int findPeakUtil(int arr[], int low,
6. int high, int n)
7. {
8. // Find index of middle element
9. // low + (high - low) / 2
10. int mid = low + (high - low) / 2;
11.
12. // Compare middle element with its neighbours (if
neighbours exist)
13. if ((mid == 0 || arr[mid - 1] <= arr[mid]) &&
14. (mid == n - 1 || arr[mid + 1] <= arr[mid]))
15. return mid;
16.
17. /* If middle element is not peak and its left
neighbour is greater than it, then left half must
have a peak element */
18. else if (mid > 0 && arr[mid - 1] > arr[mid])
19. return findPeakUtil(arr, low, (mid - 1), n);
20.
21. /* If middle element is not peak and its
22. right neighbour is greater than it, then right
half must have a peak element*/
23. else
24. return findPeakUtil(
25. arr, (mid + 1), high, n);
26. }
27.
28. // A wrapper over recursive function findPeakUtil()
29.
30. int findPeak(int arr[], int n)
31. {
32. return findPeakUtil(arr, 0, n - 1, n);
33. }
34.
35. // Driver Code
36. int main()
37. {
38. int arr[] = { 1, 3, 20, 4, 1, 0 };
39. int n = sizeof(arr) / sizeof(arr[0]);
40. cout << "Index of a peak point is "
41. << findPeak(arr, n);
42. return 0;
43. }

You might also like