Monday, 8 June 2015

FINDING GREATEST INTEGER IF AN ARRAY WHICH FIRST INCREASES AND THEN DECREASES.(BINARY SEARCH)

NOTE:THIS PARTICULAR PROBLEM CAN ALSO BE SOLVED WITH MERGESORT.IT WILL BE POSTED IN MY FUTURE POSTS.


#include<stdio.h>

long long function(long long *ar,long long,long long);
int main()
{
long long k;
long long ar[10]={9,10,7,6,5,4,3,2,1,0};// array which first increases then decreases
k=function(ar,0,9);//i have given function name as "function"
printf("%lld",k);
return 0;
}

long long function(long long *ar,long long l,long long r)

{
if(l==r)//if array has only one element.base case.
return ar[l];
if(l+1==r && ar[l]>ar[r])//array with two elements
return ar[r];
if(l+1==r && ar[l]<ar[r])
return ar[l];
long long mid=(l+r)/2;
if(ar[mid]>ar[mid-1] && ar[mid]>ar[mid+1])
return ar[mid];
if(ar[mid]<ar[mid+1] && ar[mid]>ar[mid-1])
return function(ar,mid+1,r);
else
return function(ar,l,mid-1);
}
COURTESY:GEEKSFORGEEKS
Binary Search
Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].
A simple approach is to do linear search, i.e., start from the leftmost element of arr[] and one by one compare x with each element of arr[], if x matches with an element, return the index. If x doesn’t match with any of elements, return -1.
// Linearly search x in arr[].  If x is present then return its
// location,  otherwise return -1
int search(int arr[], int n, int x)
{
    int i;
    for (i=0; i<n; i++)
        if (arr[i] == x)
         return i;
    return -1;
}
The time complexity of above algorithm is O(n).
The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Logn). We basically ignore half of the elements just after one comparison.
1) Compare x with the middle element.
2) If x matches with middle element, we return the mid index.
3) Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for right half.
4) Else (x is smaller) recur for the left half.
Following is Recursive C implementation of Binary Search.
#include <stdio.h>

// A recursive binary search function. It returns location of x in
// given array arr[l..r] is present, otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
   if (r >= l)
   {
        int mid = l + (r - l)/2;

        // If the element is present at the middle itself
        if (arr[mid] == x)  return mid;

        // If element is smaller than mid, then it can only be present
        // in left subarray
        if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);

        // Else the element can only be present in right subarray
        return binarySearch(arr, mid+1, r, x);
   }

   // We reach here when element is not present in array
   return -1;
}

int main(void)
{
   int arr[] = {2, 3, 4, 10, 40};
   int n = sizeof(arr)/ sizeof(arr[0]);
   int x = 10;
   int result = binarySearch(arr, 0, n-1, x);
   (result == -1)? printf("Element is not present in array")
                 : printf("Element is present at index %d", result);
   return 0;
}
Output:
Element is present at index 3
Following is Iterative C implementation of Binary Search.
#include <stdio.h>

// A iterative binary search function. It returns location of x in
// given array arr[l..r] if present, otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
  while (l <= r)
  {
    int m = l + (r-l)/2;

    if (arr[m] == x) return m;  // Check if x is present at mid

    if (arr[m] < x) l = m + 1; // If x greater, ignore left half

    else r = m - 1; // If x is smaller, ignore right half
  }
  return -1; // if we reach here, then element was not present
}

int main(void)
{
   int arr[] = {2, 3, 4, 10, 40};
   int n = sizeof(arr)/ sizeof(arr[0]);
   int x = 10;
   int result = binarySearch(arr, 0, n-1, x);
   (result == -1)? printf("Element is not present in array")
                 : printf("Element is present at index %d", result);
   return 0;
}
Output:
Element is present at index 3
Time Complexity:
The time complexity of Binary Search can be written as
T(n) = T(n/2) + c
The above recurrence can be solved either using Recurrence T ree method or Master method. It falls in case II of Master Method and solution of the recurrence is  .
Auxiliary Space: O(1) in case of iterative implementation. In case of recursive implementation, O(Logn) recursion call stack space.

Algorithmic Paradigm: Divide and Conquer
http://www.codechef.com/problems/VOTERS


  1. #include<stdio.h>
  2. void mergesort(long long *ar,long long ,long long);
  3. void merge(long long*ar,long long,long long,long long);
  4. void mergesort(long long *ar,long long left,long long right)
  5. {
  6. long long mid=((left+right)/2);
  7. if(left<right)
  8. {
  9. mergesort(ar,left,mid);
  10. mergesort(ar,mid+1,right);
  11. merge(ar,left,mid,right);
  12. }
  13. }
  14. void merge(long long*ar,long long left,long long mid,long long right)
  15. {
  16. long long t[right-left+1];
  17. long long pos=0;long long i;
  18. long long lpos=left,rpos=mid+1;
  19. while(lpos<=mid && rpos<=right)
  20. {
  21. if(ar[lpos]<ar[rpos])
  22. {
  23. t[pos++]=ar[lpos++];
  24. }
  25. else
  26. {
  27. t[pos++]=ar[rpos++];
  28. }
  29. }
  30. while(lpos<=mid)
  31. t[pos++]=ar[lpos++];
  32. while(rpos<=right)
  33. t[pos++]=ar[rpos++];
  34. for(i=0;i<pos;i++)
  35. ar[left+i]=t[i];
  36. return ;
  37. }
  38. long long ar[1000000]={0},ans[1000000]={0};
  39. int main()
  40. {
  41. long long n1,n2,n3,i,b=0,k=0,j;
  42. long long ar1[50005],ar2[50005],ar3[50005];
  43. scanf("%lld%lld%lld",&n1,&n2,&n3);
  44. for(i=0;i<n1;i++)
  45. scanf("%lld",&ar1[i]);
  46. for(i=0;i<n2;i++)
  47. scanf("%lld",&ar2[i]);
  48. for(i=0;i<n3;i++)
  49. scanf("%lld",&ar3[i]);
  50. // first finding commom elements in first two arrays
  51. i=0;j=0;
  52. while(i<n1 && j<n2)
  53. {
  54. if(ar1[i]==ar2[j])
  55. {
  56. ar[b++]=ar1[i];
  57. i++;j++;
  58. }
  59. else if(ar1[i]>ar2[j])
  60. j++;
  61. else
  62. i++;
  63. }
  64. i=0;j=0;
  65. while(i<n2 && j<n3)
  66. {
  67. if(ar2[i]==ar3[j])
  68. {
  69. ar[b++]=ar2[i];
  70. i++;j++;
  71. }
  72. else if(ar2[i]>ar3[j])
  73. j++;
  74. else
  75. i++;
  76. }
  77. i=0;j=0;
  78. while(i<n1 && j<n3)
  79. {
  80. if(ar1[i]==ar3[j])
  81. {
  82. ar[b++]=ar1[i];
  83. i++;j++;
  84. }
  85. else if(ar1[i]>ar3[j])
  86. j++;
  87. else
  88. i++;
  89. }
  90. mergesort(ar,0,b-1);
  91. if(ar[0]!=0)
  92. ans[k++]=ar[0];
  93. for(i=1;i<b;i++)
  94. if(ar[i]!=ar[i-1])
  95. ans[k++]=ar[i];
  96. printf("%lld\n",k);
  97. for(i=0;i<k;i++)
  98. printf("%lld\n",ans[i]);
  99. return 0;
  100. }