📌  相关文章
📜  对给定范围内具有不同总和的计数对

📅  最后修改于: 2021-04-18 02:24:39             🧑  作者: Mango

给定两个正整数LR ,任务是查找具有在[L,R]范围内的元素,其和不同的对的数量。

例子:

方法:请按照以下步骤解决问题:

  • 初始化一个变量,例如firstNum ,以存储对可以获取的最小和,即2 * L。
  • 初始化一个变量lastNum ,以存储可以通过两对获得的最高和,即2 * R。
  • 初始化一个变量,例如cntPairs ,以存储具有不同总和的对的计数,即lastNum – firstNum + 1
  • 最后,打印cntPairs的值。

下面是上述方法的实现:

C++
// C++ program for
// the above approach
 
#include 
using namespace std;
 
// Function to count pairs made
// up of elements from the range
// [L, R] having distinct sum
long countPairs(long L, long R)
{
 
    // Stores the least sum which
    // can be formed by the pairs
    long firstNum = 2 * L;
 
    // Stores the highest sum which
    // can be formed by the pairs
    long lastNum = 2 * R;
 
    // Stores the count of pairs
    // having distinct sum
    long Cntpairs = lastNum - firstNum + 1;
 
    // Print the count of pairs
    cout << Cntpairs;
}
 
// Driver Code
int main()
{
    long L = 2, R = 3;
 
    // Function call to count
    // the number of pairs
    // having distinct sum in
    // the range [L, R]
    countPairs(L, R);
 
    return 0;
}


Java
// Java program for
// the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to count pairs made
    // up of elements from the range
    // [L, R] having distinct sum
    static void countPairs(long L, long R)
    {
 
        // Stores the least sum which
        // can be formed by the pairs
        long firstNum = 2 * L;
 
        // Stores the highest sum which
        // can be formed by the pairs
        long lastNum = 2 * R;
 
        // Stores the count of pairs
        // having distinct sum
        long Cntpairs = lastNum - firstNum + 1;
 
        // Print the count of pairs
        System.out.println(Cntpairs);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        long L = 2, R = 3;
 
        // Function call to count
        // the number of pairs
        // having distinct sum in
        // the range [L, R]
        countPairs(L, R);
    }
}


Python3
# Python3 program for
# the above approach
 
# Function to count pairs made
# up of elements from the range
# [L, R] having distinct sum
def countPairs(L, R):
 
    # Stores the least sum which
    # can be formed by the pairs
    firstNum = 2 * L
 
    # Stores the highest sum which
    # can be formed by the pairs
    lastNum = 2 * R
 
    # Stores the count of pairs
    # having distinct sum
    Cntpairs = lastNum - firstNum + 1
 
    # Print the count of pairs
    print (Cntpairs)
 
# Driver Code
if __name__ == '__main__':
    L,R = 2,3
 
    # Function call to count
    # the number of pairs
    # having distinct sum in
    # the range [L, R]
    countPairs(L, R)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for
// the above approach
 
using System;
 
public class GFG {
 
    // Function to count pairs made
    // up of elements from the range
    // [L, R] having distinct sum
    static void countPairs(long L, long R)
    {
 
        // Stores the least sum which
        // can be formed by the pairs
        long firstNum = 2 * L;
 
        // Stores the highest sum which
        // can be formed by the pairs
        long lastNum = 2 * R;
 
        // Stores the count of pairs
        // having distinct sum
        long Cntpairs = lastNum - firstNum + 1;
 
        Console.WriteLine(Cntpairs);
    }
 
    // Driver Code
    static public void Main()
    {
        long L = 2, R = 3;
 
        // Function call to count
        // the number of pairs
        // having distinct sum in
        // the range [L, R]
        countPairs(L, R);
    }
}


输出:
3

时间复杂度: O(1)
辅助空间: O(1)