给定大小数组和一个整数 。在给定的数组上执行以下操作:
- 如果a [i]> M,则将a [i] – M推到数组的末尾,否则将其从数组中删除。
- 数组为非空时执行第一个操作。
任务是找到最后移除的元素的原始位置。
例子:
Input: arr[] = {4, 3}, M = 2
Output: 2
Remove 4 from the array and the array becomes {3, 2} with original positions {2, 1}
Remove 3 from the array and the array becomes {2, 1} with original positions {1, 2}
Remove 2 from the array and the array becomes {1} with original positions {2}
So, 2nd positioned element is the last to be removed from the array.
Input: arr[] = {2, 5, 4}, M = 2
Output: 2
这个想法是观察将要从数组中删除的最后一个元素。可以很容易地说,最后要删除的元素是可以减去最大次数的元素。 在数组的所有元素之间。即,具有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
?>
Javascript
2
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。