通过选择对使得 arr[i] >= arr[j] 并将 arr[i] 替换为 arr[i] – arr[j] 来最小化 Array 的最后一个剩余元素
给定一个包含N个正整数的数组arr[] ,任务是在执行任意次数以下操作后,找到数组中最后一个剩余元素的最小可能值:
- 选择一对索引(i, j)使得arr[i] >= arr[j]并将arr[i]替换为arr[i] – arr[j] 。
- 如果数组元素arr[i] <= 0 ,则将其从数组中删除。
例子:
Input: arr[] = {2, 4, 8, 32}
Output: 2
Explanation: In first 4 operations, select (i, j) as (3, 2). Hence, the array after 4 operations will become arr[] = {2, 4, 8, 0}. Here, arr[3] can be removed as arr[3] = 0. Similarly, perform the operation twice for (i, j) = (2, 1). The array after operations is arr[] = {2, 4}. Now perform the given operation twice for (i, j) as (1, 0). The final array will be arr[] = {2}. Therefore, the last remaining element is 2, which is the minimum possible.
Input: arr[] = {5, 13, 8, 10}
Output: 1
方法:给定的问题可以通过以下观察来解决:
- 根据基本欧几里得算法求两个整数 (x, y) 的 GCD,可以推导出GCD(x, y) = GCD(x, y – x) ,如果y > x ,否则, x和y可以简单地交换。直到y – x的值减小到 0,这种关系才会成立。
- 因此,可以得出结论,通过从较小的值中减去较大的值,对(x, y)的最小可达值是GCD(x, y) 。
因此,使用上述观察,所需的答案将是给定数组arr[]的所有元素的 GCD。
下面是上述方法的实现:
C++
// C++ Program os the above approach
#include
using namespace std;
// Function to minimize the last
// remaining element of array
int minValue(int arr[], int n)
{
// Stores the required value
int ans;
// Initialize answer
ans = arr[0];
// Loop to traverse arr[]
for (int i = 1; i < n; i++) {
// Calculate the GCD
ans = __gcd(ans, arr[i]);
}
// Return Answer
return ans;
}
// Driver Code
int main()
{
int arr[] = { 5, 13, 8, 10 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << minValue(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Recursive function to return gcd of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to minimize the last
// remaining element of array
static int minValue(int arr[], int n)
{
// Stores the required value
int ans;
// Initialize answer
ans = arr[0];
// Loop to traverse arr[]
for (int i = 1; i < n; i++) {
// Calculate the GCD
ans = gcd(ans, arr[i]);
}
// Return Answer
return ans;
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 5, 13, 8, 10 };
int N = arr.length;
System.out.println(minValue(arr, N));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python Program os the above approach
def __gcd (a, b):
if (not b):
return a;
return __gcd(b, a % b);
# Function to minimize the last
# remaining element of array
def minValue (arr, n):
# Stores the required value
ans = None
# Initialize answer
ans = arr[0];
# Loop to traverse arr[]
for i in range(1, n):
# Calculate the GCD
ans = __gcd(ans, arr[i]);
# Return Answer
return ans;
# Driver Code
arr = [5, 13, 8, 10];
N = len(arr)
print(minValue(arr, N));
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG {
// Recursive function to return gcd of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to minimize the last
// remaining element of array
static int minValue(int []arr, int n)
{
// Stores the required value
int ans;
// Initialize answer
ans = arr[0];
// Loop to traverse arr[]
for (int i = 1; i < n; i++) {
// Calculate the GCD
ans = gcd(ans, arr[i]);
}
// Return Answer
return ans;
}
// Driver Code
public static void Main () {
int []arr = { 5, 13, 8, 10 };
int N = arr.Length;
Console.Write(minValue(arr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
1
时间复杂度: O(N)
辅助空间: O(1)