Skip to main content

Posts

Showing posts with the label Basic

Angle between hour and minute hand[BASIC]

Company: Salesforce, Paytm, Amazon, Infinera Calculate the angle between hour hand and minute hand. There can be two angles between hands, we need to print minimum of two. Also, we need to print  floor  of final result angle. For example, if the final angle is 10.61, we need to print 10. 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 one line conatining two space separated numbers h and m where h is hour and m is minute. Output: Coresponding to each test case, print the angle b/w hour and min hand in a separate line. Constraints: 1 ≤ T ≤ 100 1 ≤ h ≤ 12 1 ≤ m ≤ 60 Example: Input 2 9 60 3 30 Output 90 75 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(); ...

Leaders in Array [BASIC]

Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the elements to its right side. The rightmost element is always a leader. COMPANY:PayU   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 all the leaders. Constraints: 1 <= T <= 100 1 <= N <= 100 0 <= A[i]<=100 Example: Input: 2 6 16 17 4 3 5 2 5 1 2 3 4 0 Output: 17 5 2 4 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++){    int n=sc.nextInt();    int [] ar=...

k large elements [BASIC]

Company: Walmart- Labs, Microsoft, Amazon Question: Given an array, print k largest elements from the array.  The output elements should be printed in decreasing order. Input: The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N and k, N is the size of array and K is the largest elements to be returned. The second line of each test case contains N input C[i]. Output: Print the k largest element in descending order. Constraints: 1 ≤ T ≤ 100 1 ≤ N ≤ 100 K ≤ N 1 ≤ C[i] ≤ 1000 Example: Input: 2 5 2 12 5 787 1 23 7 3 1 23 12 9 30 2 50 Output: 787 23 50 30 23 CODE: import java.util.*; import java.lang.*; import java.io.*; class GFG { 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();     int []ar= new int[n];     int k=sc...

Count total set bits [BASIC/IMP]

Question:  Find the sum of all bits from numbers 1 to N. Company: Amazon Input: The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N. Output: Print the sum of all bits. Constraints: 1 ≤ T ≤ 100 1 ≤ N ≤ 1000 Example: Input: 2 4 17 Output: 5 35 Explanation: An easy way to look at it is to consider the number 6: 0|0 0 0|0 1 0|1 0 0|1 1 -|– 1|0 0 1|0 1 1|1 0 CODE: #include <stdio.h> int getCount(int n){     int c=0;     while(n){         n=n&(n-1);         c++;     }     return c; } int main() {     int t,i,n,c;     scanf("%d",&t);     while(t--){         c=0;         scanf("%d",&n);         for(i=1;i<=n;i++){             c+=getCount(i);   ...

nth term of an AP[BASIC]

Given the first 2 terms of Arithmetic Series tell the nth term of the series.  Input: First line contains an integer, the number of test cases 'T'. Each test case in its first line should contain two positive integer a and b(First 2 terms of AP). In the second line of every test case it contains of an integer N. Output: In each seperate line print the Nth term of the Arithmetic Progression. Constraints: 1<=T<=30 -100<=a<=100 -100<=b<=100 1 <= N <= 100 Example: Input: 2 2 3 4 1 2 10 Output: 5 10 CODE: import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (String[] args) { Scanner sc = new Scanner (System.in); int m =sc.nextInt(); for(int mi=0;mi<m;mi++){     int a=sc.nextInt();     int b = sc.nextInt();     int d= b-a;     int n =sc.nextInt();     System.out.println(a+(n-1)*d); ...

Check Palindrome of String [BASIC]

Company: Paytm Question: Given a string s check if it is palindrome or not. Input: The first line contains 'T' denoting the number of test cases. Then follows description of test cases. Each case begins with a single integer N denoting the length of string. The next line contains the string s. Output: Print "Yes" if it is a palindrome else "No". (Without the double quotes) Constraints: 1<=T<=30 1<=N<=100 Example: Input: 1 4 abba Output: Yes CODE: import java.util.*; import java.lang.*; import java.io.*; class GFG { 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 s1 =sc.next();   // System.out.print(s1+" ");    char [] t1= s1.toCharArray();    int count=0;    for(int i=n-1;i>=0;i--){        if(t1[i]==t1[n-i-1]){ ...

Pair cube [BASIC]

Company:Adobe Question: Given N, count all ‘a’ and ‘b’ that satisfy the condition a^3 + b^3 = N. Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains integer n. Output: Count all 'a' and 'b' that satisfy the above equation. Constraints: 1<=T<=10^5 1<=n<=10^5 Example: Input: 2 9 28 Output: 2 2 Code: import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); int tc=sc.nextInt(); for(int i=0;i<tc;i++) {     int c=0;     double x=sc.nextInt();     for(int j=0;j<=x;j++)     {         for(int k=1;k<=x;k++)         {             double l=Math.pow(j,3) + Math.pow(k,3);    ...

Rotate by 90 degrees [BASIC/IMPORTANT]

Company: Microsoft Question: Given an square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space. 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 consist of two lines. The first line of each test case consists of an integer N, where N is the size of the square matrix.The second line of each test case contains NxN space separated values of the matrix M. Output: Corresponding to each test case, in a new line, print the rotated array. Constraints: 1 ≤ T ≤ 50 1 ≤ N ≤ 50 Example: Input 1 3 1 2 3 4 5 6 7 8 9 Output 3 6 9 2 5 8 1 4 7 Code: import java.util.*; import java.lang.*; import java.io.*; class GFG { 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.nextIn...

Find the Highest number [BASIC]

Company: Amazon Question: Given an array with distinct elements in such a way that first the elements stored in array are in increasing order and then after reaching to a peak element , elements stored are in decreasing order. Find the highest element. Input: The first line of input contains an integer  T  denoting the number of test cases. The first line of each test case consists of an integer  n . The next line consists of  n  spaced integers.  Output: Print the highest number in the array. Constraints:   1<=T<=100 1<=n<=100 1<=a[i]<=10 5 Example: Input: 2 11 1 2 3 4 5 6 5 4 3 2 1 9 1 3 4 5 7 8 9 5 2  Output: 6 9 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();   ...