The problem is "Find the min length of a List that has all numbers from 1 to K". Input : List, K , N where N is the length of the List
My algorithm (it's obviously a bit C oriented) :
- Set Min= N+1
- find the first subList (FSub) that has all numbers from 1 to K
- if there isn't one then Min=0 and finish
- if there is create a list C that has all the elements of the starting list except the first length(FSub) elements.
FUNCTION THAT FINDS THE MIN (arguments: FSub, C , NewList , Min )
- if length(Fsub)=K then we don't have to continue searching since we found the min length of sublist
- If Head of FSub is in Tail of Fsub then we remove Head from the FSub, update min and recurce,
- If it isn't then we append to FSub the Head of the list C (created above) and recurse.
This stops if C is equal to the empty list
This algorithm seems time-consuming and in some cases of big lists it will propably run out of stack.
I asked about the First sublist part and got this code.
findFirstSubListHavingAllColors( NumList, List ,SubList, Len):-
once((
append(SubList, _, List), % get SubList
subtract(NumList, SubList, []), % test whether NumList is contained in SubList
length(SubList, Len)
)).
I can't connect the dots to find the solution.
EXAMPLE
List=[1,3,1,3,1,3,3,2,2,1] , K=3 , N=10
Answer should be 4 because the min SubList that contains numbers 1,2,3 is [1,3,3,2] or [3,2,2,1].