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

📅  最后修改于: 2021-09-22 10:23:23             🧑  作者: 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


Javascript


输出:
2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程