Given an unsorted array of size n containing the integers 1 through n in random order with one element randomly replaced by 0, determine the missing element most efficiently.
Here was the solution I came up with:
#include <stdio.h> #include <stdlib.h> #include <time.h> void InitArray (int *TheArray, int n) { // Fill the array with values 1..n for (int i = 0; i < n; i++) TheArray[i] = i+1; // Remove a value at random. TheArray[rand() % n] = 0; } // From: http://codesam.blogspot.com/2011/04/how-to-shuffle-array-or-fisher-yates.html // With reasoning from CodingHorror: http://www.codinghorror.com/blog/2007/12/the-dangerof-naivete.html // Original Theory: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle void KnuthShuffle(int *orgArray, int arraySize) { if (arraySize == 0 || arraySize == 1) return; int i; int index, temp; for (i = arraySize - 1; i > 0; i--) { index = rand() % (i+1); temp = orgArray[index]; orgArray[index] = orgArray[i]; orgArray[i] = temp; } } void PrintArray (int *TheArray, int n) { printf ("TheArray[%d]: {%d", n, TheArray[0]); for (int i = 1; i < n; i++) printf (", %d", TheArray[i]); printf ("}\n"); } int main(int argc, char *argv[]) { // Sum of integers between 1 and n = n(n+1)/2 int n = 9; int TheArray[n]; int TotalValue = 0; // Check for arguement passed to the program to define n. if (argc == 2) n = atoi(argv[1]); // Seed the randomizer with the current time. srand(time(NULL)); InitArray (TheArray, n); KnuthShuffle (TheArray, n); PrintArray (TheArray, n); // And we will avoid a function here.. for (int x = 0; x < n; x++) TotalValue += TheArray[x]; printf ("The missing number is: %d!\n", ((n*(n+1))/2) - TotalValue); return 0; }
This code accepts input from the command line to specify n. If I see a way to optimize it more I will do so at a later time.
EDIT:
Ack, I just realized that I defined TheArray with the default value BEFORE checking the command line arguments. It wasn't too long before I was grabbing data from outside the array's boundaries. This is what I get for adding the command line functionality at the last second.
A simple fix would be to replace the beginning of the main function with:
#include <stdio.h> int main(int argc, char *argv[]) { // Sum of integers between 1 and n = n(n+1)/2 // Check for arguement passed to the program to define n. int n; if (argc == 2) n = atoi(argv[1]); else n = 9; int TheArray[n]; int TotalValue = 0; . . .
That works much better.
No comments:
Post a Comment