Computer Science, asked by TbiaSamishta, 1 year ago

amy has an array nums of n positive integers and another array maxes of m positive integers. for each maxesi in maxes she wants to know the total number of elements in nums which are less than or equal to that maxesi. for example if nums = [1 2 3] and maxes = [2 4] then there are 2 elements in nums that are √ҐвА∞¬§ maxes0 (which is 2) and 3 elements in nums that are √ҐвА∞¬§ maxes1 (which is 4). we can store these respective answers in another array answer = [2 3].

Answers

Answered by aqibkincsem
0

"It has two parameters: An array, nums, of n positive integers. An array, maxes, of m positive integers. The function must return an array of m positive integers in which the integer at each index i (where 0 ≤ i < m) denotes the total number of elements numsj (where 0 ≤ j < n) satisfying numsj ≤ maxesi. We are given, n = 5, nums = [2, 10, 5, 4, 8], m = 4, and maxes = [3, 1, 7, 8]. For maxes0 = 3, we have one element in nums (nums0 = 2) that is ≤ maxes0. For maxes1 = 1, there are zero elements in nums that are ≤ maxes1. For maxes2 = 7, we have three elements in nums (nums0 = 2, nums2 = 5, and nums3 = 4) that are ≤ maxes2. For maxes3 = 8, we have four elements in nums (nums0 = 2, nums2 = 5, nums3 = 4, and nums4 = 8) that are ≤ maxes3. Thus, the function returns the array [1, 0, 3, 4] as the answer.

#!/bin/python



import sys


import os



# Complete the function below.



def counts(nums, maxes):


index_to_maxes = {i: m for i, m in enumerate(maxes)}


maxes_to_count = {m: 0 for m in maxes}



maxes.sort()


nums.sort()



num_index = 0


max_index = 0


count = 0


while num_index < len(nums) and max_index < len(maxes):


num_value = nums[num_index]


max_value = maxes[max_index]



if num_value <= max_value:


count += 1


else:


maxes_to_count[max_value] = count


max_index += 1


continue


num_index += 1



while max_index < len(maxes):


max_value = maxes[max_index]




maxes_to_count[max_value] = count


max_index += 1






answer = [-1] * len(maxes)


for i, m in index_to_maxes.iteritems():


answer[i] = maxes_to_count[m]




return answer






f = open(os.environ['OUTPUT_PATH'], 'w')






_nums_cnt = 0


_nums_cnt = int(raw_input())


_nums_i=0


_nums = []


while _nums_i < _nums_cnt:


_nums_item = int(raw_input());


_nums.append(_nums_item)


_nums_i+=1








_maxes_cnt = 0


_maxes_cnt = int(raw_input())


_maxes_i=0


_maxes = []


while _maxes_i < _maxes_cnt:


_maxes_item = int(raw_input());


_maxes.append(_maxes_item)


_maxes_i+=1






res = counts(_nums, _maxes)


for res_cur in res:


f.write( str(res_cur) + ""\n"" )



       

f.close()

""It has two parameters: An array, nums, of n positive integers. An array, maxes, of m positive integers. The function must return an array of m positive integers in which the integer at each index i (where 0 ≤ i < m) denotes the total number of elements numsj (where 0 ≤ j < n) satisfying numsj ≤ maxesi. We are given, n = 5, nums = [2, 10, 5, 4, 8], m = 4, and maxes = [3, 1, 7, 8]. For maxes0 = 3, we have one element in nums (nums0 = 2) that is ≤ maxes0. For maxes1 = 1, there are zero elements in nums that are ≤ maxes1. For maxes2 = 7, we have three elements in nums (nums0 = 2, nums2 = 5, and nums3 = 4) that are ≤ maxes2. For maxes3 = 8, we have four elements in nums (nums0 = 2, nums2 = 5, nums3 = 4, and nums4 = 8) that are ≤ maxes3. Thus, the function returns the array [1, 0, 3, 4] as the answer.

#!/bin/python



import sys


import os



# Complete the function below.



def counts(nums, maxes):


index_to_maxes = {i: m for i, m in enumerate(maxes)}


maxes_to_count = {m: 0 for m in maxes}



maxes.sort()


nums.sort()



num_index = 0


max_index = 0


count = 0


while num_index < len(nums) and max_index < len(maxes):


num_value = nums[num_index]


max_value = maxes[max_index]



if num_value <= max_value:


count += 1


else:


maxes_to_count[max_value] = count


max_index += 1


continue


num_index += 1



while max_index < len(maxes):


max_value = maxes[max_index]




maxes_to_count[max_value] = count


max_index += 1






answer = [-1] * len(maxes)


for i, m in index_to_maxes.iteritems():


answer[i] = maxes_to_count[m]




return answer






f = open(os.environ['OUTPUT_PATH'], 'w')






_nums_cnt = 0


_nums_cnt = int(raw_input())


_nums_i=0


_nums = []


while _nums_i < _nums_cnt:


_nums_item = int(raw_input());


_nums.append(_nums_item)


_nums_i+=1








_maxes_cnt = 0


_maxes_cnt = int(raw_input())


_maxes_i=0


_maxes = []


while _maxes_i < _maxes_cnt:


_maxes_item = int(raw_input());


_maxes.append(_maxes_item)


_maxes_i+=1






res = counts(_nums, _maxes)


for res_cur in res:


f.write( str(res_cur) + ""\n"" )



       

f.close()

"

Similar questions