Monday, 23 January 2017

Queries

Kunal likes numbers and today he has an list of N integers.Sunny likes to ask a lot questions and he saw Kunal with his list of integers and instantly decided to ask Kunal q number of queries.Each query can be of the following:
A Y - Add the integer Y to the list.
M - Print the maximum integer from the list and remove it from the list.
m - Print the minimum integer from the list and remove it from the list.
Note: if there are multiple occurences of maximum/minimum number, you can remove any one of them
Help Kunal answer this queries quickly.
Input Format
First line contains two integers N and q denoting the size of the list of numbers with Kunal and number of queries Sunny will ask respectively.
Next line contains N space separated integers Xi, the integers of the list.
q lines follow with one of the three types of queries mentioned in the problem statement.
1<=N<=105
1<=q<=105
1<=Xi , Y<=109
Before any m or M type queries, it is guaranteed that the size of the list would be atleast one and also not exceed 105 considering all the queries of type A Y
Output Format
For each query of type M and m, print the answer to that query on a new line.
Sample Input
10 6
13 12 999 14 15 16 19 18 17 110
m
A 1000
M
M
A 10
m
Sample Output
12
1000
999
10
Explanation
Initially the list is [13, 12, 999, 14, 15, 16, 19, 18, 17, 110]
First query is m, print the minimum and remove it from the list.
We print 12 and remove it after which our list is [13, 999, 14, 15, 16, 19, 18, 17, 110]
Second query is A 1000, we add it to the list after which our list is [13, 999, 14, 15, 16, 19, 18, 17, 110 , 1000]
Third query is M print the maximum and remove it from the list.
We print 1000 and remove it after which our list is [13, 999, 14, 15, 16, 19, 18, 17, 110]
Fourth query is M print the maximum and remove it from the list.
We print 999 and remove it after which our list is [13, 14, 15, 16, 19, 18, 17, 110]
Fifth query is A 10, we add it to the list after which our list is [13, 14, 15, 16, 19, 18, 17, 110 , 10]
Sixth query is m, print the minimum and remove it from the list.
We print 10 and remove it after which our list is [13, 14, 15, 16, 19, 18, 17, 110]

source code in python:
import bisect
n,m = [int(x) for x in input().split()]
t=[];
r=[];
y=[int(x) for x in input().split()]
y.sort();
for i in range(m):
        r=input().split();
        if r[0]=='m':
              print(y.pop(0));
        elif r[0]=='M':
              print( y.pop());
        else:
             bisect.insort(y,int(r[1]));
              
output:
Input (stdin)
10 6
13 12 999 14 15 16 19 18 17 110
m
A 1000
M
M
A 10
m
Your Output (stdout)
12
1000
999
10
Expected Output
12
1000
999
10

test case 2:
Input (stdin)
6 2
629321215 11361548 817039529 786899840 231603078 594823668
A 718761252
M

Your Output (stdout):

817039529
Expected Output
817039529

Winner

Athlos has a total of N events. Each event has M matches. The winner of each of these NxM matches is given to you in the form of a grid. 

The grid contains N horizontal lines denoting each event and M vertical lines denoting the M matches of each event. There are a total of NxM students each having a unique id between 1 to NxM. The jth element of the ith line denotes the id of the winner of that match. 

Given the grid you need to help Ruben determine the ultimate winner i.e. the student who has won maximum number of matches. In case there are more than one student with same number of matches won then print the least id amongst all.
Input Format
Input consists of T testcases. First line of each case consists of two space seperated integers N, M denoting the number of events and number of matches in each event. That is followed by N lines each containing M integers denoting the winners of each match.
Constraints
1 ≤ T ≤ 10
1 ≤ N, M ≤ 600
1 ≤ Winner's id (aij) ≤ N*M
Output Format
For each testcase print one line containing the id of the ultimate winner i.e. the student who won the maximum number of matches. (In case of tie print the id which is least).
Sample Input 0
2
2 2
1 2
3 3
2 2
1 1
4 4
Sample Output 0
3
1
source code:
import java.io.*;
import java.util.*;
import java.util.Collections;
public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int i=0;i<t;i++)
            {
            int n=sc.nextInt();
            int m=sc.nextInt();
             int q[]=new int[n*m+1];
            q[0]=0;int max=0;
             for(int j=1;j<=n*m;j++)
             {
                 int temp=sc.nextInt();///storing frequency of winning of a player
               q[temp]++;
                 
                if(q[temp]>q[max])//comparing the greatest winner
                   { 
                      max=temp;
                   }
                if(q[temp]==q[max] && temp<max)//if two people with same no.of //wins then placing the winner with least id
                    max=temp;
            }
            System.out.println(max);
            
        }
        
    }
}

Finding Power of 2

Mufaddal is organizing the table tennis tournament for Athlos. He calls a tournament to be "Perfect" if the number of participants in the tournament is a power of two.

Given the number of participants N you need to help Mufaddal determine if the tournament will be "Perfect" or not.
Input Format
The first line of input contains T denoting the number of testcases (Number of tournaments). It is followed by T lines containing one integer per line which denotes the total number of participants in that tournament.
Constraints
1 ≤ T ≤ 106
1 ≤ N ≤ 2 * 1018
Output Format
Print T lines (one for each testcase), if the tournament is perfect print "Yes" (without quotes) otherwise print "No" (without quotes).
Sample Input 0
4
8
5
4
11
Sample Output 0
Yes
No
Yes
No

Saturday, 14 January 2017

Hello World ! Program



public class Solution {    //class

    public static void main(String[] args) {//main
        System.out.println("Hello, World.");//printing  Hello, World.on new line.
        System.out.println("Hello, Java.");//printing Hello, Java on new line
           
           

       
    }
}
output:
            Hello, World.
            Hello, Java.



explaination: System.out.println("Text to be printed"); is used to print on new line
                      and System.out.print(" "); is used to print on same line.