Skip to main content

Posts

Showing posts from August, 2017

Longest Arithmetic Progression [MEDIUM]

Company: Microsoft (2017) , Snapdeal Question: Given an array of sorted numbers having no duplicates , write a program to find the length of the  L ongest A rithmetic  P rogression (LLAP) in it. Input: The first line of input contains an integer  T  denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. The first line of each test case contains an integer  N  , where   N  is the size of array. The second line of each test case contains  N  space separated integers denoting array input  A[i]. Output:  Print the length of the Longest Arithmetic Progression ​ Constraints: 1 <=T<= 200 1 <= N <= 1000 1 <= arr[i] <= 10000 Example: Input: 2 6 1  7 10  13  14  19 5 2 4  6 8 10 Output: 4 5 Explanation: For test case 1: set[] = {1, 7, 10, 13, 14, 19} output = 4 The longest arithmetic progression is {1, 7, 13, 19} For test case 2: set[] = {2, 4, 6, 8, 10} output = 5  The whole set is in AP Code: import java.util.Scanner;

Sum of bit difference [MEDIUM]

Company: Google Question: Write a program to find the sum of bit differences in all pairs that can be formed from array elements n. Bit difference of a pair (x, y) is a count of different bits at same positions in binary representations of x and y. For example, bit difference for 2 and 7 is 2. Binary representation of 2 is 010 and 7 is 111 ( first and last bits differ in two numbers). Input:  The first line of input contains an integer  T  denoting the number of test cases. First line of the test case will contain an array of elements n. Output:  The sum of bit differences of all pairs that can be formed by given array. Constraints: 1 <=T<= 100 1 <=N<= 10 1 <=a[i]<= 10 Example: Input : 2 2  1 2 3  1 3 5 Output: 4 8 CODE: import java.util.*; import java.lang.*; import java.io.*; class Solution  { public static void main (String[] args) {   Scanner sc = new Scanner(System.in);    int t = sc.nextInt();    sc.nextLine();

Trapping Rain water [MEDIUM]

Company: PayU, Microsoft, De-Shaw, Amazon, Accolite Question: Given n non-negative integers in array representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example: Input: 3 2 0 2 Output: 2 Structure is like below |  | |_| We can trap 2 units of water in the middle gap. Below is another example. Input: The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. Each test case contains an integer N followed by N numbers to be stored in array. Output: Print trap units of water in the middle gap. Constraints: 1<=T<=100 3<=N<=100 0<=Arr[i]<10 Example: Input: 2 4 7 4 0 9 3 6 9 9 Output: 10  0   Code: import java.util.*; import java.lang.*; import java.io.*; class Solution { public static void main (String[] args) { Scanner sc = new Scanner(System.in);     int m = sc.nextInt();     for(int mi=0;mi<m;mi++){

Set Bit [EASY]

Company: Qualcomm, Juniper Networks, Cisco, Adobe, Brocade Question: Given a positive integer  N , print count of set bits in it. For example, if the given number is 6, output should be 2 as there are two set bits in it. Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow.  The next T lines will contain an integer  N . Output: Corresponding to each test case, in a new line, print count of set bits in it. Constraints: 1 ≤ T ≤ 100 1 ≤ N ≤ 1000000 Example: Input: 2 6 11   Output: 2 3 Code: import java.util.*; import java.lang.*; import java.io.*; class Solution { public static void main (String[] args) { Scanner sc = new Scanner (System.in); int m = sc.nextInt(); for(int mi=0;mi<m;mi++){    int n = sc.nextInt();    String a = Integer.toBinaryString(n);   // System.out.println(a);    //String ar= new String[a.length()];    int count=0;    for(int i=0;i<a.length

Fifth root of a number

Example of Zeno's paradox

Explanation of Zeno's paradox The above series is one solution to a scenario proposed by Zeno wherein he considered a man and a turtle to race and since turtle is slower, it was given a head start of say 100m. Man being faster then sprints and makes up that 100 m. But during this time , the turtle may have covered another 10 m. So the man again has to sprint 10 m to be with turtle. But again during this time the turtle may have covered another 1 m. So does that mean the turtle wins? Because that's not practical right? The man has to eventually take over the turtle and win the race..But Everytime he tries to overcome turtle..he has to cover certain more distance..and this process will be infinite right? The answer is we don't know..but eventually the man does take over the turtle and that's the truth. The above series is one such example wherein say the man has to initially cover 1 m and maybe during this time turtle covers 1/2 m and when man tries to cover these 1/

Sum of infinite integers

DNS - Do Not get Scared

D o N ot get S cared- that's what I feel DNS actually can be referred to as. Let's dive into this short article. DNS means Domain Name System. To understand the importance of DNS lemme tell you this and I can say so safely that " Without DNS your Internet would get crippled! " Yes that's right! DNS can be considered as a backbone or an link between various aspects of Internet which makes it easy to use and ofcourse access to. And believe me ..Without DNS you will go back to olden days wherein you will have to type a letter and post it, listen music in CDs, read about news in newspaper (well thats what its for ..) .So question arises what it is and why it is? Well as many may know, computers communicate with each other with a 32 bit numbers which are called IP addresses . Now think of IP as your normal home address which you may need to mention in a letter you want to post to any friend of yours. This means IP is nothing but address written in numbers. That

Implement Queue using Stack [EASY/IMPORTANT]

Company:Flipkart, De-Shaw, Amazon, Adobe, Accolite, Inmobi, Microsoft, MAQ-Software, MakeMyTrip, InfoEdge, Goldman-Sachs, Walmart-Labs, Oracle Question: Implement a Queue using 2 stacks  s1  and  s2  . Input  : The first line of the input contains an integer ' T ' denoting the number of test cases. Then T test cases follow. First line of each test case contains an integer  Q  denoting the number of queries .  A Query  Q  is of 2 Types (i)  1 x   (a query of this type means  pushing  'x'  into the queue) (ii)  2     (a query of this type means to pop element from queue and print the poped element) The second line of each test case contains  Q  queries seperated by space. Output: The output for each test case will  be space separated integers having  -1  if the queue is empty else the element poped out from the queue .  You are required to complete the two methods  push  which take one argument an integer  'x'  to be pushed into the quee  and  pop  which retur

Implement Stack Using Queues [EASY/IMPORTANT]

Company: Grofers, DE-Shaw, CouponDunia, Amazon, Adobe, Accolite, Snapdeal, Oracle, Kritical Solutions Question: Implement a Stack using 2 queue  q1  and  q2  . Input : The first line of the input contains an integer ' T ' denoting the number of test cases. Then T test cases follow. First line of each test case contains an integer  Q  denoting the number of queries .  A Query  Q  is of 2 Types (i)  1 x   (a query of this type means  pushing  'x'  into the stack) (ii)  2     (a query of this type means to pop element from stack and print the poped element) The second line of each test case contains  Q  queries seperated by space. Output: The output for each test case will  be space separated integers having  -1  if the stack is empty else the element poped out from the stack .  You are required to complete the two methods  push  which take one argument an integer  'x'  to be pushed into the stack  and  pop  which returns a integer poped out fro