给定一个由N个整数组成的数组arr [] ,任务是在从下一个相邻元素中减去每个元素并重复删除最后一个数组元素后,找到剩余的数组元素。
例子:
Input: arr[] = {3, 4, 2, 1}
Output: 4
Explanation:
Operation 1: The array arr[] modifies to {4 – 3, 2 – 4, 1 – 2} = {1, -2, -1}.
Operation 2: The array arr[] modifies to {-2 – 1, -1 + 2} = {-3, 1}.
Operation 3: The array arr[] modifies to {1 + 3} = {4}.
Therefore, the last remaining array element is 4.
Input: arr[] = {1, 8, 4}
Output: -11
Explanation:
Operation 1: The array arr[] modifies to {1 – 8, 4 – 8} = {7, -4}.
Operation 2: The array arr[] modifies to {-4 – 7 } = {-11}.
Therefore, the last remaining array element is -11.
天真的方法:最简单的方法是遍历数组直到其大小减小到1并在数组上执行给定的操作。完成遍历后,打印其余元素。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
- 假设给定数组为arr [] = {a,b,c,d} 。然后,执行操作:
- 现在,假设数组arr [] = {a,b,c,d,e} 。然后,执行操作:
- 从以上两个观察结果可以得出结论,答案是在(x-y) (N-1)和每个数组元素arr [i]的展开中项系数相乘的总和。
- 因此,这个想法是在将每个数组元素更新为(arr [i] * (N – 1) C (i-1) *(-1) i )之后,找到数组arr []的总和。
请按照以下步骤解决问题:
- 遍历数组arr []并在使用Pascal三角计算出N C r之后,将arr [i]更新为arr [i] = arr [i] * (N – 1) C (i – 1) *( -1 ) i 。
- 打印数组arr []的总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function to find the last remaining
// array element after performing
// the given operations repeatedly
int lastElement(const int arr[], int n)
{
// Stores the resultant sum
int sum = 0;
int multiplier = n % 2 == 0 ? -1 : 1;
// Traverse the array
for (int i = 0; i < n; i++) {
// Increment sum by arr[i]
// * coefficient of i-th term
// in (x - y) ^ (N - 1)
sum += arr[i] * multiplier;
// Update multiplier
multiplier
= multiplier * (n - 1 - i)
/ (i + 1) * (-1);
}
// Return the resultant sum
return sum;
}
// Driver Code
int main()
{
int arr[] = { 3, 4, 2, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << lastElement(arr, N);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Function to find the last remaining
// array element after performing
// the given operations repeatedly
public static int lastElement(int arr[], int n)
{
// Stores the resultant sum
int sum = 0;
int multiplier = n % 2 == 0 ? -1 : 1;
// Traverse the array
for (int i = 0; i < n; i++) {
// Increment sum by arr[i]
// * coefficient of i-th term
// in (x - y) ^ (N - 1)
sum += arr[i] * multiplier;
// Update multiplier
multiplier
= multiplier * (n - 1 - i) / (i + 1) * (-1);
}
// Return the resultant sum
return sum;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 4, 2, 1 };
int N = 4;
System.out.println(lastElement(arr, N));
}
}
// This code is contributed by aditya7409.
Python3
# Python 3 program for the above approach
# Function to find the last remaining
# array element after performing
# the given operations repeatedly
def lastElement(arr, n):
# Stores the resultant sum
sum = 0
if n % 2 == 0:
multiplier = -1
else:
multiplier = 1
# Traverse the array
for i in range(n):
# Increment sum by arr[i]
# * coefficient of i-th term
# in (x - y) ^ (N - 1)
sum += arr[i] * multiplier
# Update multiplier
multiplier = multiplier * (n - 1 - i) / (i + 1) * (-1)
# Return the resultant sum
return sum
# Driver Code
if __name__ == '__main__':
arr = [3, 4, 2, 1]
N = len(arr)
print(int(lastElement(arr, N)))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the last remaining
// array element after performing
// the given operations repeatedly
public static int lastElement(int[] arr, int n)
{
// Stores the resultant sum
int sum = 0;
int multiplier = n % 2 == 0 ? -1 : 1;
// Traverse the array
for (int i = 0; i < n; i++) {
// Increment sum by arr[i]
// * coefficient of i-th term
// in (x - y) ^ (N - 1)
sum += arr[i] * multiplier;
// Update multiplier
multiplier
= multiplier * (n - 1 - i) / (i + 1) * (-1);
}
// Return the resultant sum
return sum;
}
// Driver code
static void Main()
{
int[] arr = { 3, 4, 2, 1 };
int N = 4;
Console.WriteLine(lastElement(arr, N));
}
}
// This code is contributed by susmitakundugoaldanga.
Javascript
4
时间复杂度: O(N)
辅助空间: O(1)