maxSumLessThanK

    0

    0

    The code output: 60

    function maxSumLessThanK(A, K) {
        A.sort((a, b) => a - b);
        let i = 0;
        let j = A.length - 1;
        let max = -1;
      
        while (i < j) {
          if (A[i] + A[j] < K) {
            max = Math.max(max, A[i] + A[j]);
            i++;
          } else {
            j--;
          }
        }
      
        return max;
      }
      
      console.log(maxSumLessThanK([34, 23, 1, 24, 75, 33, 54, 8], 60));
      console.log(maxSumLessThanK([10, 20, 30], 15));
      
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.