How to find the number of tuples whose sum is equal to or less than a given number?

I am developing an algorithm for finding the number of tuples whose sum is equal to or less than a given number. I know other ways to solve it using LinkedList, so I am not looking for this solution. I just want to go through the array and count possible matches that satisfy the relation. I have come up so far

public static int numberOfPairs(int[] a, int k ){

    int len = a.length;

    if (len == 0){

      return -1;
    }

    Arrays.sort(a);
    int count  = 0, left = 0, right = len -1; 

    while( left < right ){

        if ( a[left] + a[right] <= k  ){

            // System.out.println(a[left] + "\t" + a[right]);
            count++; 

            /*may be the same numbers will also satisfy the relation*/
            if ( a[left] + a[left+1] <= k) {

                count ++;
                // System.out.println(a[left] + "\t"+a[left+1]);
            }

            /*now, use iteration to traverse the same elements*/
            while (a[left] == a[left+1] && left < len-1 ){

              left++;
            }

            /*may be the same numbers will also satisfy the relation*/
            if ( a[right] + a[right-1] <= k) {

                count ++;
                // System.out.println(a[right] +"\t"+ a[right-1]);
            }

            /*now, use iteration to traverse 
            the same elements*/
            while ( a[right] == a[right-1] && right >1 ){

              right-- ; 
            }
        }

        // traverse through the array
        left++;
        right--;
    }

    return count; 
}

This does not provide the correct solution, say if I pass in an array and a number as follows,

int [] arr = { 3, 3, -1, 4, 2,5, 5, 1};
System.out.println(numberOfPairs(arr, 8));

The correct decision will consist of 15 pairs:

{ 
        {-1,1},  {-1,2} ,  {3,-1}, {-1,4 }, {-1,5}, 
        {2,1},{3,1 },{4,1}, { 5,1},
        {3,2 },{4,2}, {2,5 }, 
        {3,4 } ,  {3,5 },   
        {3,3}

}

How can I improve my code? Thank you

Note. Another way that I solve using LinkedList is as follows:

public static int numberOfPairs10(int[] arr, int sum){

    /*1.can be the same values of i 
     2. can be the same valus of j for the same i*/
    List<Integer> li = new ArrayList<Integer>();
    List<Integer> lj;
    int count = 0; 

    for ( int i =0; i < arr.length -1 ; i++){

        if (li.contains(arr[i]) )
            continue; 

        lj =  new ArrayList<Integer>();
        for (int j = i+1; j < arr.length; j++){

            if (lj.contains(arr[j]))
                continue;

            // if ( arr[i]+ arr[j] <= sum){
            if ( arr[i]+ arr[j] == sum){

                count++;
                lj.add(arr[j]);
            }
        }

        li.add(arr[i]);
    }

    return count;
}
+4
source share
2 answers

int [] arr = { 3, 3, -1, 4, 2,5, 5, 1};
numberOfPairs(arr, 8);

public static int numberOfPairs(int[] a, int k ) {
    int len = a.length;
    int counter = 0;
    boolean isCheckedLeft = false;
    boolean isCheckedRight = false;
    int i, j, h;

    Arrays.sort(a);
    for (i = 0; i < len; i++) {

        isCheckedLeft = false;
        for (j = i - 1; j >= 0; j--) {
            if (a[i] == a[j]) {
                isCheckedLeft = true;
                break;
            }
        }
        if (isCheckedLeft) {
            continue;
        }

        for (j = i + 1; j < len; j++) {

            isCheckedRight = false;
            for (h = j - 1; h > i; h--) {
                if (a[j] == a[h]) {
                    isCheckedRight = true;
                    break;
                }
            }
            if (isCheckedRight) {
                continue;
            }

            System.out.print("("+a[i]+", "+a[j]+") ");
            if (a[i] + a[j] <= k) {
                counter++;
            }
        }
    }
    return counter;
}
+2

, , . , Java 8:

public int numberOfPairs(int[] values, int maxVal) {
    return IntStream.range(0, values.length)
        .flatMap(i1 -> IntStream.range(i1 + 1, values.length)
            .map(i2 -> Arrays.asList(values[i1], values[i2])))
        .distinct().filter(l -> l.stream().sum() <= maxVal).count();
} 
+2

Source: https://habr.com/ru/post/1622841/


All Articles