C Programming – HackerRank Solution | Sam Height insert sorting |

(HackerRank) Write a Modular C Programming code to solve Sam Height insert sorting

Sam among his friends wants to go to watch a movie in Armstord Cinema.
There is something special about Armstord cinema whenever people come in the group here. They will get seats accordingly their heights. Sam as a curious guy always wants to sit in the middle as cinema has the best view from the middle.

Now, Sam as the leader of his group decides who will join him for the movie.
Initially, he has N−1 friends with him (N including him).
You are given N−1 numbers that represent the heights of Sam’s friends.
You are given the height of Sam as well.

Now, Sam can do two operations:
1. He can call a new friend of height H.
2. He can cancel any of his friend invitations.

Each operation will cost him a unit time.
He wants to do this as soon as possible.

Input format

  • The first line contains T, where T is the test case.
  • Each test case contains two lines,
    • The first line contains two space-separated integer N, S where N is the total number of Sam’s friend and S is Sam height.
    • The second line contains N space-separated integers that represent the height of Sam’s friend.

Output format
Print the required answer for each test case in a new line.

Constraints
1≤T≤100

1≤N≤105

1≤Ar[i]≤109

Note:

  • Sam should always sit in middle and there’s an equal number of persons in his left and right.
  • Height of Sam is always unique.      

 

 

Sample Input

2

3 2

4 3 1

1 5

6

Sample Output

1

1

 

Explanation

In first test case:
We can cancel invitation of person of height 4 (Cost = 1)
In second Test Case:
We can invite person with height 4 (Cost =1)

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void sort(int a[1000],int n)
{

    int temp;

    for(int i=0;i<n-1;i++){
        for(int j=0;j<n-i-1;j++) { if(a[j]>a[j+1]){
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;

        }
        }
    }
}

main()
{
    int t,n,h,a[1000],i;
    scanf("%d",&t);
   while(t--)
   {
       scanf("%d %d",&n,&h);
       for(i=0;i<n;i++)
       {
           scanf("%d",&a[i]);
       }
       sort(a,n);
       int z=0;
       i=0;

           while(a[i]<h)
           {

             z=i;
             i++;

           }
           z++;
           printf("%d",abs((n-z)-z));


   }

}


OUTPUT

Congratulations, you passed the sample test case.
Click the Submit Code button to run your code against all the test cases.

Input (stdin)

2
3 2
4 3 1
1 5
6
Your Output (stdout)

1
1
Expected Output

1
1

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!