Skip to content

Instantly share code, notes, and snippets.

@rjsnh1522
Created March 6, 2018 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjsnh1522/937e4e6a1b28391ba0365793dc1143e8 to your computer and use it in GitHub Desktop.
Save rjsnh1522/937e4e6a1b28391ba0365793dc1143e8 to your computer and use it in GitHub Desktop.
Maximum Of K- size subarrays (Deque)
# question
# quertion url
# https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/maximum-of-k-size-subarrays-deque/
# Given an array A of size 'N' and an integer k, find the maximum for each and every contiguous subarray of size k.
# Input :
# First line contains 2 space separated integers 'N' and 'k' .
# Second line contains 'N' space separated integers denoting array elements.
# Output:
# Space separated Maximum of all contiguous sub arrays of size k.
# Constraints :
# 1<= N <=10^5
# 1<= Ai <=10^9
# 1<= k <=N
# # sample input
# 9 3
# 1 2 3 1 4 5 2 3 6
# sample output
# 3 3 4 5 5 5 6
number_of_elements,size_of_sub_array = gets.chomp.split(' ').map {|d| d.to_i}
array_inputs = gets.chomp.split(' ').map {|d| d.to_i}
start = 0
arr = []
sz = array_inputs.size
while start <= (sz - size_of_sub_array)
arr << array_inputs[start,size_of_sub_array].sort[-1]
start += 1
end
puts arr.join(' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment