📌  相关文章
📜  更改给定数组中的一个元素以使其成为算术级数

📅  最后修改于: 2021-04-27 17:21:47             🧑  作者: Mango

给定一个数组,该数组是原始算术级数,但其中一个元素已更改。任务是使其再次成为算术级数。如果有很多这样的可能序列,请返回其中任何一个。数组的长度将始终大于2。
例子:

方法:

算术级数的关键组成部分是初始项和共同点。因此,我们的任务是找到这两个值,以了解实际的算术级数。

  • 如果数组的长度为3,则将公共差作为任意两个元素之间的差。
  • 否则,请尝试使用前四个元素找到共同的区别。
    • 使用公式a [1] – a [0] = a [2] – a [1]检查前三个元素是否处于算术级数中。如果它们在算术级数上,则公共差d = a [1] – a [0],并且初始项将是a [0]
    • 使用公式a [2] – a [1] = a [3] – a [2]检查第二,第三和第四元素是否处于算术级数。如果它们在算术级数上,则公共差d = a [2] – a [1],这意味着第一个元素已更改,因此初始项为a [0] = a [1] – d
    • 在以上两种情况下,我们都检查了第一个元素或第四个元素是否已更改。如果两种情况都为假,则表示第一个或第四个元素未更改。因此,可以将共同差取为d =(a [3] – a [0])/ 3,而初始项= a [0]
  • 使用初始术语和共同差异打印所有元素。

下面是上述方法的实现:

C/C++
// C++ program to change one element of an array such
// that the resulting array is in arithmetic progression.
#include 
using namespace std;
  
// Finds the initial term and common difference and
// prints the resulting sequence.
void makeAP(int arr[], int n)
{
    int initial_term, common_difference;
  
    if (n == 3) {
        common_difference = arr[2] - arr[1];
        initial_term = arr[1] - common_difference;
    }
  
    else if ((arr[1] - arr[0]) == arr[2] - arr[1]) {
  
        // Check if the first three elements are in
        // arithmetic progression
        initial_term = arr[0];
        common_difference = arr[1] - arr[0];
    }
    else if ((arr[2] - arr[1]) == (arr[3] - arr[2])) {
  
        // Check if the first element is not
        // in arithmetic progression
        common_difference = arr[2] - arr[1];
        initial_term = arr[1] - common_difference;
    }
    else {
  
        // The first and fourth element are
        // in arithmetic progression
        common_difference = (arr[3] - arr[0]) / 3;
        initial_term = arr[0];
    }
  
    // Print the arithmetic progression
    for (int i = 0; i < n; i++)
        cout << initial_term + (i * common_difference) << " ";
    cout << endl;
}
  
// Driver Program
int main()
{
    int arr[] = { 1, 3, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    makeAP(arr, n);
  
    return 0;
}


Java
// Java program to change one element of an array such
// that the resulting array is in arithmetic progression.
import java.util.Arrays;
  
class AP {
    static void makeAP(int arr[], int n)
    {
        int initial_term, common_difference;
  
        // Finds the initial term and common difference and
        // prints the resulting array.
        if (n == 3) {
            common_difference = arr[2] - arr[1];
            initial_term = arr[1] - common_difference;
        }
        else if ((arr[1] - arr[0]) == arr[2] - arr[1]) {
  
            // Check if the first three elements are in
            // arithmetic progression
            initial_term = arr[0];
            common_difference = arr[1] - arr[0];
        }
        else if ((arr[2] - arr[1]) == (arr[3] - arr[2])) {
            // Check if the first element is not
            // in arithmetic progression
            common_difference = arr[2] - arr[1];
            initial_term = arr[1] - common_difference;
        }
        else {
            // The first and fourth element are
            // in arithmetic progression
            common_difference = (arr[3] - arr[0]) / 3;
            initial_term = arr[0];
        }
  
        // Print the arithmetic progression
        for (int i = 0; i < n; i++)
            System.out.print(initial_term +
                            (i * common_difference) + " ");
        System.out.println();
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 3, 7 };
        int n = arr.length;
        makeAP(arr, n);
    }
}


Python3
# Python program to change one element of an array such
# that the resulting array is in arithmetic progression.
def makeAP(arr, n): 
    initial_term, common_difference = 0, 0
    if (n == 3):
        common_difference = arr[2] - arr[1]
        initial_term = arr[1] - common_difference
    elif((arr[1] - arr[0]) == arr[2] - arr[1]):
  
        # Check if the first three elements are in 
        # arithmetic progression
        initial_term = arr[0]
        common_difference = arr[1] - arr[0]
          
    elif((arr[2] - arr[1]) == (arr[3] - arr[2])):
  
        # Check if the first element is not 
        # in arithmetic progression
        common_difference = arr[2] - arr[1]
        initial_term = arr[1] - common_difference
          
    else:
        # The first and fourth element are 
        # in arithmetic progression
        common_difference = (arr[3] - arr[0]) / 3
        initial_term = arr[0]
  
    # Print the arithmetic progression
    for i in range(n):
        print(int(initial_term+
                 (i * common_difference)), end = " ")
    print()
    
# Driver code 
arr = [1, 3, 7] 
n = len(arr) 
makeAP(arr, n)


C#
// C# program to change one element of an array such
// that the resulting array is in arithmetic progression.
using System;
  
public class AP 
{
    static void makeAP(int []arr, int n)
    {
        int initial_term, common_difference;
  
        // Finds the initial term and common difference and
        // prints the resulting array.
        if (n == 3) 
        {
            common_difference = arr[2] - arr[1];
            initial_term = arr[1] - common_difference;
        }
        else if ((arr[1] - arr[0]) == arr[2] - arr[1]) 
        {
  
            // Check if the first three elements are in
            // arithmetic progression
            initial_term = arr[0];
            common_difference = arr[1] - arr[0];
        }
        else if ((arr[2] - arr[1]) == (arr[3] - arr[2]))
        {
            // Check if the first element is not
            // in arithmetic progression
            common_difference = arr[2] - arr[1];
            initial_term = arr[1] - common_difference;
        }
        else
        {
            // The first and fourth element are
            // in arithmetic progression
            common_difference = (arr[3] - arr[0]) / 3;
            initial_term = arr[0];
        }
  
        // Print the arithmetic progression
        for (int i = 0; i < n; i++)
            Console.Write(initial_term +
                            (i * common_difference) + " ");
        Console.WriteLine();
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = { 1, 3, 7 };
        int n = arr.Length;
        makeAP(arr, n);
    }
}
  
// This code contributed by Rajput-Ji


输出:
-1 3 7

时间复杂度:O(N),其中N是数组中元素的数量。