给定一个由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[]的总和。
请按照以下步骤解决问题:
- 在使用帕斯卡三角形计算N C r后,遍历数组arr[]并将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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live