Searching Algorithm
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. #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. }
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. }