maximum no. of elements in an array having sum less than or equal to k

Brij Raj Kishore

I want to find the maximum no. of elements in a given positive integer array such that their sum is less than or equal to a given no. k. For example, I have an array

[3,4,7,2,6,5,1] and k=6;

The Answer is 3 as 1,2,3 are maximum elements to give sum 6.

chiliNUT

Sort the array, count the number of elements, then start summing elements sequentially until their sum is greater than k or you have gone through each element, then subtract 1 from the count if the sum is greater than k

Pseudocode:

    let k=6
    sort the array
    [1,2,3,4,5,6,7]
    let sum=0
    let count=7 //(7 elements in the array)
    for (i=0;i<7;i++) {
        sum+=array[i];
        if (sum>k)
            break;
    }
    if (sum>k)
    i--;

i is the max number of elements.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

maximum no. of elements in an array having sum less than or equal to k

From Dev

Maximum possible sum less than or equal to k in a 2D array using each row

From Dev

Count number of times two elements in array are less than or equal to a sum value - Javascript

From Dev

Finding the number of elements less than or equal to k in a multiset

From Dev

greatest sum sub array such that each element is less than or equal to X

From Dev

Divide an array into k or less subparts to minimize the maximum sum of each part

From Dev

Divide an array into k or less subparts to minimize the maximum sum of each part

From Dev

Largest subset in an array such that the smallest and largest elements are less than K apart

From Dev

How to return boolean statement if the sum of an arraylist is less than or equal to a maximum number?

From Dev

Arrays with elements less or equal to the elements in given array

From Dev

Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

From Dev

Return all possible combinations of numbers in an array whose sum is less than or equal to n

From Dev

sum's sum of divizors of numbers less than or equal to N

From Dev

Sum only for elements less than N

From Dev

Maximum sum of k connected elements of a matrix

From Dev

maximum no of equal elements in the array after n transformation

From Dev

with complexity better than quadratic, how to find if there is two elements in an array there sum equal a passed value

From Dev

how to find items in list that their sum is less than or equal a number

From Dev

How to replace the elements of a range less than k with k?

From Dev

How to find numbers in an array that are greater than, less than, or equal to a value?

From Dev

Remove array elements that are less than X

From Dev

Python/Numpy - calculate sum of equal array elements

From Dev

Prolog - How to find the maximum set of elements that their sum is equal to N

From Dev

Equal to or less than not working

From Dev

Less than or equal to;

From Dev

Maximum sum of non adjacent elements of an array, to be printed

From Dev

maximum sum of n consecutive elements of array

From Dev

Find increasing triplets such that sum is less than or equals to k

From Dev

Greater than or Less than equal to

Related Related

  1. 1

    maximum no. of elements in an array having sum less than or equal to k

  2. 2

    Maximum possible sum less than or equal to k in a 2D array using each row

  3. 3

    Count number of times two elements in array are less than or equal to a sum value - Javascript

  4. 4

    Finding the number of elements less than or equal to k in a multiset

  5. 5

    greatest sum sub array such that each element is less than or equal to X

  6. 6

    Divide an array into k or less subparts to minimize the maximum sum of each part

  7. 7

    Divide an array into k or less subparts to minimize the maximum sum of each part

  8. 8

    Largest subset in an array such that the smallest and largest elements are less than K apart

  9. 9

    How to return boolean statement if the sum of an arraylist is less than or equal to a maximum number?

  10. 10

    Arrays with elements less or equal to the elements in given array

  11. 11

    Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

  12. 12

    Return all possible combinations of numbers in an array whose sum is less than or equal to n

  13. 13

    sum's sum of divizors of numbers less than or equal to N

  14. 14

    Sum only for elements less than N

  15. 15

    Maximum sum of k connected elements of a matrix

  16. 16

    maximum no of equal elements in the array after n transformation

  17. 17

    with complexity better than quadratic, how to find if there is two elements in an array there sum equal a passed value

  18. 18

    how to find items in list that their sum is less than or equal a number

  19. 19

    How to replace the elements of a range less than k with k?

  20. 20

    How to find numbers in an array that are greater than, less than, or equal to a value?

  21. 21

    Remove array elements that are less than X

  22. 22

    Python/Numpy - calculate sum of equal array elements

  23. 23

    Prolog - How to find the maximum set of elements that their sum is equal to N

  24. 24

    Equal to or less than not working

  25. 25

    Less than or equal to;

  26. 26

    Maximum sum of non adjacent elements of an array, to be printed

  27. 27

    maximum sum of n consecutive elements of array

  28. 28

    Find increasing triplets such that sum is less than or equals to k

  29. 29

    Greater than or Less than equal to

HotTag

Archive