Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

March 19, 2012

Sieve of Eratosthenes - Prime Numbers


Question: Provide a simple algorithm that prints all prime numbers smaller than given number N.


The sieve of Eratosthenes is a simple algorithm for finding all prime numbers up to any given number N. It does so by iteratively marking multiples of prime numbers starting from 2.


Time Complexity: O(n)



int soe(int n, int *array)
{
    int x, y;
    
    for (x = 2; x <= (n/2); x++)
    {
        if (array[x] == 1) continue;


        for (y = 2 * x; y < n; y += x) {
            array[y] = 1;
        }
    }
}


Array is memset to zero before calling this function. Once above function has processed the array, array index between 2 and N with value zero are prime numbers.


Prime numbers less than 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Prime numbers less than 1000: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997

March 16, 2012

Reverse the order of words in a string


Q: Write a C program to reverse the order of words in a string. For simplicity, space has been chosen as word delimiter and punctuation is considered part of the word.


Example: 
Input: Can you reverse the words in this string.
Output: string. this in words the reverse you Can


Time Complexity: O(log(n))


void reverse_word(char *word, int length)
{
    int start;
    int end = length - 1;
    char temp;


    for (start = 0; start < end; start++, end--) {
        temp = word[start];
        word[start] = word[end];
        word[end] = temp;
    }
}


void reverse_string(char *string)
{
    int index;
    int string_length = strlen(string);
    int word_start_index = 0;
    int word_length = 0;


    // Reverse the original string
    reverse_word(string, string_length);


    for (index = 0; index < string_length; index++) {
        // found word delimiter
        if (string[index] == ' ') {
            reverse_word(&string[word_start_index], word_length);
            word_length = 0;
            word_start_index = index + 1;
        } else {
            word_length++;
        }
    }


    // Reverse the last word
    if (word_length) {
        reverse_word(&string[word_start_index], word_length);
    }
}

July 28, 2011

Maximum Sum Sub-array (Kadane's Algorithm)

Question: Given an array of integers (both positive and negative), find the sub-array with maximum sum. Example: An array -3, 2, -4, 4, -1, 3, 2, -5, 4 should return the sum as eight, sub-array start index is 3 and sub-array end index is 6.


Time Complexity: O(n)


int
maximum_sum_subarray(int *array, int length, int *max_sum, int *max_start, int *max_end)
{
    int cur_sum = 0, cur_start = 0, cur_end = 0;


    for (cur_end = 0; cur_end < length; cur_end++) {
        cur_sum += array[cur_end];
        if (cur_sum > *max_sum) {
            *max_sum = cur_sum;
            *max_start = cur_start;
            *max_end = cur_end;
        }


        if (cur_sum < 0) {
            cur_sum = 0;
            cur_start = cur_end + 1;
        }
    }
}

June 17, 2011

Missing Number

Question: Given an array of integers of size n. This array has integer values starting from x, incrementing by 1. The final array element is n+x (instead of n + x - 1) i.e. it is missing one integer in between. Write a function to identify this missing number.


Example: array[5] = { 2,3,4,6,7 }; missing number is 5.


Time Complexity: O(log n)



int missing_number(int *a, int len)
{
    int mid = len / 2;


    if (len == 1) {
        printf ("missing number is %d\n", a[0] + 1);
        return (a[0] + 1);
    }


    if ((a[0] + mid) == a[mid])
    {
        // first half of array is not missing a number
        missing_number(&a[mid], len - mid);
    } else {
        // second half of array is not missing a number
        missing_number(a, mid);
    }
}