Skip to content

Commit

Permalink
Written the code for largest two elements in an array in R language. c…
Browse files Browse the repository at this point in the history
  • Loading branch information
srini696 authored Feb 2, 2024
1 parent 9847d90 commit 2111f91
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
moveZeroesToEnd <- function(arr) {
nonZeroIndex <- 1

for (i in 1:length(arr)) {
if (arr[i] != 0) {
arr[nonZeroIndex] <- arr[i]
nonZeroIndex <- nonZeroIndex + 1
}
}

for (i in nonZeroIndex:length(arr)) {
arr[i] <- 0
}

return(arr)
}


arr1 <- c(1, 2, 0, 4, 3, 0, 5, 0)
result1 <- moveZeroesToEnd(arr1)
cat("Input : arr[] =", arr1, "\n")
cat("Output : arr[] =", result1, "\n")

arr2 <- c(1, 2, 0, 0, 0, 3, 6)
result2 <- moveZeroesToEnd(arr2)
cat("Input : arr[] =", arr2, "\n")
cat("Output : arr[] =", result2, "\n")

0 comments on commit 2111f91

Please sign in to comment.