📜  来自前N个自然数的总和为N的对的计数

📅  最后修改于: 2021-04-21 21:46:57             🧑  作者: Mango

给定整数N ,任务是对前N个自然数中的对数进行计数,总和等于N。

例子:

天真的方法:
解决问题的最简单方法是使用两个指针。请按照以下步骤解决问题:

  • 初始设置i = 0j = N – 1
  • 迭代直到i> = j ,并针对每对i j ,检查它们的总和是否等于N。如果是这样,请增加对的数量
  • 通过将ij分别增加和减少1来移至下一对。
  • 最后,打印获得的对

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
int numberOfPairs(int n)
{
 
    // Stores the count of
    // pairs
    int count = 0;
    // Set the two pointers
    int i = 1, j = n - 1;
 
    while (i < j) {
 
        // Check if the sum of
        // pairs is equal to N
        if (i + j == n) {
            // Increase the count
            // of pairs
            count++;
        }
 
        // Move to the next pair
        i++;
        j--;
    }
 
    return count;
}
 
// Driver Code
int main()
{
    int n = 8;
    cout << numberOfPairs(n);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to calculate the value of count
public static int numberOfPairs(int n)
{
 
    // Stores the count of pairs
    int count = 0;
 
    // Set the two pointers
    int i = 1, j = n - 1;
 
    while (i < j)
    {
         
        // Check if the sum of
        // pairs is equal to N
        if (i + j == n)
        {
             
            // Increase the count
            // of pairs
            count++;
        }
 
        // Move to the next pair
        i++;
        j--;
    }
    return count;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 8;
     
    System.out.println(numberOfPairs(n));
}
}
 
// This code is contributed by piyush3010


Python3
# Python3 program for the
# above approach
def numberOfPairs(n):
   
  # Stores the count
  # of pairs
  count = 0
   
  # Set the two pointers
  i = 1
  j = n - 1
 
  while(i < j):
     
    # Check if the sum
    # of pirs is equal to n
    if (i + j) == n:
       
      # Increase the count of pairs
      count += 1
       
      # Move to the next pair
      i += 1
      j -= 1
       
  return count
 
# Driver code
if __name__=='__main__':
   
  n = 8
  print(numberOfPairs(n))
     
# This code is contributed by virusbuddah_


C#
// C# program for the above approach
using System;
class GFG{
      
// Function to calculate the value of count
public static int numberOfPairs(int n)
{
  
    // Stores the count of pairs
    int count = 0;
  
    // Set the two pointers
    int i = 1, j = n - 1;
  
    while (i < j)
    {
          
        // Check if the sum of
        // pairs is equal to N
        if (i + j == n)
        {
              
            // Increase the count
            // of pairs
            count++;
        }
  
        // Move to the next pair
        i++;
        j--;
    }
    return count;
}
  
// Driver code
public static void Main (string[] args)
{
    int n = 8;
      
    Console.Write(numberOfPairs(n));
}
}
  
// This code is contributed by rock_cool


Javascript


C++
// C++ program to count the number
// of pairs among the first N
// natural numbers with sum N
#include 
using namespace std;
 
// Function to return the
// count of pairs
int numberOfPairs(int n)
{
    // If n is even
    if (n % 2 == 0)
 
        // Count of pairs
        return n / 2 - 1;
 
    // Otherwise
    else
        return n / 2;
}
 
// Driver Code
int main()
{
    int n = 8;
    cout << numberOfPairs(n);
 
    return 0;
}


Java
// Java program to count the number
// of pairs among the first N
// natural numbers with sum N
import java.io.*;
 
class GFG{
     
// Function to calculate the value of count
public static int numberOfPairs(int n)
{
 
    // If n is even
    if (n % 2 == 0)
     
        // Count of pairs
        return n / 2 - 1;
 
    // Otherwise
    else
        return n / 2;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 8;
     
    System.out.println(numberOfPairs(n));
}
}
 
// This code is contributed by piyush3010


Python3
# Python3 program to count the number
# of pairs among the first N
# natural numbers with sum N
 
# Function to calculate the value of count
def numberOfPairs(n):
 
    # If n is even
    if (n % 2 == 0):
 
        # Count of pairs
        return n // 2 - 1;
 
    # Otherwise
    else:
        return n // 2;
 
# Driver code
n = 8;
 
print(numberOfPairs(n));
 
# This code is contributed by Rajput-Ji


C#
// C# program to count the number
// of pairs among the first N
// natural numbers with sum N
using System;
class GFG{
      
// Function to calculate the value of count
public static int numberOfPairs(int n)
{
  
    // If n is even
    if (n % 2 == 0)
      
        // Count of pairs
        return n / 2 - 1;
  
    // Otherwise
    else
        return n / 2;
}
  
// Driver code
public static void Main (string[] args)
{
    int n = 8;
      
    Console.Write(numberOfPairs(n));
}
}
  
// This code is contributed by Ritik Bansal


Javascript


输出
3

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

高效方法:
为了优化上述方法,我们只需要观察N偶数还是奇数。如果N为偶数,则可能的对数为N / 2 –1。否则,为N / 2。

下面是上述方法的实现:

C++

// C++ program to count the number
// of pairs among the first N
// natural numbers with sum N
#include 
using namespace std;
 
// Function to return the
// count of pairs
int numberOfPairs(int n)
{
    // If n is even
    if (n % 2 == 0)
 
        // Count of pairs
        return n / 2 - 1;
 
    // Otherwise
    else
        return n / 2;
}
 
// Driver Code
int main()
{
    int n = 8;
    cout << numberOfPairs(n);
 
    return 0;
}

Java

// Java program to count the number
// of pairs among the first N
// natural numbers with sum N
import java.io.*;
 
class GFG{
     
// Function to calculate the value of count
public static int numberOfPairs(int n)
{
 
    // If n is even
    if (n % 2 == 0)
     
        // Count of pairs
        return n / 2 - 1;
 
    // Otherwise
    else
        return n / 2;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 8;
     
    System.out.println(numberOfPairs(n));
}
}
 
// This code is contributed by piyush3010

Python3

# Python3 program to count the number
# of pairs among the first N
# natural numbers with sum N
 
# Function to calculate the value of count
def numberOfPairs(n):
 
    # If n is even
    if (n % 2 == 0):
 
        # Count of pairs
        return n // 2 - 1;
 
    # Otherwise
    else:
        return n // 2;
 
# Driver code
n = 8;
 
print(numberOfPairs(n));
 
# This code is contributed by Rajput-Ji

C#

// C# program to count the number
// of pairs among the first N
// natural numbers with sum N
using System;
class GFG{
      
// Function to calculate the value of count
public static int numberOfPairs(int n)
{
  
    // If n is even
    if (n % 2 == 0)
      
        // Count of pairs
        return n / 2 - 1;
  
    // Otherwise
    else
        return n / 2;
}
  
// Driver code
public static void Main (string[] args)
{
    int n = 8;
      
    Console.Write(numberOfPairs(n));
}
}
  
// This code is contributed by Ritik Bansal

Java脚本


输出
3

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