Tuesday, October 28, 2014

C Subarray PRo



Find the Odd Element given an Array with only two Different Element

#include 
#include 
void printodd(int array[], int size)
{
    int xor2 = array[0];
    int set;
    int i;
    int n = size - 2;
    int x = 0, y = 0;
    for (i = 1; i < size; i++)
        xor2 = xor2 ^ array[i]; 
    set = xor2 & ~(xor2 - 1);
    for (i = 0; i < size; i++)
    {
        
        if (array[i] & set)
        x = x ^ array[i];
        else
        y = y ^ array[i];
    }
    printf("\n The odd elements are %d & %d ", x, y);
}
 
int main()
{
    int array[] = {10, 3, 2, 10, 2, 8, 8, 7};
    int arr_size = sizeof(array) / sizeof(array[0]);
    printodd(array, arr_size);
    getchar();
    return 0;
    getch();
}

Output
 
            The odd elements are 7 & 3

Find the Sum of Contiguous Subarray within a 1 – D Array of Numbers which has the Largest Sum

         #include 
#include 
int maxSubArraySum(int a[], int size, int *begin, int *end)
{
    int max_so_far = 0, max_end = 0;
    int i, current_index = 0;
     for (i = 0; i < size; i++)
    {
        max_end = max_end + a[i];
        if (max_end <= 0)
        {
            max_end = 0;
            current_index = i + 1;
        }
        else if (max_so_far < max_end)
        {
            max_so_far = max_end;
            *begin = current_index;
            *end = i;
        }
   }
   return max_so_far;
}
 int main()
{
    int arr[] = {10, -2, 15, 9, -8, 12, 20, -5};
    int start = 0, end = 0;
    int size = sizeof(arr) / sizeof(arr[0]);
    printf(" The max sum is %d", maxSubArraySum(arr, size, &start, &end));
    printf(" The begin and End are %d & %d", start, end);
    getchar();
    return 0;
     getch();
   }
 
Output
            
            The max sum is 56 The begin and End are 0 & 6


Find the Median of the Elements after Merging these 2 Sorted Arrays with Same Size
            #include 
#include 
int getMedian(int array1[], int array2[], int n)
{
    int i = 0;  
    int j = 0;
    int count;
    int m1 = -1, m2 = -1;
   for (count = 0; count <= n; count++)
    {
        if (i == n)
        {
            m1 = m2;
            m2 = array2[0];
            break;
        }
        else if (j == n)
        {
            m1 = m2;
            m2 = array1[0];
            break;
        }
        if (array1[i] < array2[j])
        {
            m1 = m2;  
            m2 = array1[i];
            i++;
        }
        else
        {
            m1 = m2;  
            m2 = array2[j];
            j++;
        }
    }
    return (m1 + m2)/2;
}
int main()
{
    int array1[] = {20, 25, 35, 30, 38};
    int array2[] = {22, 53, 65, 72, 45};
    int n1 = sizeof(array1) / sizeof(array1[0]);
    int n2 = sizeof(array2) / sizeof(array2[0]);
    if (n1 == n2)
        printf("Median is %d", getMedian(array1, array2, n1));
    else
        printf("not possible to findout");
    getchar();
    return 0;
    getch();
}

Output
 
            Median is 34

No comments:

Post a Comment