📌  相关文章
📜  减少数组后找到最后剩余的元素

📅  最后修改于: 2021-09-04 08:20:49             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个整数K 。任务是在减少数组后找到数组中最后剩余的元素。减少数组的规则是:

  • 第一个和最后一个元素说 X 和 Y 被选择并从数组 arr[] 中删除。
  • 值 X 和 Y 相加。 Z = X + Y。
  • Z % K的值插入数组 arr[] 的第((N/2) + 1)位置,其中 N 表示数组的当前长度。

例子:

朴素的方法:这个问题的朴素的方法是在每一步,找到数组中的第一个元素和最后一个元素并计算(X + Y) % K ,其中 X 是第一个元素,Y 是数组的最后一个元素每一步。计算完这个值后,在给定的位置插入这个值。
时间复杂度: O(N 2 )
有效的方法:

  • 仔细观察,可以说阵列模K的元素之和在阵列中始终不变。
  • 这是因为我们基本上是将值X + Y % K插入回数组中。
  • 因此,可以通过直接找到数组的总和并找到值sum % K在线性时间内解决此问题。

下面是上述方法的实现:

C++
// C++ program to find the value of the
// reduced Array by reducing the array
// based on the given conditions
 
#include 
using namespace std;
 
// Function to find the value of the
// reduced Array by reducing the array
// based on the given conditions
int find_value(int a[], int n, int k)
{
    // Variable to store the sum
    int sum = 0;
 
    // For loop to iterate through the
    // given array and find the sum
    for (int i = 0; i < n; i++) {
        sum += a[i];
    }
 
    // Return the required value
    return sum % k;
}
 
// Driver code
int main()
{
    int n = 5, k = 3;
    int a[] = { 12, 4, 13, 0, 5 };
    cout << find_value(a, n, k);
    return 0;
}


Java
// Java program to find the value of the
// reduced Array by reducing the array
// based on the given conditions
 
public class GFG {
 
    // Function to find the value of the
    // reduced Array by reducing the array
    // based on the given conditions
    public static int find_value(int a[], int n, int k)
    {
        // Variable to store the sum
        int sum = 0;
 
        // For loop to iterate through the
        // given array and find the sum
        for (int i = 0; i < n; i++) {
            sum += a[i];
        }
 
        // Return the required value
        return sum % k;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 5, k = 3;
        int a[] = { 12, 4, 13, 0, 5 };
        System.out.println(find_value(a, n, k));
    }
}


Python3
# Python3 program to find the value of the
# reduced Array by reducing the array
# based on the given conditions
 
# Function to find the value of the
# reduced Array by reducing the array
# based on the given conditions
def find_value(a, n, k):
 
    # Variable to store the sum
    sum = 0
 
    # For loop to iterate through the
    # given array and find the sum
    for i in range(n):
        sum += a[i]
 
    # Return the required value
    return sum % k
 
# Driver code
if __name__ == "__main__":
    n, k = 5, 3;
    a = [12, 4, 13, 0, 5];
    print(find_value(a, n, k))


C#
// C# program to find the value of the
// reduced Array by reducing the array
// based on the given conditions
using System;
 
class GFG {
 
    // Function to find the value of the
    // reduced Array by reducing the array
    // based on the given conditions
    public static int find_value(int []a, int n, int k)
    {
        // Variable to store the sum
        int sum = 0;
 
        // For loop to iterate through the
        // given array and find the sum
        for (int i = 0; i < n; i++) {
            sum += a[i];
        }
 
        // Return the required value
        return sum % k;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int n = 5, k = 3;
        int []a = { 12, 4, 13, 0, 5 };
        Console.WriteLine(find_value(a, n, k));
    }
}
 
// This code is contributed  by AnkitRai01


Javascript


输出:
1

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live