Multi-level marketing "legs" investment rule
Jelly, 12 9 bytes
ṢṚ×J$ṫ⁵<Ṃ
A full program which accepts x T M
and prints 0
if the peer is rewarded and 1
if not.
Try it online!
How?
ṢṚ×J$ṫ⁵<Ṃ - Main Link: list of numbers, x; number, T e.g. [100, 50, 77, 22, 14, 45], 180
Ṣ - sort x [ 14, 22, 45, 50, 77,100]
Ṛ - reverse [100, 77, 50, 45, 22, 14]
$ - last two links as a monad:
J - range of length [ 1, 2, 3, 4, 5, 6]
× - multiply [100,154,150,180,110, 84]
ṫ - tail from index:
⁵ - 5th argument (3rd input), M (e.g. M=3) [ 150,180,110, 84]
< - less than T? [ 1, 0, 1, 1]
Ṃ - minimum 0
05AB1E, 9 bytes
{Rƶ.ssè›ß
Try it online or verify all test cases.
Port of @JonathanAllan's Jelly answer, so also takes the inputs x T M
and outputs 0
for "yes"
and 1
for "no"
. If this is not allowed, and it should be inverted, a trailing _
can be added.
Explanation:
{ # Sort the (implicit) input `x`
# i.e. `x`=[100,50,77,22,14,45] → [14,22,45,50,77,100]
R # Reverse it
# i.e. [14,22,45,50,77,100] → [100,77,50,45,22,14]
ƶ # Multiply it by it's 1-indexed range
# i.e. [100,77,50,45,22,14] → [100,154,150,180,110,84]
.s # Get all the suffices of this list
# i.e. [100,154,150,180,110,84]
# → [[84],[110,84],[180,110,84],[150,180,110,84],[100,154,150,180,110,84]]
s # Swap to take the (implicit) input `T`
è # Get the prefix at index `T`
# i.e. [[84],[110,84],[180,110,84],[150,180,110,84],[100,154,150,180,110,84]]
# and `T=3` → [150,180,110,84]
› # Check for each list-value if the (implicit) input `M` is larger than it
# i.e. [150,180,110,84] and `M`=180 → [1,0,1,1]
ß # And pop and push the minimum value in the list (which is output implicitly)
# i.e. [1,0,1,1] → 0
Alternative for .ssè
:
sG¦}
Try it online or verify all test cases.
Explanation:
s # Swap to take the (implicit) input `T`
G } # Loop `T-1` times:
¦ # Remove the first item from the list that many times
# i.e. [100,154,150,180,110,84] and `T=3` → [150,180,110,84]
C# (Visual C# Interactive Compiler) with flag /u:System.Linq.Enumerable
, 69 bytes
(n,x,t,m)=>Range(0,n-m+1).Where(b=>x.Count(a=>a>=t/(b+m))>=b+m).Any()
Try it online!
// Takes in 4 parameters as input
(n,x,t,m)=>
// Create a new array with the length of all the numbers from m to n, inclusive
Range(0,n-m+1)
// And filter the results by
.Where((_,b)=>
// If the number of people that invested more than the total amount divided by the index plus m
x.Count(a=>a>=t/(b+m))
// Is greater than the index plus m
>= b+m)
// And check if there is at least one value in the filtered IEnumerable<int>, and if there is, return true
.Any()
Without any flags, 73 bytes
(n,x,t,m)=>new int[n-m+1].Where((_,b)=>x.Count(a=>a>=t/(b+m))>=b+m).Any()
Try it online!