Skip to main content

Posts

Showing posts with the label Samsung

Next larger element [EASY]

Company: Snapdeal, Samsung, PayU, Informatica, CouponDunia, Amazon  Question:  Given an array A [ ] having distinct elements, the task is to find the next greater element for each element of the array in order of their appearance in the array. If no such element exists, output -1  Input: The first line of input contains a single integer  T  denoting the number of test cases.Then  T  test cases follow. Each test case consists of two lines. The first line contains an integer N denoting the size of the array. The Second line of each test case contains N space separated positive integers denoting the values/elements in the array A[ ].   Output: For each test case, print in a new line, the next greater element for each array element separated by space in order. Constraints: 1<=T<=200 1<=N<=1000 1<=A[i]<=1000 Example: Input 1 4 1 3 2 4  Output 3 4 4 -1 Explanation In the array, the next larger ...

Longest Increasing Subsequence

Company: Amazon, Samsung, Paytm, BankBazaar. Question:  Given a sequence, find the length of the longest increasing subsequence from a given sequence . The longest increasing subsequence means to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique. Note:  Duplicate numbers are not counted as increasing subsequence. For example:  length of LIS for  { 10, 22, 9, 33, 21, 50, 41, 60, 80 } is 6 and LIS is {10, 22, 33, 50, 60, 80}. Input: The first line contains an integer T, depicting total number of test cases.  Then following T lines contains an integer N depicting the size of array and next line followed by the value of array. Output: Print the Max length of the subsequence in a separate line. Constraints: 1 ≤ T ≤ 100 1 ≤ N ≤ 1000 0 ≤ A[i] ≤ 300 INPUT: 1 16 0 8...

Egg Dropping Puzzle: DP [MEDIUM/VERY IMPORTANT]

Company: MakeMyTrip, Hike, Google, Goldman-Sachs, D-E-Shaw, Amazon, Philips, Oracle, Opera, Nearby, Mynta, MAQ-Software, VMWare, Unisys, Service Now, Samsung Question: The following is a description of the instance of this famous puzzle involving n=2 eggs and a building with k=36 floors. Suppose that we wish to know which stories in a 36-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions: …..An egg that survives a fall can be used again. …..A broken egg must be discarded. …..The effect of a fall is the same for all eggs. …..If an egg breaks when dropped, then it would break if dropped from a higher floor. …..If an egg survives a fall then it would survive a shorter fall. …..It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the 36th-floor do not cause an egg to break. In this problem you have to find minimum trials to solve for n eggs and k floors. For more description...

Power of 2 [Easiest/One liner]

Company: Oracle, Microsoft, Mallow Technologies, Intel , FactSet, BankBazaar, Adobe, Samsung, Practo Question: Given a positive integer N, check if N is a power of 2. Input: The first line contains 'T' denoting the number of test cases. Then follows description of test cases. Each test case contains a single positive integer N. Output: Print "YES" if it is a power of 2 else "NO". (Without the double quotes) Constraints: 1<=T<=100 0<=N<=10^18 Example: Input : 2 1 98 Output : YES ​NO Explanation:  (2^0 == 1) Code: import java.util.*; import java.lang.*; import java.io.*; class Solution { public static void main (String[] args) { //code Scanner sc= new Scanner(System.in); int m = sc.nextInt(); for(int i=0;i<m;i++){ System.out.println((Math.log(sc.nextBigInteger().doubleValue())/Math.log(2))%1<=.00001?"YES":"NO"); } } }

Kadane Algorithm- Longest contiguous sub-array with maximum sum [MEDIUM]

Asked in : Metlife, Housing.com ,Hike, Flipkart, FactSet, D-E Shaw ,Amazon,Accolite, Snapdeal, Samsung ,PayU , Paytm ,Oracle, Ola,Microsoft, Morgan Stanley Question: Given an array containing both negative and positive integers. Find the contiguous sub-array with maximum sum. Input: The first line of 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 a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array. Output: Print the maximum sum of the contiguous sub-array in a separate line for each test case. Constraints: 1 ≤ T ≤ 200 1 ≤ N ≤ 100 -100 ≤ A[i] <= 100 Example: Input 2 3 1 2 3 4 -1 -2 -3 -4 Output 6 -1 Code: import java.io.*; // Java program to print largest contiguous array sum import java.util.*; class Solution {     public static void main (Strin...