Skip to main content

Posts

Showing posts from July, 2022

TCS Digital 2021 Day 1 Slot 1 : Question 1

  // TCS Digital 2021 Day 1 Slot 1 : Question 1   import java.util.*; class Solution {     public static boolean isPalindrome (String s)     {                 if (s.length () == 1)             return true;         StringBuilder sb = new StringBuilder (s);         sb = sb.reverse ();         String rev = new String (sb);         return s.equals (rev);     }       public static void main (String[]args)     {         Scanner sc = new Scanner (System.in);         String str = sc.next ();          ...

TCS Java Coding Question Day 2 Slot 1 : Property Painting

  // TCS Java Coding Question Day 2 Slot 1 : Property Painting   import java.util.Scanner; class Prop_paint {     public static void main(String[] args) {        int ni, ne, i = 0;        float intP = 18, extP = 12, cost = 0, temp;        Scanner sc = new Scanner(System.in);        System.out.printf("Number of Interior Walls : ");        ni = sc.nextInt();        System.out.printf("\nNumber of Exterior Walls : ");        ne = sc.nextInt();        if(ni < 0 || ne < 0) {            System.out.print("INVALID INPUT");        } else if(ni == 0 && ne == 0) {         ...

TCS Java Coding Question - Jar of Candies

  // TCS Java Coding Question - Jar of Candies   import java.util.Scanner; class Main{     public static void main(String[] args) {               int n = 10, k = 5;               int num;               Scanner sc = new Scanner(System.in);               num = sc.nextInt();               if(num >= 1 && num <= 5) {                           System.out.println("NUMBER OF CANDIES SOLD : " + num);                           System.out.pri...

WeCP Python Coding Question 8 : Lazy String

  WeCP   Python Coding Question 8 : Lazy String Problem Statement-:   Anish is the laziest person you can ever see. He is tasked to write the name of the winner in a game where two people take part. And he just writes the longest common subsequence over there, so that with minimum chane or no backspace he can edit the name to the winner’s name. For two given names, you have to predict what Anish will write in his computer before the start of the name. If there are more than two longest subsequences possible,write the one with less lexicographic value.   Input Format: Two lines including two strings of name(All with capital letters)   Output Format: A single line with the lexicographically smallest possible longest common subsequence.   Sample Input: ABCD BACD Sample Output: ACD   Explanation: ACD and BCD these are the two possible biggest substring   Program :   # WeCP Python Coding Question 8 : Laz...

WeCP Python Coding Question 7 : Momentum LinkedList

  WeCP   Python Coding Question 7 : Momentum LinkedList Problem Statement -:   Ratul made a linked list, a list made of n nodes, where every node has two variables, the velocity and the mass of a particle. Since all the particles have the velocity in the same direction, find the total momentum of the entity made by the particles from the linked list. Constraints : 1<=n<=10000 1<=m,v<=100   Input format: First line containing n, number of nodes Then n lines containing the mass and the velocity space separated.   Output Format: Single integer denoting the momentum   Sample Input: 4 1 3 2 4 2 3 4 5   Sample Output: 37   Program :   # WeCP Python Coding Question 7 : Momentum LinkedList   n=int(input()) s=0 for i in range(n):     m,v=map(int,input().split())     s+=(m*v) print(s)   For Output Check the Video :

WeCP Python Coding Question 6 : Help of Prepsters

  WeCP   Python Coding Question 6 : Help of Prepsters Problem Statement-: Arnab has given me a challenge. I have to calculate the number of numbers which are less than a certain value n, and have exactly k set bits in its binary form. As you are a Prepster like me, help me write a code that will take input for n and k and give the expected output.   Constraints : 1<=n<=10000 1<=k<=10   Input Format : First line containing n and k space separated Output Format : Number of numbers present in a single line   Sample Input: 7 2   Sample Output: 3   Explanation: 011,110,101 -> These three numbers.   Program : # WeCP Python Coding Question 6 :    Help of Prepsters   ans=0 n,k=map(int,input().split()) l=len(bin(n)[2:]) def func(i,s,L):     global l     global ans     if(L>l):       ...

WeCP Python Coding Question 5 : Corona for Computer

  WeCP   Python Coding Question 5 : Corona for Computer Problem Statement-: Every decimal number can be changed into its binary form. Suppose your computer has it’s own CoronaVirus, that eats binary digits from the right side of a number. Suppose a virus has 6 spikes, it will eat up 6 LSB binary digits in your numbers. You will have a bunch of numbers, and your machine will have a virus with n spikes, you have to calculate what will be the final situation of the final numbers.   Input Format: First line, a single Integer N Second line N space separated integers of the bunch of values as array V Third line a single integer n, the number of spikes in Corona for Computer   Output Format: Single N space separated integers denoting the final situation with the array v.   Sample Input: 5 1 2 3 4 5 2   Sample Output: 0 0 0 1 1   Explanation: 5 is 101 in binary, when you cut the last two binary digits, its 1. ...

WeCP Python Coding Question 4 : Class Monitor

  WeCP   Python Coding Question 4 : Class Monitor Problem Statement-:   After JEE Mains, some students got admission into an engineering college. Now there is a class consisting of such n students, and the HOD came to say it is time to select the class monitor. But He never gets all of them at one time. So he brought a register, every time he gets someone with less rank than the previous time he cut the name and wrote the name of the student and the rank. For a given number of ranks he gets each time, you have to predict how many names are cut in the list. Constraints: Number of Visiting<=10^9 ranks <=10000   Input Format: Number of Visiting N in their first line N space separated ranks the HOD gets each time Output Format: Number of ranks cut in the list   Sample Input: 6 4 3 7 2 6 1   Sample Output: 3   Program : # WeCP Python Coding Question 4 : Class Monitor   n=int(input()) a=list(map(in...

WeCP Python Coding Question 3 : Profit balance

  WeCP   Python Coding Question 3 : Profit balance Problem Statement-: Anand and Brijesh got a bunch of profit in their business. Now it’s time to divide the profit between themselves. Anand being the one with the calculator was the one who could decide who will get how much. But as he was one hell of an honest guy, he can’t cheat on Brijesh to get some extra money. So he decided to divide them in such a way where they can get almost the same amount possible. Although, it is sometimes impossible to divide the profit into two, because the profits must be written in their production managed copy in a way where you cannot share the profit of a single thing, A single Profit will be held by a single person. You are the one who is called to mitigate the problem. Given an array of profit, find out in the process, what can be the minimum difference between them in terms of income. Constraints: 1<=N<=10^3 0<=Profits<=1000 Input Format: First line contains an...

WeCP Python Coding Question 2: Even Odd Even Odd

  WeCP   Python Coding Question 2: Even Odd Even Odd Problem Statement -:   A number is even odd special if and only if there are even digits side by side, same goes for odd digits too. So the distributions must be in an alternating basis of even digits and odd digits. You are given a number N, and you have to say how many special numbers are possible <=N, and are greater than 0. Constraints: 1<=N<=10^8   Input Format: First Line containing a single integer N. Output Format: Single line containing a single integer denoting the possible number of special numbers.   Sample Input: 30   Output: 20   Explanation: 20 such special numbers are there :- 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 21, 23, 25, 27, 29, 30     Program : # WeCP Python Coding Question 2 : Even Odd Even Odd   a=input() ans=0 l=len(a) def func(s):     global ans     glob...

WeCP Python Coding Question 1: Sentence counter

  WeCP   Python Coding Question 1: Sentence counter Problem statement-:   We know sentences are terminated with certain punctuations like ‘.’,’!’,’?’, whereas there are several punctuations which don’t terminate the punctuations. Given for a paragraph, write a code to find out how many sentences are there. Note that, ‘…’,’,’,’-‘ these don’t terminate a sentence. Constraints: Number of words in the paragraph<=10^8   Input Format: Paragraph ended with an enter.   Output Format: Single Integer denoting number of sentences.   Sample Input: Hello! How are you? Last time I saw you… you were nervous.   Sample Output: 3   Explanation: The three sentences are:   hello!   How are you?   Last time I saw you… you were nervous.   Program :   # WeCP Python Coding Question 1 : Sentence Counter   def SentenceCount(L):     ans=0     fo...

Microsoft CoCubes Java Programming Questions – 4 Find the number closest to n and divisible by m

  Microsoft   CoCubes Java Programming Questions – 4 Find the number closest to n and divisible by m Given two integers n and m. The problem is to find the number closest to n and divisible by m. If there are more than one such number, then output the one having maximum absolute value. If n is completely divisible by m, then output n only. Time complexity of O(1) is required. Constraints: m != 0 We find value of n/m. Let this value be q. Then we find closest of two possibilities. One is q * m other is (m * (q + 1)) or (m * (q – 1)) depending on whether one of the given two numbers is negative or not.   Algorithm: closestNumber(n, m)     Declare q, n1, n2     q = n / m     n1 = m * q       if (n * m) > 0         n2 = m * (q + 1)     else         n2 = m * (q - 1)       i...

Microsoft CoCubes Java Programming Question – 3 Longest Prefix Suffix

  Microsoft   CoCubes Java Programming Question – 3 Longest Prefix Suffix Given a string of character, find the length of longest proper prefix which is also a proper suffix. Example: S = abab lps is 2 because, ab.. is prefix and ..ab is also a suffix. Input: First line is T number of test cases. 1<=T<=100. Each test case has one line denoting the string of length less than 100000. Expected time compexity is O(N). Output: Print length of longest proper prefix which is also a proper suffix. Example: Input: 2 abab aaaa Output: 2 3   Program :   // Microsoft CoCubes Java Programming Question – 3   import java.util.*; import java.lang.*; import java.io.*;   class PreSuf {   public static void main (String[]args)   {       Scanner s = new Scanner (System.in);     System.out.println("Enter Number of Testcases : ");     int t = ...

Microsoft CoCubes Java Programming Question – 2 Maximum difference between two elements such that larger element appears after the smaller number

  Microsoft CoCubes Java Programming Question – 2 Maximum difference between two elements such that larger element appears after the smaller number Given an array arr[] of integers, find out the difference between any two elements such that larger element appears after the smaller number in arr[]. Examples: If array is [2, 3, 10, 6, 4, 8, 1] then returned value should be 8 (Diff between 10 and 2). If array is [ 7, 9, 5, 6, 3, 2 ] then returned value should be 2 (Diff between 7 and 9) Time Complexity: O(n^2) Auxiliary Space: O(1)   Use two loops. In the outer loop, pick elements one by one and in the inner loop calculate the difference of the picked element with every other element in the array and compare the difference with the maximum difference calculated so far.   Program :   // Microsoft CoCubes Java Programming Question – 2   import java.util.*;   class MaximumDifference {     int maxDiff(int arr[], int ...