Home  >  Article  >  Web Front-end  >  JavaScript program to calculate triples whose sum is less than a given value

JavaScript program to calculate triples whose sum is less than a given value

PHPz
PHPzforward
2023-09-24 22:37:021494browse

JavaScript 程序计算总和小于给定值的三元组

We will write a JavaScript program to count the number of triples whose sum is less than a given value. This problem can be solved by sorting the array and using two pointers to check possible combinations. First, we will sort the array in ascending order, then, for each element in the array, we will use two pointers to check for triples whose sum is less than the given value. The number of such triples will be the count we will keep track of.

Additionally, we will update the count and pointer based on the sum of the triples being less than or equal to the given value. In this way, we can solve the problem efficiently with O(n^2) time complexity. This is a very useful technique to remember in case of future problems where we need to find counts for certain combinations that satisfy certain conditions.

Finally, we will return the count of such triples whose sum is less than the given value.

method

  • First, sort the given array of numbers in ascending order.

  • Initialize three variables: left, right and count.

  • Then use the two pointer method, the left pointer starts from 0, and the right pointer starts from end.

  • For each iteration, calculate the sum of the current triplet (the element pointed to the left, the element pointed to the right, the current element).

  • If the sum is less than the given value, increment the count and left pointer.

  • If the sum is greater than the given value, decrement the right pointer. Repeat this process until the left pointer is smaller than the right pointer.

Example

This is a complete example of a JavaScript program to count the number of triples whose sum is less than a given value -

function countTriplets(arr, sum) {
   let count = 0;
   arr.sort((a, b) => a - b); 
   // sorting the array in ascending order
   for (let i = 0; i < arr.length - 2; i++) {
      let left = i + 1;
      let right = arr.length - 1;
      while (left < right) {
         if (arr[i] + arr[left] + arr[right] >= sum) {
         right--;
         } else {
            count += right - left;
            left++;
         }
      }
   }
   return count;
}
const arr = [5, 1, 3, 4, 7];
const sum = 12;
console.log(countTriplets(arr, sum));

illustrate

    The
  • countTriplets function takes the array arr and the value sum as its arguments.

  • The
  • count variable keeps track of the number of triples whose sum is less than sum.

  • arr Sort in ascending order using the sort function.

  • Outer loopfor (let i = 0; i Iterate over the array, left and right pointers Initialized to the next index of i and the last index of the array respectively.

  • while (left The loop continues until the left pointer is greater than or equal to the right pointer.

  • while (left The loop continues until the left pointer is greater than or equal to the right pointer.

  • In each iteration of the while loop, arr[i], arr[left], and arr[right] are calculated . If that sum is greater than or equal to the sum, the right pointer is decremented. If the sum is less than the sum, the count will be incremented by the number of elements remaining between the left and right b> pointers, left The pointer is incremented.

  • The function returns the count variable, which represents the number of triples whose sum is less than sum.

The above is the detailed content of JavaScript program to calculate triples whose sum is less than a given value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete