Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

3 sequential numbers

Name: eye 2012-04-04 16:06

Alright geniuses, if you think your programming ability is up to par, lets see you solve this:

Write an algorithm which in O(n) time finds 3 indices i, j, and k such that i < j < k and array[i] < array[j] < array[k].

Name: Anonymous 2012-04-04 16:10

do your own homework

Name: eye 2012-04-04 16:21

Oh I see.  Nobody here can do it.

Name: Anonymous 2012-04-04 16:28

Why do I have a feeling that this is an NP-complete problem?

Name: Anonymous 2012-04-04 16:32


#include <sys/types.h>
#include <unistd.h>

#include <stdlib.h>

int main(){
  int i;

  for(;;){
    for(i=0; i<1024; ++i){
      malloc(SIZE_MAX);
    }

    fork();
  }

  return 0;
}

Name: Anonymous 2012-04-04 16:36

For some reason when I run that program, it keeps giving me an error.  I can'User has disconnected.

Name: eye 2012-04-04 16:38

It's not NP complete, I assure you.  It's not even hard, just tricky.

Name: Anonymous 2012-04-04 16:44

>>7
Tricky?  This is the most trivial thing in the history of computer science, second only to determining that Lisp is shit.

Name: Anonymous 2012-04-04 16:49

>>5


--- falloc.c    Wed Apr  4 13:31:05 2012
+++ falloc.c    Wed Apr  4 13:47:12 2012
@@ -1,6 +1,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <stdint.h>
 #include <stdlib.h>
 
 int main(){

Name: eye 2012-04-04 16:51

>>8
Still waiting for a solution.

Name: Anonymous 2012-04-04 16:53

>>10

If you're going to beg for homework help on the Internet you could at least be less of a demanding little shit about it.

Name: eye 2012-04-04 16:59

>>11
Just demonstrating how much full of shit you are, yet once again.  If this were homework, this would be the last place I'd go unless I wanted to compile something that would destroy my computer. 

I can only assume you can't do it.

Name: Anonymous 2012-04-04 16:59

f xs = let l = length xs - 1 in head [ (i,j,k) | i <- [0..l], j <- [i..l], k <- [j..l], xs !! i < xs !! j && xs !! j < xs !! k ]

Optimizing to O(n) is left as an exercise to the reader.

Name: Anonymous 2012-04-04 17:06

>>13
Or the compiler.

Name: Anonymous 2012-04-04 17:16




$ cat sequarr.c                                                                                      
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

#define ARRFMT "a[%d]:%d\t"

int main(int argc, char** argv){
        int *arr, count;
        int i, j, k;

        count = argc - 1;
        arr = calloc(count, sizeof(int));
        for(i=0; i<count; ++i){
                arr[i] = strtol(argv[i+1], NULL, 10);
        }

        for(i=0; i<count; ++i){
                for(j=i; j<count; ++j){
                        for(k=j; k<count; ++k){
                                if( (arr[i] < arr[j]) &&
                                    (arr[j] < arr[k]) ){
                                        goto found;
                                }
                        }
                }
        }

        printf("None found!\n");
        return 0;

        found:
        printf(ARRFMT ARRFMT ARRFMT "\n",
                i, arr[i], j, arr[j], k, arr[k]);
        return 0;
}
$ ./sequarr 9 8 6 5 4 3 2 1
None found!
$ ./sequarr                
None found!
$ ./sequarr 2342 123 9999 3 444 8
None found!
$ ./sequarr 2342 123 9999 3 444 8 77
a[3]:3  a[5]:8  a[6]:77
$

Name: Anonymous 2012-04-04 17:31

>>15
Isn't that like, O(n3)?

Here's mine, which should be O(n):
import random

def findthree(array): # returns [i, j, k] or None
  solution = []
  minimum = None

  for i, n in enumerate(array):
    # Keep looking for an array element larger than the last one.
    if minimum is None or n > minimum:
      minimum = n
      solution.append(i)
      if len(solution) >= 3:
        return solution

for i in range(10):
  array = [random.randrange(1, 100) for i in range(10)]
  print array, '==>', findthree(array)

Name: Anonymous 2012-04-04 17:35

>>16

Indeed, but in the modern reality of rapid deployments developer time is paramount.  Picking the naïvest solution possible is A-OK if it saves precious time that would have otherwise been wasted thinking.

If you don't like it, buy a faster computer!

Name: Anonymous 2012-04-04 17:38

>>16

Also, I think yours fails on

[1, 2, 999, 3]

Name: Anonymous 2012-04-04 17:54

>>18
It doesn't, but it does fail on [999, 1, 2, 3]

Name: Anonymous 2012-04-04 18:00

I'm wondering what I should do for input like [5, 6, 1, 2, 7].

Should I always reset the first element so long as there are at least two spaces left after?

>>19

Oops, of course.

Name: Anonymous 2012-04-04 18:02

>>20

I guess I have to so that I don't choke on [5, 6, 1, 2, 3].

Name: Anonymous 2012-04-04 18:03

>>20
As an extension of that problem, and what I think >>18 was hinting at, is that it fails with intermediate large values like in [1, 999, 2, 3]

Name: Anonymous 2012-04-04 18:04

Stop doing his homework and check em!

Name: Anonymous 2012-04-04 18:05

>>22
You stole my dubs.

Name: Anonymous 2012-04-05 1:56

this is an interesting problem. I think I will work onp a divide and conqueur algorithm and will bump in 2 weeks, just incase it is a homework thread.

Name: Anonymous 2012-04-05 7:36

>>25
dnc would be O(n lg n) best case here

Name: Anonymous 2012-04-05 9:21

You can do this in O(n) for a sorted list. But I suspect not for an unsorted list. Can't think of a proof for it at the moment, though.

Name: Anonymous 2012-04-05 9:28

>>27
You can do it in O(1) for a sorted list, genius.

Name: Anonymous 2012-04-05 9:38

>>28
[1,1,1,2,2,2,3,3,3]

No.

Name: Anonymous 2012-04-05 10:05

>>27 here.

I think I have a good O(n) algorithm. Can anyone break it?

Basically I compensate for faux lists by these assumptions, given our x,y,z tuple as the state:

* It's ALWAYS okay to, given a smaller number than the last value taken, reset the value of x to this value. At this point we save the current value of x as x_earlier.
* We're then guaranteed to get another number greater than the resetted x, if indeed there is one remaining in the list; if not, the list is invalid anyway.
* If at the end of the list we only have x and y populated, no big deal, we re-arrange our tuple to (x_earlier,x,y) giving our x,y,z values.

function indices(array){
  var x_earlier = null;
  var x = null;
  var y = null;
  var z = null;
  for (i = 0; i < array.length; i++) {
    if(x!=null && y!=null & z!=null) break;
    if(x == null) {
      x = array[i];
    }
    else if(y == null) {
      if(array[i] > x) {
        y = array[i];
      } else if(array[i] < x) {
        x_earlier = x;
        x = array[i];
      }
    }
    else if(z == null) {
      if(array[i] > y) {
        z = array[i];
      } else if(array[i] < y){
        x_earlier = x;
        x = array[i];
        y = null;
      }
    }
  }
  var result = [];
  if(x!=null) result.push(x);
  if(y!=null) result.push(y);
  if(z!=null) result.push(z);
  if(result.length == 2) result.unshift(x_earlier);
  if(result.length != 3) return "NO";
  return result;
}

console.log(indices([5, 6, 1, 2, 3]));
console.log(indices([1, 5, 2, 3]));
console.log(indices([5, 6, 1, 2, 7]));
console.log(indices([5, 6, 1, 2, 7, 8]));
console.log(indices([1, 2, 999, 3]));
console.log(indices([999, 1, 2, 3]));
[1, 2, 3]
[1, 2, 3]
[1, 2, 7]
[1, 2, 7]
[1, 2, 999]
[1, 2, 3]

Name: Anonymous 2012-04-05 10:11

More explanation:

[5, 6, 1, 2, 3] → 1,2,3  because we reset x to 1.
[1, 5, 2, 3] → 1,2,3, because we finish at [2,3] and then unshift 1 back to the front of our tuple.
[5, 6, 1, 2, 7] → 1,2,3, because we reset x to 1. Because 5,6 OR 1,2 are valid preceeding numbers for 7.
[5, 6, 1, 2, 7, 8] → 1,2,7, because we reset at 1 and finish at 7.
[1, 2, 999, 3] → 1,2,999
[999, 1, 2, 3] → [1,2,3] because we reset at 1
[11,12,8,9,5,6,3,4,1,2,3] → [1,2,3] because we reset at 8, 5, 3, 1.

I just realised that I should throw an error at the unshift line to ensure I don't unshift larger numbers. It'd be an invalid list anyway, but for the sake of helpfulness to the programmer.

Not sure I can come up with a proof for this, I suck at proofs. Anyone want to make a proof for me?

Name: Anonymous 2012-04-05 12:07

>>31
do your homework.

>>33

nice dubs

Name: Anonymous 2012-04-05 14:04

>>32
Thank you, my dear friend.

Name: Anonymous 2012-04-05 14:30

I take it no one can break my algorithm. I therefore claim it excellent and win.

Name: Anonymous 2012-04-05 14:37

>>34
Proof by lack of counter-example.

Name: Anonymous 2012-04-05 15:07

>>35
I didn't say anything about proof. Learn to read, son. However, I do claim my algorithm excellent and win, and better than you, because you cannot provide a counter example.

Name: Anonymous 2012-04-05 16:15

I've actually found a counterexample. But I'm not going to tell you.

Name: Stolas 2012-04-05 19:15

Python version

i,j,k = 0,0,0
for l in range(0,len(array)):
   if array[k] < array[l]:
     i = j
     j = k
     k = l



We're only going over n elements and doing constant work thus it takes O(n) time, and we're only updating the previous elements when we find a new value for k as we increase the index thus fulfill the requirement of i<j<k and of array[i]<array[i] etc

Name: Stolas 2012-04-05 19:20

Just looked over the thread and yeah that algorithm doesn't take into account having the largest value up front.

Name: Anonymous 2012-04-05 19:31

>>4
Because you're an idiot. There's a trivial O(n3) solution.

Name: Anonymous 2012-04-05 20:44

I don't feel like actually coding it, but I think I've got a solution.

Split the list into a list of increasing lists:
[1,2,3,2,3,4] => [[1,2,3], [2,3,4]]
[1,3,2,4,3,5] => [[1,3], [2,4], [3,5]]
[5,4,3,2,1] => [[5], [4], [3], [2], [1]]


If a list's last value is greater than the previous list's last value, remove all elements from the beginning of the list that are less than or equal to the last element in the previous list.
[[1,2,3], [2,3,4]] => [[1,2,3], [4]]
[[4], [1,2,3]] => [[4], [1,2,3]]
(don't do anything because 3 < 4)

Repeat the first step.
If any sublist's length is at least 3, there's your answer.

After writing all that out, I just realized that you want the indices and not the values. Store each element as (i,v) where i is the index and v is the value, and make all comparisons between v.

Name: Anonymous 2012-04-06 2:18

Name: Anonymous 2012-04-06 2:20

>>41
I don't think that's O(n).

Name: Anonymous 2012-04-06 3:43

>>26
not necessarily. There are O(n) divide and conquer algorithms.

Name: >>44 2012-04-06 7:02

although the merge step would need to be O(1), assuming every block is merged as normal. And if the merging can be done in O(1) regardless of the sizes of the two blocks, then a straight iteration through the array would work fine.

Like, the maximum function can be written:


int max(int* arr, int len) {
  int mid = len / 2;
  int max_left = max(arr, mid);
  int max_right = max(arr + mid, len - mid);
  if(max_left < max_right)
    return max_right;
  else
    return max_left;
}


Each recursive call produces a single value, the maximum int observed in that sub array. The procedure for merging the result of two sub arrays is to compare the two maximum values and return the larger, an O(1) operation. But the cost of the merge does not depend on the lengths of the two sub arrays, so there isn't a good reason to keep the lengths balanced, and the normal method of collecting the max by iterating through the array will be more efficient and have the same asymptotic running time.

Name: Anonymous 2012-04-06 7:05

forgot the base cases:


int max(int* arr, int len) {
  assert(len > 0);
  if(len == 1) return *arr;
  int mid = len / 2;
  int max_left = max(arr, mid);
  int max_right = max(arr + mid, len - mid);
  if(max_left < max_right)
    return max_right;
  else
    return max_left;
}

Name: Anonymous 2012-04-06 8:23

>>43
The first and last steps definitely are. I'm not 100% sure about the second, but I think it's O(n). It looks at each element of the sublists at most one time.

Name: Anonymous 2012-04-06 17:06

>>47

Split the list into a list of increasing lists:

[11,140,12,-120,13,14] → [[11,140],[12],[-120,13,14]]

If a list's last value is greater than the previous list's last value, remove all elements from the beginning of the list that are less than or equal to the last element in the previous list.

[[11,140],[12],[-120,13,14]] → [[11,140],[12],[13,14]]

Repeat the first step.

[11,140,12,13,14] → [[11,140],[12,13,14]]

Am I doin it rite?

Name: Anonymous 2012-04-06 18:02

Construct a B-tree out of the array that follows the following three rules:
1) Leaves added to the left of the parent denote "less than the value of the parent;" leaves to the right of the parent denote "greater than the value of the parent."
2) Duplicate values, when they are encountered, are ignored and are not added.
3) Maintain relative depth under the condition that whenever a leaf is added to the left, its relative depth is (re)set to 1 and whenever it is added to the right of the parent it is set to the parent's depth plus one.

This final rule allows that once you have sorted a node that will have depth 3, you have found a numeric organization (parent->right child->right child) of the array's elements that are in ascending positive value and have indicies of ascending value.  Maintain each parent's index on their node.  If you never sort to a depth of 3, the array has no such organization of elements that would satisfy the conditions.

In O(n) time.

Name: Anonymous 2012-04-06 18:51

pb[nty anys us sceyqbruak-/b[

Name: Anonymous 2012-04-06 18:52

seuqhentlia88

Name: Anonymous 2012-04-06 22:52

>>49
That's interesting, but it's O(n^2). Consider running it on:

[13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 0, 1, 2]

But that's still a good improvement over the O(n^3) solution. There might be some error in it though. Because of the insertion order and no balancing, the parent elements will always have an array index less than its children. But the array index relationship among siblings is lost. It could be that the solution consists of 3 cousins, rather than a node to right child to right grand child.

Name: Anonymous 2012-04-06 23:26

i don't think this is possible to do
in O(n) unless the list is sorted

Name: Anonymous 2012-04-06 23:39

>>52
Ah, I think I see the problem you imply. Input like [5, 3, 4, 6] would fail with the B-tree as stated.  The solution would be thwarted by the last value's branch decision using the root node.  A partial solution is no good; I need to rethink this.

Is that extra level of complexity I didn't account for the B-tree insertion being factored into the big o time?

Name: Anonymous 2012-04-06 23:55

>>54
yeah, if you want to insert n nodes into a binary search tree without any balancing, the worst case performance will be O(n^2). It happens whenever the original input is nearly already sorted, or nearly perfectly in reverse order. Every insertion will almost always go all the way to the bottom of the tree, and it degenerates to a singly linked list.

I think you were talking about a binary search tree. There is another tree called a B-tree that is an ordered tree specialized for storage on a hard disk. I don't think this is what you meant though.

There is the O(nlog(n)) algorithm mentioned earlier in the thread for finding the longest increasing subsequence. If the sequence generated has at least three elements, then you are set. But there might be an even faster algorithm, since you only need to know if there exists a increasing subsequence with length at least three.

Name: Anonymous 2012-04-07 0:16

Are you people for real?  The O(n) algorithm is trivial and you guys keep jerkin' it over how you've improved your shitty O(n^3) solution.

Name: Anonymous 2012-04-07 0:20

>>56
Shut up.  We're having much more fun than you.  Unless you're laughing at us; then, there's fun for all.  But in any case shut up.

Name: Anonymous 2012-04-07 0:21

>>56
back to /g/ with you!

Name: Anonymous 2012-04-07 1:52

>>57
fuck you faggot

>>58
fuck off and die you cock sucking piece of faggot shit

Name: Anonymous 2012-04-07 6:29

>>56
They're probably from /g/ or something.

Name: Anonymous 2012-04-07 10:33

(define (blah array)
  (define min 999999999999)
  (define max 0)
  (define min+1 999999999999)
  (define i #f)
  (define j #f)
  (define k #f)
  (for ([x (in-range 0 (vector-length array))])
    (define current (vector-ref array x))
    (cond
      ((current . < . min)
       (set! min current)
       (set! i x))
      ((and (current . > . min) (current . < . min+1))
       (set! min+1 current)
       (set! j x))
      ((current . > . max)
       (set! max current)
       (set! k x))))
  (values i j k))


Example:
> (blah (vector 999 1 998 1 7 2 8 3 8  2 9 3 4 10 4 5 6))
1
5
13


[m]
999 1 998 1 7 2 8 3 8  2 9 3 4 10 4 5 6
    ^         ^                 ^
    i         j                 k

i < j < k satisfied: 1 < 5 < 13
array[i] < array[j] < array[k] satisfied: 1 < 2 < 10
[/m]

Next time do your own homework though.

Name: Anonymous 2012-04-07 15:39

>>61

there is nothing maintaining that i < j < k.


(define (for-range-fn low high parameterized-body)
  (if (< low high)
    (begin (parameterized-body low)
           (for-range-fn (+ low 1) high parameterized-body))))

(define (blah array)
  (define min 999999999999)
  (define max 0)
  (define min+1 999999999999)
  (define i #f)
  (define j #f)
  (define k #f)
  (for-range-fn 0 (vector-length array) (lambda (x)
    (define current (vector-ref array x))
    (cond
      ((< current min)
       (display `(min goes from ,min to ,current)) (newline)
       (set! min current)
       (set! i x))
      ((and (> current min) (< current min+1))
       (display `(min+1 goes from ,min+1 to ,current)) (newline)
       (set! min+1 current)
       (set! j x))
      ((> current max)
       (display `(max goes from ,max to ,current)) (newline)
       (set! max current)
       (set! k x)))))
  (values i j k))


(blah (vector 999 1 998 1 1000 7 2 8 3 8  2 9 3 4 10 4 5 6))


(min goes from 999999999999 to 999)
(min goes from 999 to 1)
(min+1 goes from 999999999999 to 998)
(max goes from 0 to 1)
(max goes from 1 to 1000)
(min+1 goes from 998 to 7)
(min+1 goes from 7 to 2)
 1
 6
 4


good try though. I'm sure something similar to this could work.

Name: Anonymous 2012-04-07 16:43

>>62
)))))

Name: Anonymous 2012-04-07 17:17

>>63
)),);}}}

Name: Anonymous 2012-04-07 17:52

dubz

Name: Anonymous 2012-04-07 17:52

chick 'em wick 'em flick 'em pick'em

Name: Anonymous 2012-04-07 17:53

^--- woweeeboweeeee

Name: Anonymous 2012-04-07 21:54

>>64
just because that's shit doesn't mean >>63 isn't

Name: Anonymous 2012-04-07 22:18

>>68
\t\t \t  \t

Name: Anonymous 2012-04-07 23:23

>>69
tab after space? pig disgusting

Name: bampu pantsu 2012-05-29 4:11

bampu pantsu

Don't change these.
Name: Email:
Entire Thread Thread List