给定正整数N ,任务是按给定顺序重复执行以下操作后,打印序列[1,N]中的最后一个剩余元素:
- 从序列中删除所有奇数索引元素。
- 从序列中删除所有偶数索引元素。
例子:
Input: N = 9
Output: 6
Explanation:
Sequence = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Step 1: Removing odd-indexed elements modifies sequence to {2, 4, 6, 8}
Step 2: Removing even-indexed elements modifies sequence to {2, 6}
Step 3: Removing odd-indexed elements modifies sequence to {6}
Therefore, the last remaining element is 6.
Input: N = 5
Output: 2
Explanation:
Sequence = {1, 2, 3, 4, 5}
Step 1: Removing odd-indexed elements modifies sequence to {2, 4}
Step 2: Removing even-indexed elements modifies sequence to {2}
Therefore, the last remaining element is 2.
天真的方法:最简单的方法是将所有从1到N的元素顺序存储在一个数组中。对于每个操作,从数组中删除元素,然后将剩余的元素向左移动。将数组简化为单个元素后,将其余元素打印为所需答案。
时间复杂度: O(N 2 * log N)
辅助空间: O(N)
高效方法:可以使用动态编程来优化上述方法。
递归关系如下:
where, i is in the range [1, N]
dp[i] stores the answer when the array elements are from 1 to i.
请按照以下步骤解决问题:
- 初始化数组dp [] ,其中dp [i]存储剩余元素或序列[1,i] 。
- 对于i = 1的基本条件,请打印1作为所需答案。
- 使用上述递归关系来计算dp [N]的值,并使用已经计算的子问题来避免重新计算重叠的子问题。
- 完成上述步骤后,打印dp [N]的值作为结果。
下面是上述方法的实现:
C++14
// C++14 program for the above approach
#include
using namespace std;
// Function to calculate the last
// remaining element from the sequence
int lastRemaining(int n, map &dp)
{
// If dp[n] is already calculated
if (dp.find(n) != dp.end())
return dp[n];
// Base Case:
if (n == 1)
return 1;
// Recursive call
else
dp[n] = 2 * (1 + n / 2 -
lastRemaining(n / 2, dp));
// Return the value of dp[n]
return dp[n];
}
// Driver Code
int main()
{
// Given N
int N = 5;
// Stores the
map dp;
// Function call
cout << lastRemaining(N, dp);
return 0;
}
// This code is contributed by mohit kumar 29
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function to calculate the last
// remaining element from the sequence
static int lastRemaining(int n, HashMap dp)
{
// If dp[n] is already calculated
if (dp.containsKey(n))
return dp.get(n);
// Base Case:
if (n == 1)
return 1;
// Recursive call
else
dp.put(n, 2 * (1 + n / 2 -
lastRemaining(n / 2, dp)));
// Return the value of dp[n]
return dp.get(n);
}
// Driver Code
public static void main(String[] args)
{
// Given N
int N = 5;
// Stores the
HashMap dp = new HashMap();
// Function call
System.out.print(lastRemaining(N, dp));
}
}
// This code is contributed by Princi Singh
Python3
# Python program for the above approach
# Function to calculate the last
# remaining element from the sequence
def lastRemaining(n, dp):
# If dp[n] is already calculated
if n in dp:
return dp[n]
# Base Case:
if n == 1:
return 1
# Recursive Call
else:
dp[n] = 2*(1 + n//2
- lastRemaining(n//2, dp))
# Return the value of dp[n]
return dp[n]
# Driver Code
# Given N
N = 5
# Stores the
dp = {}
# Function Call
print(lastRemaining(N, dp))
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate the last
// remaining element from the sequence
static int lastRemaining(int n, Dictionary dp)
{
// If dp[n] is already calculated
if (dp.ContainsKey(n))
return dp[n];
// Base Case:
if (n == 1)
return 1;
// Recursive call
else
dp.Add(n, 2 * (1 + n / 2 -
lastRemaining(n / 2, dp)));
// Return the value of dp[n]
return dp[n];
}
// Driver Code
public static void Main(String[] args)
{
// Given N
int N = 5;
// Stores the
Dictionary dp = new Dictionary();
// Function call
Console.Write(lastRemaining(N, dp));
}
}
// This code is contributed by Princi Singh
2
时间复杂度: O(N)
辅助空间: O(N)