Skip to content

Instantly share code, notes, and snippets.

@crutchcorn
Last active September 24, 2019 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crutchcorn/137ec7db36f98cf6c2aabe0a1890f95a to your computer and use it in GitHub Desktop.
Save crutchcorn/137ec7db36f98cf6c2aabe0a1890f95a to your computer and use it in GitHub Desktop.

Given an array of integers 0-9 inclusive, compute the max sum of k elements in the array. Where the length of the array > 0 and k is 0 < k <= length array Example: array ar is ar = [3, 2, 4, 5, 1, 0, 9, 8, 8, 7], and k is k = 3. Should return 25. finish this function (this is python, but you can use any language):

def compute_max_k(ar, k):
    return
// Array value
a = [3, 2, 4, 5, 1, 0, 9, 8, 8, 7];
k = 3;
f='filter'
r='reduce'
c='concat'
// Okay now do the rest
const answer = (v=>(b=>Array.from({length: k})[r](p=>(([N])=>v.shift()&&p[c](N))(b(v)), [])[r]((a,i) => a+i, 0))((a,i=0)=>a[f](n=>(i=Math.max(i,n))&&i==n)))(a.sort((i,n)=>n-i))
a = [3, 2, 4, 5, 1, 0, 9, 8, 8, 7];
k = 3;
r = 'reduce'
const answer = (b => Array.from({ length: k })[r](prev => (([N]) => a.splice(a.findIndex(i => i == N), 1) && prev.concat(N))(b(a)), []))((a, i = 0) => { for (v = a; !v.every(n => n == v[0]);)v = v.filter(n => (i = Math.max(n, i)) && n == i); return v })[r]((a, i) => a + i, 0)
// sort the numbers small to big
v=a.sort((i,n)=>i-n)
// Get the biggest number in array
b=(a,i=0)=>a.filter(n=>(i=Math.max(i,n))&&i==n)
// Get the top 3 highest numbers in an array
w = Array.from({length: k}).reduce(prev=>(([N])=> {
// mutate `v` to `pop` the top value off the stack for the next iteration
v.shift()
// concat the previous value with the new one
return prev.concat(N)
})(b(v)), [])
// Add them together
const answer = w.reduce((a,i) => a+i, 0))
@crutchcorn
Copy link
Author

crutchcorn commented Sep 24, 2019

finalArr = [54,6,78,54,2,1]
secondArr = finalArr;
biggestNum = 0;

for (;!secondArr.every(i => i === secondArr[0]);)
    secondArr = secondArr.filter(i => (biggestNum=Math.max(i, biggestNum))&&i==biggestNum)

Is a way to get the biggest item without using a sort

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment