Uncle Johny | CodeChef Solution

Hello coders, today we are going to solve Uncle Johny CodeChef Solution whose Problem Code is JOHNY.

Uncle Johny

Task

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.

Input Format

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.The first line of each test case contains one integer N denoting the number of songs in Vlad’s playlist. The second line contains N space-separated integers A1A2, …, AN denoting the lenghts of Vlad’s songs. The third line contains the only integer K – the position of “Uncle Johny” in the initial playlist.

Output Format

For each test case, output a single line containing the position of “Uncle Johny” in the sorted playlist.

Constraints

  • 1 ≤ T ≤ 1000
  • 1 ≤ K ≤ N ≤ 100
  • 1 ≤ Ai ≤ 109

Example

Sample Input

3
4
1 3 4 2
2
5
1 2 3 9 4
5
5
1 2 3 9 4 
1

Sample Output

3
4
1

Explanation

In the example test there are T=3 test cases.

Test case 1

In the first test case N equals to 4, K equals to 2, A equals to {1, 3, 4, 2}. The answer is 3, because {1, 3, 4, 2} -> {1, 2, 3, 4}. A2 now is on the 3-rd position.

Test case 2

In the second test case N equals to 5, K equals to 5, A equals to {1, 2, 3, 9, 4}. The answer is 4, because {1, 2, 3, 9, 4} -> {1, 2, 3, 4, 9}. A5 now is on the 4-th position.

Test case 3

In the third test case N equals to 5, K equals to 1, A equals to {1, 2, 3, 9, 4}. The answer is 1, because {1, 2, 3, 9, 4} -> {1, 2, 3, 4, 9}. A1 stays on the 1-th position.

Solution – Uncle Johny

C++

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t ;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n;
        int arr[n];
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        cin>>k;
        k=k-1;
        int john=arr[k];
        int m = sizeof(arr)/sizeof(arr[0]); 
        sort(arr, arr+n); 
        for(int i=0;i<n;i++)
        {
            if(arr[i]==john)
            cout<<(i+1)<<endl;
        }
        
    }
    return 0;
}

Python

n = int(input())
for x in range(n):
    a = int(input())
    b = list(map(int,input().split()))
    c = int(input())
    d = b[c-1]
    b.sort()
    print(b.index(d)+1)

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (T>0)
        {
            int N = sc.nextInt();
            int[] s = new int[N];
            for (int i = 0; i < N; i++)
                s[i] = sc.nextInt();
            int k = sc.nextInt();
            int element = s[k-1];

            Arrays.sort(s);

            for (int i = 0; i < N; i++)
            {
                if (s[i]==element)
                {
                    System.out.println(i+1);
                    break;
                }
            }

            T--;
        }
	}
}

Disclaimer: The above Problem (Uncle Johny) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *