Given an unsorted array of unique elements, find the position of a specified elements in array’s sorted form.
QUICK EXPLANATION:
Count the number of elements in an unsorted array which are smaller than the given element.
EXPLANATION:
Since the length of all songs is unique, if we count the number of songs which have length less than the length of “Uncle Johny”, we know its position in sorted array.
Constraint on the value of N in this problem is very low so many contestants sorted the array and then did binary search for the length of “Uncle Johny” song in the sorted array. This approach also passes well within time limit.
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution will be uploaded soon
Tester’s solution can be found here and here
Vlad enjoys listening to music. He lives in Sam’s Town. A few days ago he had a birthday, so his parents gave him a gift: MP3-player! Vlad was the happiest man in the world! Now he can listen his favorite songs whenever he wants!
Vlad built up his own playlist. The playlist consists of N songs, each has a unique positive integer length. Vlad likes all the songs from his playlist, but there is a song, which he likes more than the others. It’s named “Uncle Johny”.
After creation of the playlist, Vlad decided to sort the songs in increasing order of their lengths. For example, if the lengths of the songs in playlist was {1, 3, 5, 2, 4} after sorting it becomes {1, 2, 3, 4, 5}. Before the sorting, “Uncle Johny” was on K-th position (1-indexing is assumed for the playlist) in the playlist.
Vlad needs your help! He gives you all the information of his playlist. Your task is to find the position of “Uncle Johny” in the sorted playlist.
#include #include
using namespace std;
void fastscan(int &number)
{
//variable to indicate sign of input number
bool negative = false;
register int c;
number = 0;
c = getchar();
if (c=='-')
{
// number is negative
negative = true;
c = getchar();
}
for (; (c>47 && c<58); c=getchar())
number = number *10 + c - 48;
if (negative)
number *= -1;
}
int main()
{
int a;
fastscan(a);
while(a–)
{
int b;
fastscan(b);
int l[b];
for(int i=0;i<b;i++)
{
fastscan(l[i]);
}
int y;
fastscan(y);
int u=l[y-1];int o=0;
for(int i=0;i<b;i++)
{
if(u>=l[i])
{
o++;
}
}
cout<<o+1<<endl;
}
}
#include #include
using namespace std;
void fastscan(int &number)
{
//variable to indicate sign of input number
bool negative = false;
register int c;
number = 0;
c = getchar();
if (c=='-')
{
// number is negative
negative = true;
c = getchar();
}
for (; (c>47 && c<58); c=getchar())
number = number *10 + c - 48;
if (negative)
number *= -1;
}
int main()
{
int a;
fastscan(a);
while(a–)
{
int b;
fastscan(b);
int l[b];
for(int i=0;i<b;i++)
{
fastscan(l[i]);
}
int y;
fastscan(y);
int u=l[y-1];int o=0;
for(int i=0;i<b;i++)
{
if(u>=l[i])
{
o++;
}
}
cout<<o+1<<endl;
}
}
Your algorithm is right, but i don’t understand why it works faster. Your solution consider every element of the array, so author’s solution do. So, i guess it works as fast as author’s one.
Firstly i cant read your code…Then why do you need to go with binary search?
Make the code as simple as that-make linear search.
I am sorry i cant read it completly…
If you provide the proper indentation then i shall try… All the best…
Yes you are correct by saying that it considers every element in the array but notice that it considers it 2 at a time while the authors own considers it 1 at a time leading my own to an O(n/2) algorithm which will be faster asymptotically speaking.
It would be better if you format and use proper indentation while posting codes. Posting the code in a paragraph makes it unreadable and impossible to debug.