📜  最小化算术级数(AP)的第N个项

📅  最后修改于: 2021-06-25 13:17:42             🧑  作者: Mango

给定两个整数AB(它们是算术级数序列的任意两项)和整数N ,任务是最小化该算术级数的N项。

注意: AP系列的所有元素必须为正。

例子:

方法:可以通过在算术级数中的所有可能位置放置AB并检查产生最小的N项的方法来解决该问题。考虑到AB的位置分别为ij ,则需要进行以下计算:

最后,返回获得的最小的N项。
请按照以下步骤解决问题:

  1. 初始化一个变量,例如res,以存储所需的N算术级数项的最小可能值。
  2. 使用两个嵌套循环,将AB放置在AP中所有可能的位置,然后使用上述计算来计算第N项。不断更新res以存储第N项获得的最小值。
  3. 最后,将res的值打印为必需的答案。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the smallest
// Nth term of an AP possible
int smallestNth(int A, int B, int N)
{
    // Stores the smallest Nth term
    int res = INT_MAX;
 
    for (int i = 1; i < N; i++) {
        for (int j = N; j > i; j--) {
 
            // Check if common difference
            // of AP is an integer
            if ((B - A) % (j - i) == 0) {
 
                // Store the common
                // difference
                int D = (B - A) / (j - i);
 
                // Store the First Term
                // of that AP
                int FirstTerm = A - (i - 1) * D;
 
                // Store the Nth term of
                // that AP
                int NthTerm = FirstTerm + (N - 1) * D;
 
                // Check if all elements of
                // an AP are positive
                if (FirstTerm > 0)
                    res = min(res, NthTerm);
            }
        }
    }
 
    // Return the least
    // Nth term obtained
    return res;
}
 
// Driver Code
int main()
{
    int N = 3;
    int A = 1;
    int B = 6;
    cout << smallestNth(A, B, N);
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
  
// Function to find the smallest
// Nth term of an AP possible
static int smallestNth(int A, int B, int N)
{
 
    // Stores the smallest Nth term
    int res = Integer.MAX_VALUE;
  
    for(int i = 1; i < N; i++)
    {
        for(int j = N; j > i; j--)
        {
             
            // Check if common difference
            // of AP is an integer
            if ((B - A) % (j - i) == 0)
            {
                 
                // Store the common
                // difference
                int D = (B - A) / (j - i);
  
                // Store the First Term
                // of that AP
                int FirstTerm = A - (i - 1) * D;
  
                // Store the Nth term of
                // that AP
                int NthTerm = FirstTerm + (N - 1) * D;
  
                // Check if all elements of
                // an AP are positive
                if (FirstTerm > 0)
                    res = Math.min(res, NthTerm);
            }
        }
    }
  
    // Return the least
    // Nth term obtained
    return res;
}
  
// Driver Code
public static void main (String[] args)
{
    int N = 3;
    int A = 1;
    int B = 6;
  
    System.out.print(smallestNth(A, B, N));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement
# the above approach
import sys
 
# Function to find the smallest
# Nth term of an AP possible
def smallestNth(A, B, N):
     
    # Stores the smallest Nth term
    res = sys.maxsize
 
    for i in range(1, N):
        for j in range(N, i, -1):
 
            # Check if common difference
            # of AP is an integer
            if ((B - A) % (j - i) == 0):
 
                # Store the common
                # difference
                D = (B - A) // (j - i)
 
                # Store the First Term
                # of that AP
                FirstTerm = A - (i - 1) * D
 
                # Store the Nth term of
                # that AP
                NthTerm = FirstTerm + (N - 1) * D
 
                # Check if all elements of
                # an AP are positive
                if (FirstTerm > 0):
                    res = min(res, NthTerm)
 
    # Return the least
    # Nth term obtained
    return res
 
# Driver Code
if __name__ == '__main__':
     
    N = 3
    A = 1
    B = 6
     
    print(smallestNth(A, B, N))
     
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
  
// Function to find the smallest
// Nth term of an AP possible
static int smallestNth(int A, int B, int N)
{
     
    // Stores the smallest Nth term
    int res = Int32.MaxValue;
  
    for(int i = 1; i < N; i++)
    {
        for(int j = N; j > i; j--)
        {
             
            // Check if common difference
            // of AP is an integer
            if ((B - A) % (j - i) == 0)
            {
                 
                // Store the common
                // difference
                int D = (B - A) / (j - i);
  
                // Store the First Term
                // of that AP
                int FirstTerm = A - (i - 1) * D;
  
                // Store the Nth term of
                // that AP
                int NthTerm = FirstTerm + (N - 1) * D;
  
                // Check if all elements of
                // an AP are positive
                if (FirstTerm > 0)
                    res = Math.Min(res, NthTerm);
            }
        }
    }
  
    // Return the least
    // Nth term obtained
    return res;
}
  
// Driver Code
public static void Main ()
{
    int N = 3;
    int A = 1;
    int B = 6;
     
    Console.Write(smallestNth(A, B, N));
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
11

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