📌  相关文章
📜  反复重复删除奇数和偶数索引元素后,找到最后剩余的元素

📅  最后修改于: 2021-04-23 22:25:50             🧑  作者: Mango

给定正整数N ,任务是按给定顺序重复执行以下操作后,打印序列[1,N]中的最后一个剩余元素:

  1. 从序列中删除所有奇数索引元素。
  2. 从序列中删除所有偶数索引元素。

例子:

天真的方法:最简单的方法是将所有从1N的元素顺序存储在一个数组中。对于每个操作,从数组中删除元素,然后将剩余的元素向左移动。将数组简化为单个元素后,将其余元素打印为所需答案。

时间复杂度: O(N 2 * log N)
辅助空间: O(N)

高效方法:可以使用动态编程来优化上述方法。
递归关系如下:

请按照以下步骤解决问题:

  1. 初始化数组dp [] ,其中dp [i]存储剩余元素或序列[1,i]
  2. 对于i = 1的基本条件,请打印1作为所需答案。
  3. 使用上述递归关系来计算dp [N]的值,并使用已经计算的子问题来避免重新计算重叠的子问题。
  4. 完成上述步骤后,打印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)