📌  相关文章
📜  缩小数组后查找最后剩余的元素

📅  最后修改于: 2021-06-26 01:58:29             🧑  作者: 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值重新插入到数组中。
  • 因此,可以通过直接找到数组的总和并找到总和%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


输出:
1

时间复杂度: O(N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。