Monday 30 January 2017

Removing middle element from stack


import java.io.*;
import java.util.*;
class Stack
{
Object stk[];

int tos;
Stack(int capacity)
   {
stk=new Object[capacity];

tos=-1;

}
void push(Object ob)
{
stk[++tos]=ob;
if(tos==stk.length)
{
System.out.println("stack overflow");
System.exit(0);
}
}

Object pop()
{
return(stk[tos--]);
}
void display()
{
for(int i=tos;i>=0;i--)
{System.out.print(stk[i]+" ");}
}
 
}
class Demo
{
public static void main(String args[])throws IOException
{
Scanner sc=new Scanner(System.in);
System.out.println("enter number of elements to be pushed");
int c=sc.nextInt();
Stack s=new Stack(c);
Stack s1=new Stack(c);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

for(int j=0;j<c;j++)
{
System.out.println("enter element");
Object ob=br.readLine();
s.push(ob);
}
int mid=c/2;

System.out.println("displaying stack");
s.display();
System.out.println("");
System.out.println("poping middle  elements from stack");
if(c%2==0)
{
while(s.tos!=mid)
{
Object t=s.pop();
s1.push(t);
}
Object t1=s.pop();
System.out.println(t1);
Object t2=s.pop();
System.out.println(t2);
}
else
{
while(s.tos!=mid)
{
Object t=s.pop();
s1.push(t);
}
Object t1=s.pop();
System.out.println(t1);



}
while(s1.tos!=-1)
s.push(s1.pop());
System.out.println("displaying stack");
s.display();

}
}




OUTPUT:(for odd number of elements)
enter number of elements to be pushed
9
enter element
1
enter element
2
enter element
3
enter element
4
enter element
5
enter element
6
enter element
7
enter element
8
enter element
9
displaying stack
9               8               7               6               5               4               3               2             1
        
poping middle  elements from stack
5
displaying stack
9               8               7               6               4               3               2               1


OUTPUT:(for even number of elements)note:in even number middle two elements are poped
enter number of elements to be pushed
4
enter element
1
enter element
2
enter element
3
enter element
4
displaying stack
4               3               2               1
poping middle  elements from stack
3
2
displaying stack
4               1

Saturday 28 January 2017

Hello World program in Java

Watch the below video for running your first Hello World program in Java.



Wednesday 25 January 2017

To Find The Absolute Difference of the sums of two diagonals of the square matrix

Question:

To Find The Absolute Difference of the sums of two diagonals of the
square matrix

source code:


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

public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt(); long sum1=0,sum2=0;
        int arr[][]=new int[n][n];
        for(int i=0;i<n;i++)
            {
            for(int j=0;j<n;j++)
                {
                arr[i][j]=sc.nextInt();
                if(i==j)
                {sum1+=arr[i][j];}
                if(j==n-1-i)
                { sum2+=arr[i][j];}
         
            }
        }
     
     
        System.out.println(Math.abs(sum1-sum2));
     
    }
}


In example given below the sum of first diagonal is 17 and second diagonal is 63.
hence printed result would be |sum1-sum2|.











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