-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathextractEachKth.R
More file actions
executable file
·37 lines (35 loc) · 908 Bytes
/
extractEachKth.R
File metadata and controls
executable file
·37 lines (35 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Given array of integers, remove each kth element from it.
#
# Example
#
# For inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and k = 3, the output should be
# extractEachKth(inputArray, k) = [1, 2, 4, 5, 7, 8, 10].
#
# Input/Output
#
# [execution time limit] 5 seconds (r)
#
# [input] array.integer inputArray
#
# Guaranteed constraints:
# 5 ≤ inputArray.length ≤ 15,
# -20 ≤ inputArray[i] ≤ 20.
#
# [input] integer k
#
# Guaranteed constraints:
# 1 ≤ k ≤ 10.
#
# [output] array.integer
#
# inputArray without elements k - 1, 2k - 1, 3k - 1 etc.
inputArray = list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
k = 3
extractEachKth <- function(inputArray, k) {
inputArray <- unlist(inputArray)
if (k > length(inputArray)) {
return(inputArray)
}
indicestoremove = seq(from = k, to = length(inputArray), by = k)
return(inputArray[!(seq(1:length(inputArray)) %in% indicestoremove)])
}