I have written a code to loop through, get the inputs and add value to
f1 <- file("stdin")
open(f1)
arr = c(0, 0, 0, 0, 0)
for(i in 1:3){
#user will provide the input 3 times. It will be a space separated input. Each input will have 3 numbers.
#1st will indicate the starting index.
#2nd will indicate the ending index.
#And 3rd will indicate the value to be added between the starting and ending index
inp1 = readLines(f1, n = 1, warn = FALSE)
spl = strsplit(inp1, " ")[[1]]
a = as.numeric(spl[1]) #start index
b = as.numeric(spl[2]) #end index
k = as.numeric(spl[3]) #value to be added
arr[a:b] = arr[a:b] + k
}
arr
Sample Input:
1 3 5
1 3 5
2 4 5
Expected Output:
10 15 15 5 0
Is there a way to improve its performance, may be by eliminating the for loop.