A
A
asdsdsdafsdafdsaf2021-11-29 12:21:36
Algorithms
asdsdsdafsdafdsaf, 2021-11-29 12:21:36

How to find the sum of numbers at odd positions after sorting an array?

Given an array of N numbers, how to find the sum of those numbers that will be in odd places after sorting the array?
It is clear that you can sort the array and walk through it again, but how to do it faster?
The task itself:
61a49d6fe6d7d414914034.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wataru, 2021-11-29
@wataru

No way. There are methods to get a number that will stand at a given position (or numbers on a segment) in a sorted array faster than sorting (QuickSelect algorithm). It is impossible to find all the numbers in odd positions somehow faster than sorting.
Edit: If standard sorting doesn't work and L is large, then it's worth looking into radix sort. If L were relatively small, then counting sort would be the best option.

A
Alexandroppolus, 2021-11-29
@Alexandroppolus

There are suspicions that the task did not appear out of nowhere.
In fact, we have only 4 input parameters here.
Theoretically, this sum should be calculated in O(1).
Try to calculate on different sets of parameters, find some patterns in the results...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question