📌  相关文章
📜  查找数组中最后删除的元素的位置

📅  最后修改于: 2021-04-25 00:44:13             🧑  作者: Mango

给定大小数组N和一个整数M 。在给定的数组上执行以下操作:

  1. 如果a [i]> M,则将a [i] – M推到数组的末尾,否则将其从数组中删除。
  2. 数组为非空时执行第一个操作。

任务是找到最后移除的元素的原始位置。

例子:

这个想法是观察将要从数组中删除的最后一个元素。可以很容易地说,最后要删除的元素是可以减去最大次数的元素。 M在数组的所有元素之间。即,具有ceil(a [i] / M)最大值的元素。

因此,现在任务减少了,以找到具有ceil(a [i] / M)最大值的数组中元素的索引。

下面是上述方法的实现:

C++
// C++ program to find the position of the
// last removed element from the array
#include 
using namespace std;
  
// Function to find the original position
// of the element which will be
// removed last
int getPosition(int a[], int n, int m)
{
    // take ceil of every number
    for (int i = 0; i < n; i++) {
        a[i] = (a[i] / m + (a[i] % m != 0));
    }
  
    int ans = -1, max = -1;
    for (int i = n - 1; i >= 0; i--) {
        if (max < a[i]) {
            max = a[i];
            ans = i;
        }
    }
  
    // Since position is index+1
    return ans + 1;
}
  
// Driver code
int main()
{
    int a[] = { 2, 5, 4 };
  
    int n = sizeof(a) / sizeof(a[0]);
  
    int m = 2;
  
    cout << getPosition(a, n, m);
  
    return 0;
}


Java
// Java program to find the position of the
// last removed element from the array
import java.util.*;
  
class solution
{
  
// Function to find the original position
// of the element which will be
// removed last
  
static int getPosition(int a[], int n, int m)
{
    // take ceil of every number
    for (int i = 0; i < n; i++) {
        a[i] = (a[i] / m + (a[i] % m));
    }
  
    int ans = -1, max = -1;
    for (int i = n - 1; i >= 0; i--) {
        if (max < a[i]) {
            max = a[i];
            ans = i;
        }
    }
  
    // Since position is index+1
    return ans + 1;
}
  
// Driver code
public static void main(String args[])
{
    int a[] = { 2, 5, 4 };
  
    int n = a.length;
  
    int m = 2;
  
System.out.println(getPosition(a, n, m));
  
}
  
}
//This code is contributed by
// Surendra_Gangwar


Python3
# Python3 program to find the position of 
# the last removed element from the array
import math as mt
  
# Function to find the original 
# position of the element which 
# will be removed last
def getPosition(a, n, m):
  
    # take ceil of every number
    for i in range(n):
        a[i] = (a[i] // m + 
               (a[i] % m != 0))
      
    ans, maxx = -1,-1
    for i in range(n - 1, -1, -1):
        if (maxx < a[i]):
            maxx = a[i]
            ans = i
              
    # Since position is index+1
    return ans + 1
  
# Driver code
a = [2, 5, 4]
  
n = len(a)
  
m = 2
  
print(getPosition(a, n, m))
  
# This is contributed by Mohit kumar 29


C#
// C# program to find the position of the
// last removed element from the array
using System;
  
class GFG
{
      
// Function to find the original 
// position of the element which 
// will be removed last
static int getPosition(int []a, 
                       int n, int m)
{
    // take ceil of every number
    for (int i = 0; i < n; i++)
    {
        a[i] = (a[i] / m + (a[i] % m));
    }
  
    int ans = -1, max = -1;
    for (int i = n - 1; i >= 0; i--)
    {
        if (max < a[i]) 
        {
            max = a[i];
            ans = i;
        }
    }
  
    // Since position is index+1
    return ans + 1;
}
  
// Driver code
static public void Main ()
{
    int []a = { 2, 5, 4 };
    int n = a.Length;
    int m = 2;
    Console.WriteLine(getPosition(a, n, m));
}
}
  
// This code is contributed by ajit


PHP
= 0; $i--) 
    {
        if ($max < $a[$i]) 
        {
            $max = $a[$i];
            $ans = $i;
        }
    }
  
    // Since position is index+1
    return $ans + 1;
}
  
// Driver code
$a = array( 2, 5, 4 );
$n = sizeof($a);
$m = 2;
  
echo getPosition($a, $n, $m);
  
// This code is contributed by jit_t
?>


输出:
2