Encuentre el número en la tarjeta que queda al final después de realizar las operaciones dadas

Dado un número entero N que representa el número de cartas en una baraja. La baraja se ordena de 1 a N , donde 1 es la carta superior y N la inferior. Sacas la carta superior del mazo y la insertas en la parte inferior y lanzas la siguiente carta que aparece en la parte superior del mazo. Vuelve a hacer lo mismo hasta que quede una sola carta. La tarea es encontrar el número de la tarjeta que queda al final.
Ejemplos: 
 

Input: N = 4 
Output: 1
1 2 3 4
^     ^
Top   Bottom

Operation 1: 3 4 1 (1 got shifted to the bottom and 2 got removed)
Operation 2: 1 3 (3 got shifted and 4 got removed)
Operation 3: 1 (3 got removed after shifting 1)

Input: N = 10
Output: 5

Acercarse: 
 

  1. En primer lugar, inserte los números del 1 al N en una cola .
  2. Ahora, elimine el elemento frontal de la cola y póngalo en cola al final.
  3. Finalmente, haga estallar el elemento en el frente.
  4. Imprime el elemento final que queda en la cola.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that will find the
// card number which is remaining
int remainingCard(int n)
{
    queue<int> queCards;
 
    // Inserting all the numbers from 1 to n
    for (int i = 1; i <= n; i++)
        queCards.push(i);
 
    // While there are atleast two
    // elements in the queue
    while (((int)queCards.size()) >= 2) {
 
        // Push the front element at the back
        queCards.push(queCards.front());
 
        // Remove the front element
        queCards.pop();
 
        // Remove another element
        queCards.pop();
    }
 
    // Return the only element
    // left in the queue
    return queCards.front();
}
 
// Driver code
int main()
{
    int n = 10;
 
    cout << remainingCard(n);
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function that will find the
// card number which is remaining
static int remainingCard(int n)
{
    Queue<Integer> queCards = new LinkedList<>();
 
    // Inserting all the numbers from 1 to n
    for (int i = 1; i <= n; i++)
    {
        queCards.add(i);
    }
 
    // While there are atleast two
    // elements in the queue
    while (((int) queCards.size()) >= 2)
    {
 
        // Push the front element at the back
        queCards.add(queCards.peek());
 
        // Remove the front element
        queCards.remove();
 
        // Remove another element
        queCards.remove();
    }
 
    // Return the only element
    // left in the queue
    return queCards.peek();
}
 
// Driver code
public static void main(String[] args)
{
    int n = 10;
 
    System.out.println(remainingCard(n));
}
}
 
// This code is contributed by PrinciRaj1992

Python3

# Python3 implementation of the approach
 
# Function that will find the
# card number which is remaining
def remainingCard(n):
 
    queCards = []
 
    # Inserting all the numbers from 1 to n
    for i in range(1, n + 1):
        queCards.append(i)
 
    # While there are atleast two
    # elements in the queue
    while (len(queCards) >= 2):
 
        # Push the front element at the back
        queCards.append(queCards[0]);
 
        # Remove the front element
        queCards.pop(0);
 
        # Remove another element
        queCards.pop(0);
 
    # Return the only element
    # left in the queue
    return queCards[0]
 
# Driver code
n = 10
print(remainingCard(n))
 
# This code is contributed by divyamohan123

C#

// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function that will find the
// card number which is remaining
static int remainingCard(int n)
{
    Queue<int> queCards = new Queue<int>();
 
    // Inserting all the numbers from 1 to n
    for (int i = 1; i <= n; i++)
    {
        queCards.Enqueue(i);
    }
 
    // While there are atleast two
    // elements in the queue
    while (((int) queCards.Count) >= 2)
    {
 
        // Push the front element at the back
        queCards.Enqueue(queCards.Peek());
 
        // Remove the front element
        queCards.Dequeue();
 
        // Remove another element
        queCards.Dequeue();
    }
 
    // Return the only element
    // left in the queue
    return queCards.Peek();
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 10;
 
    Console.WriteLine(remainingCard(n));
}
}
 
// This code is contributed by Princi Singh

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function that will find the
// card number which is remaining
function remainingCard(n)
{
    queCards = [];
 
    // Inserting all the numbers from 1 to n
    for (var i = 1; i <= n; i++)
        queCards.push(i);
 
    // While there are atleast two
    // elements in the queue
    while ((queCards.length) >= 2) {
 
        // Push the front element at the back
        queCards.push(queCards[0]);
 
        // Remove the front element
        queCards.shift();
 
        // Remove another element
        queCards.shift();
    }
 
    // Return the only element
    // left in the queue
    return queCards[0];
}
 
// Driver code
var n = 10;
document.write( remainingCard(n));
 
</script>
Producción: 

5

 

Publicación traducida automáticamente

Artículo escrito por Naman_Garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *