重复删除完美平方索引处的 Array 元素后剩余的最后一个元素
给定一个由N个整数组成的数组arr[] (基于 1 的索引),任务是在重复删除完美平方索引处的数组元素后找到最后一个元素。
例子:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 5
Explanation:
Following the removal of array element at perfect square indices that are performed:
- Removing array elements at indices 1 and 4 modifies the array to {2, 3, 5}.
- Removing array elements at indices 1 modifies the array to {3, 5}.
- Removing array elements at indices 1 modifies the array to {5}.
After performing the above operations, the array element remains is 5. Therefore, print 5.
Input: arr[] = {2, 3, 4, 4, 2, 4, -3, 1, 1}
Output: -3
朴素的方法:给定的问题可以通过删除完美平方索引处的数组元素,然后将所有元素复制到新数组来解决。继续执行此步骤,直到数组中只剩下一个元素。完成上述步骤后,打印剩下的最后一个元素。
时间复杂度: O(N 2 )
辅助空间: O(N)
有效方法:上述方法也可以通过查找潜在的最后剩余数组元素来优化,直到该元素不存在于完美平方索引处。请按照以下步骤解决给定的问题:
- 初始化一个变量,比如ans ,它在执行给定操作后存储最后剩余的索引。
- 迭代直到N的值大于1并执行以下步骤:
- 找出可以丢弃的元素的数量,比如D as sqrt(N) 。
- 如果D的平方为N ,则N不能是最后一个剩余元素,因为它被删除了。因此,将ans的值减1作为下一个潜在的剩余索引。
- 将N的值减少D 。
- 完成上述步骤后,将索引(D – 1)处的元素打印为剩余的可能元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find last remaining index
// after repeated removal of perfect
// square indices
int findRemainingIndex(int N)
{
// Initialize the ans variable as N
int ans = N;
// Iterate a while loop and discard
// the possible values
while (N > 1) {
// Total discarded values
int discard = int(sqrt(N));
// Check if this forms a
// perfect square
if (discard * discard == N) {
// Decrease answer by 1
ans--;
}
// Subtract the value from
// the current value of N
N -= discard;
}
// Return the value remained
return ans;
}
// Function to find last remaining element
// after repeated removal of array element
// at perfect square indices
void findRemainingElement(int arr[], int N)
{
// Find the remaining index
int remainingIndex = findRemainingIndex(N);
// Print the element at that
// index as the result
cout << arr[remainingIndex - 1];
}
// Driver Code
signed main()
{
int arr[] = { 2, 3, 4, 4, 2, 4, -3, 1, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
findRemainingElement(arr, N);
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
import java.lang.*;
import java.lang.Math;
class GFG {
// Function to find last remaining index
// after repeated removal of perfect
// square indices
static int findRemainingIndex(int N)
{
// Initialize the ans variable as N
int ans = N;
// Iterate a while loop and discard
// the possible values
while (N > 1) {
// Total discarded values
int discard = (int)(Math.sqrt(N));
// Check if this forms a
// perfect square
if (discard * discard == N) {
// Decrease answer by 1
ans--;
}
// Subtract the value from
// the current value of N
N -= discard;
}
// Return the value remained
return ans;
}
// Function to find last remaining element
// after repeated removal of array element
// at perfect square indices
static void findRemainingElement(int arr[], int N)
{
// Find the remaining index
int remainingIndex = findRemainingIndex(N);
// Print the element at that
// index as the result
System.out.print(arr[remainingIndex - 1]);
}
// Driver Code
public static void main(String[] args)
{
// Given input
int arr[] = { 2, 3, 4, 4, 2, 4, -3, 1, 1 };
int N = 9;
findRemainingElement(arr, N);
}
}
// This code is contributed by dwivediyash
Python3
# Python 3 program for the above approach
from math import sqrt
# Function to find last remaining index
# after repeated removal of perfect
# square indices
def findRemainingIndex(N):
# Initialize the ans variable as N
ans = N
# Iterate a while loop and discard
# the possible values
while (N > 1):
# Total discarded values
discard = int(sqrt(N))
# Check if this forms a
# perfect square
if (discard * discard == N):
# Decrease answer by 1
ans -= 1
# Subtract the value from
# the current value of N
N -= discard
# Return the value remained
return ans
# Function to find last remaining element
# after repeated removal of array element
# at perfect square indices
def findRemainingElement(arr, N):
# Find the remaining index
remainingIndex = findRemainingIndex(N)
# Print the element at that
# index as the result
print(arr[remainingIndex - 1])
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 4, 4, 2, 4, -3, 1, 1]
N = len(arr)
findRemainingElement(arr, N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find last remaining index
// after repeated removal of perfect
// square indices
static int findRemainingIndex(int N)
{
// Initialize the ans variable as N
int ans = N;
// Iterate a while loop and discard
// the possible values
while (N > 1) {
// Total discarded values
int discard = (int)(Math.Sqrt(N));
// Check if this forms a
// perfect square
if (discard * discard == N) {
// Decrease answer by 1
ans--;
}
// Subtract the value from
// the current value of N
N -= discard;
}
// Return the value remained
return ans;
}
// Function to find last remaining element
// after repeated removal of array element
// at perfect square indices
static void findRemainingElement(int[] arr, int N)
{
// Find the remaining index
int remainingIndex = findRemainingIndex(N);
// Print the element at that
// index as the result
Console.Write(arr[remainingIndex - 1]);
}
// Driver Code
public static void Main()
{
// Given input
int[] arr = { 2, 3, 4, 4, 2, 4, -3, 1, 1 };
int N = 9;
findRemainingElement(arr, N);
}
}
// This code is contributed by code_hunt.
Javascript
输出:
-3
时间复杂度: O(sqrt(N))
辅助空间: O(1)