1 Problem


Given an array with k distinct elements, write a function to return all elements that have at least two elements greater than themselves in the same array. For example, given the following array [2, 3, 9, 7, 6], your function should return [2, 3, 6].

2 Resolving Problem


2.1 In R

library(tidyverse) # Includes purrr package
library(reticulate) # Interface to Python

vector <- c(2, 3, 9, 7, 6)

min_elem_r <- function(vector) {
  aux <- c() # auxiliary vector 
  
  for (i in 1:length(vector)) {
    comp <- sum(vector[i] < vector[-i]) # amount of elements greater than i
    if (comp >= 2) {
      aux[length(aux) + 1] <- vector[i] 
    }
  }
  
  return(aux)
}

# Applying R function
min_elem_r(vector)
## [1] 2 3 6

2.2 In Python

import numpy as np

# Accessing an R object in Python
# r.vector = [2, 3, 9, 7, 6]
np.array(r.vector) 
## array([2., 3., 9., 7., 6.])
array = np.array([2, 3, 9, 7, 6])

# List comprehension
def min_elem_py(array):
  return [array[i] for i in range(0, len(array)) if sum(array[i] < array) >= 2]

# Applying Python function
min_elem_py(array)
## [2, 3, 6]

2.3 Example in Python

array = np.array([1, 4, 7, 8, 11, 15, 30])

min_elem_py(array)
## [1, 4, 7, 8, 11]

2.4 Example in R

# Accessing an Python object in R
vector <- py$array

min_elem_r(vector)
## [1]  1  4  7  8 11