You are not logged in. Please login at www.codechef.com to post your questions!

×

JOHNY - Editorial

PROBLEM LINK:

Practice
Contest

DIFFICULTY:

CAKEWALK

PREREQUISITES:

None

PROBLEM:

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

This question is marked "community wiki".

asked 11 Nov '13, 15:03

shaleen's gravatar image

4★shaleen ♦♦
616913
accept rate: 0%

edited 11 Nov '13, 22:17


12next »

Links to author's and tester's solutions not working.

link

answered 11 Nov '13, 22:08

tijoforyou's gravatar image

2★tijoforyou
4.2k52364
accept rate: 15%

You can also use two pointers at both ends to get a faster time

a---is the array of numbers x--- is the number we are trying to find its position after sorting

i=0;j=n-1 while(i<=j) if a[i]<x cnt++ if a[j]<x cnt++ i++,j-- print(cnt+1)

Hope am correct

link

answered 12 Nov '13, 07:09

okekeau's gravatar image

4★okekeau
161
accept rate: 0%

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.

(12 Nov '13, 17:39) kostya_by ♦♦6★

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.

(14 Nov '13, 02:48) okekeau4★

.. dude, believe me, n = (1/2)(n + n), if you know what I mean.

(09 Dec '13, 20:33) sanjay513★

I can't figure out why doesn't it work? https://ideone.com/ftTHzI Seems correct to me.

PS: Nicely formatted, readable code.

link

answered 02 Jun '16, 10:19

jarpit96's gravatar image

2★jarpit96
1
accept rate: 0%

Why is this code giving wrong answer ?

include<cstdio>

using namespace std;

int main() {

int T,N,K,i,song;
long long A[102];
scanf("%d",&T);
while(T--)
{
    scanf("%d",&N);
    for(i=1;i<=N;i++) scanf("%lld",&A[i]);
    scanf("%d",&K);
    song=0;
    for(i=1;i<=N;i++)
    {
        if (A[i]<A[K]) song++;
    }
    printf("%d\n",song);
}
return 0;

}

link
This answer is marked "community wiki".

answered 27 Jul '16, 14:09

adarsh1021's gravatar image

2★adarsh1021
1
accept rate: 0%

edited 27 Jul '16, 14:10

Why this code returns wrong answer??

#include <iostream>

using namespace std;

int main()
{
    int T,N,a[100],K,fav,temp=0;
    cin>>T;
    for(int k=0;k<T;k++){
        cin>>N;
        for(int i=0;i<N;i++){
            cin>>a[i];
        }
        cin>>K;
        fav=a[K-1];
        for(int j=0;j<N;j++){
            for(int k=0;k<N;k++){
             if(a[k]>a[k+1]){
                temp=a[k];
                a[k]=a[k+1];
                a[k+1]=temp;
             }
            }
        }
        for(int m=0;m<N;m++){
            if(fav==a[m]){
                cout<<m+1<<"\n";
            }
        }
    }
    return 0;
}
link

answered 26 Aug '16, 08:17

pratiktsingh17's gravatar image

1★pratiktsingh17
1
accept rate: 0%

The majority of students think that doing their homework is boring. They do everything accept their homework. If you are one of such students, struggling with the study overload and burden of assignment. Dissertation writing service

link

answered 26 Aug '16, 11:22

adam0702's gravatar image

0★adam0702
1
accept rate: 0%

Answer is hidden as author is suspended. Click here to view.

answered 04 Feb '17, 11:56

stewartouis's gravatar image

0★stewartouis
(suspended)
accept rate: 0%

The Class Clown

Easily identifiable, it is the student who is bored and tries to boycott the class by interrupting constantly with questions away from the topic that develops at that moment. Your goal is to get the attention of the class with your comments and jokes so that everyone laughs. He does not allow his classmates to get involved with the lessons or that someone actively participates in the class as he talks to the students coming to his site.

Uk Dissertation Writing Services

link

answered 23 Feb '17, 14:21

aanish's gravatar image

0★aanish
-1
accept rate: 0%

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.

link

answered 25 Apr '17, 01:40

fiery_phoenix0's gravatar image

0★fiery_phoenix0
1
accept rate: 0%

include <stdio.h>

int main(void) { int t,s,p[100],p1,i,j,k,b,u1,lst,fst,mid; scanf("%d",&t); for(i=0;i<t;i++) { scanf("%d",&s); for(j=0;j<s;j++) { scanf("%d",&p[j]); } scanf("%d",&u1); p1=p[u1-1];

      for(k=0;k<s-1;k++)
      {
           for(j=0;j<=s-k-1;j++)
          {
               if(p[j]>p[j+1])
               {
                 b=p[j];
                 p[j]=p[j+1];
                 p[j+1]=b;
               }
           }
      }
        for(j=0;j<s;j++)
     {
       //  printf("%d\t",p[j]);    //sorted
     }
    //  printf("\n");             
      fst=0;
      lst=s-1;
      mid=(fst+lst)/2;
     // printf("%d %d %d %d\n",fst,lst,mid,p1);
      while(fst<=lst)
     {
           if(p[mid]<p1)
              fst=mid+1;
           else if(p[mid]==p1)
           {
               printf("%d\n",(mid+1));
               break;
           }
           else
               lst=mid-1;
           mid=(fst+lst)/2;
      }

}
return 0;

}

why the answer is coming wrong although i am getting correct answer in online ide

link

answered 13 Jun '17, 00:10

subham_1999's gravatar image

2★subham_1999
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,005
×1,512
×15
×3

question asked: 11 Nov '13, 15:03

question was seen: 6,229 times

last updated: 28 Jul, 15:21