Check this
*int isconsecutive(int a[], int n) {*
* if (n < 1) {*
* return 0;*
* }*
* int max = a[0], min = a[0];*
* int i = 0;*
*
*
* int *hash = (int*) calloc(n, sizeof (int));*
*
*
* //find min and max from the array*
* for (i = 1; i < n; i++) {*
* if (a[i] < min)*
* min = a[i];*
* else if (a[i] > max)*
* max = a[i];*
* }*
*
*
* if (max - min + 1 != n)*
* return 0;*
*
*
* for (i = 0; i < n; i++) {*
* if (hash[a[i] - min + 1] == 1)*
* return 0;*
* hash[a[i] - min + 1] = 1;*
* }*
* return 1;*
*
*
*}*
*
*
*int main(int argc, char** argv) {*
*
*
* int a[] = {-1, 0,1,2, 4, 3, 5};*
* int n = sizeof (a) / sizeof (a[0]);*
* printf("%d", isconsecutive(a, n));*
*
*
* return (EXIT_SUCCESS);*
*}*
On Sat, Jun 25, 2011 at 1:14 AM, ross <[email protected]> wrote:
> Given an array, A, find if all elements in the sorted version of A are
> consecutive in less than O(nlogn).
> eg: A: 5 4 1 2 3 on sorting 1 2 3 4 5 all elements are consecutive --
> true
> A: 1 9 2 22 on sorting 1 2 9 22 all elements are NOT consecutive -
> false
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.