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

📅  最后修改于: 2022-05-13 01:55:29.622000             🧑  作者: Mango

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

给定一个不同整数的数组和一个总和值。查找总和小于给定总和值的三元组计数。预期的时间复杂度为 O(n 2 )。
例子:

Input : arr[] = {-2, 0, 1, 3}
        sum = 2.
Output : 2
Explanation :  Below are triplets with sum less than 2
               (-2, 0, 1) and (-2, 0, 3) 

Input : arr[] = {5, 1, 3, 4, 7}
        sum = 12.
Output : 4
Explanation :  Below are triplets with sum less than 12
               (1, 3, 4), (1, 3, 5), (1, 3, 7) and 
               (1, 4, 5)

一个简单的解决方案是运行三个循环以逐个考虑所有三元组。对于每个三元组,如果三元组总和小于给定总和,则比较总和并增加计数。

Python 3
# A Simple Python 3 program to count triplets with sum smaller
# than a given value
  
def countTriplets(arr, n, sum):
  
    # Initialize result
    ans = 0
  
    # Fix the first element as A[i]
    for i in range( 0 ,n-2):
      
        # Fix the second element as A[j]
        for j in range( i+1 ,n-1):
      
            # Now look for the third number
            for k in range( j+1, n):
                if (arr[i] + arr[j] + arr[k] < sum):
                    ans+=1
      
    return ans
  
# Driver program
arr = [5, 1, 3, 4, 7]
n = len(arr)
sum = 12
print(countTriplets(arr, n, sum))
  
#Contributed by Smitha


Python3
# Python3 program to count triplets with 
# sum smaller than a given value 
  
  
# Function to count triplets with sum smaller
# than a given value         
def countTriplets(arr,n,sum):
      
    # Sort input array
    arr.sort()
      
    # Initialize result 
    ans = 0
      
    # Every iteration of loop counts triplet with 
    # first element as arr[i].
    for i in range(0,n-2):
          
        # Initialize other two elements as corner elements 
        # of subarray arr[j+1..k] 
        j = i + 1
        k = n-1
  
        # Use Meet in the Middle concept 
        while(j < k):
              
            # If sum of current triplet is more or equal, 
            # move right corner to look for smaller values
            if (arr[i]+arr[j]+arr[k] >=sum):
                k = k-1
              
            # Else move left corner 
            else:
                  
                # This is important. For current i and j, there 
                # can be total k-j third elements. 
                ans += (k - j)
                j = j+1
      
    return ans
  
# Driver program 
if __name__=='__main__':
    arr = [5, 1, 3, 4, 7] 
    n = len(arr) 
    sum = 12
    print(countTriplets(arr, n, sum)) 
      
# This code is contributed by 
# Yatin Gupta


输出:

4

上述解决方案的时间复杂度为 O(n 3 )。一个有效的解决方案可以通过首先对数组进行排序,然后在循环中使用本文的方法 1 来计算 O(n 2 ) 中的三元组。

1) Sort the input array in increasing order.
2) Initialize result as 0.
3) Run a loop from i = 0 to n-2.  An iteration of this loop finds all
   triplets with arr[i] as first element.
     a) Initialize other two elements as corner elements of subarray
        arr[i+1..n-1], i.e., j = i+1 and k = n-1
     b) Move j and k toward each other until they meet, i.e., while (j= sum
                then k--
            // Else for current i and j, there can (k-j) possible third elements
            // that satisfy the constraint.
            (ii) Else Do ans += (k - j) followed by j++ 

下面是上述思想的实现。

Python3

# Python3 program to count triplets with 
# sum smaller than a given value 
  
  
# Function to count triplets with sum smaller
# than a given value         
def countTriplets(arr,n,sum):
      
    # Sort input array
    arr.sort()
      
    # Initialize result 
    ans = 0
      
    # Every iteration of loop counts triplet with 
    # first element as arr[i].
    for i in range(0,n-2):
          
        # Initialize other two elements as corner elements 
        # of subarray arr[j+1..k] 
        j = i + 1
        k = n-1
  
        # Use Meet in the Middle concept 
        while(j < k):
              
            # If sum of current triplet is more or equal, 
            # move right corner to look for smaller values
            if (arr[i]+arr[j]+arr[k] >=sum):
                k = k-1
              
            # Else move left corner 
            else:
                  
                # This is important. For current i and j, there 
                # can be total k-j third elements. 
                ans += (k - j)
                j = j+1
      
    return ans
  
# Driver program 
if __name__=='__main__':
    arr = [5, 1, 3, 4, 7] 
    n = len(arr) 
    sum = 12
    print(countTriplets(arr, n, sum)) 
      
# This code is contributed by 
# Yatin Gupta 

输出:

4

有关详细信息,请参阅有关总和小于给定值的计数三元组的完整文章!