Posts

Showing posts from February, 2021

Tcs assessment problem: museum problem.

 package tcs_assessments; import java.util.Scanner; public class museumwalaprogram { //below is the main method         public static void main(String[] args) {                  Scanner sc=new Scanner(System.in);         Museum[] museum=new Museum[5];         for (int i = 0; i <museum.length ; i++) {             int mi=sc.nextInt();             sc.nextLine();             String mn=sc.nextLine();             String ml=sc.nextLine();             int e=sc.nextInt();             sc.nextLine();             double r=sc.nextDouble();             sc.nextLine();             boolean rc=sc.nextBoolean();   ...

TCS JAVA PROGRAMMING QUESTION.(problem of appliances)

  package youtube ; import java.util.Scanner ; public class MyClass { // below is the main method public static void main ( String [] args ) { // below is the code for creating the array of the Appliance class Appliance [] appliance = new Appliance[ 4 ] ; // below is the code for creating the scanner object and taking input Scanner sc = new Scanner( System . in ) ; for ( int i = 0 ; i < appliance . length ; i ++) { int applianceid = sc .nextInt() ; sc .nextLine() ; String appliancename = sc .nextLine() ; String appliancecategory = sc .nextLine() ; double applianceamount = sc .nextDouble() ; appliance [ i ]= new Appliance( applianceid , appliancename , appliancecategory , applianceamount ) ; } sc .nextLine() ; int applianceid = sc .nextInt() ; boolean insurance = sc .nextBoolean() ; sc .nextLine() ; String appliancecategory = sc .nextL...

convert the integer number to its binary representation.

  Binary representation School Accuracy: 36.68% Write a program to print Binary representation of a given number N. Example 1: Input: N = 2 Output: 000000000000000000000000000010 Explanation: The binary representation of 2 is '10' but we need to print in 30 bits so append remaining 0's in the left.     ANSWER: string getBinaryRep(int N) { vector<int> vec; while(N>0) { int rem=N%2; vec.push_back(rem); N=N/2; } int l=30-vec.size(); for(int i...

Problem no 18(array):count the number of pairs in array whose sum is equal to the given number.

  Count pairs with given sum Easy Given an array of N integers, and an integer  K , find the number of pairs of elements in the array whose sum is equal to K . Example 1: Input: N = 4, K = 6 arr[] = {1, 5, 7, 1} Output: 2 Explanation: arr[0] + arr[1] = 1 + 5 = 6 and arr[1] + arr[3] = 5 + 1 = 6.     ANSWER:  int getPairsCount(int arr[], int n, int k) {         int c=0;         unordered_map<int,int> m;         for(int i=0;i<n;i++)   ...

Problem no 6: Find the union of 2 arrays.

  Union of two arrays Basic Accuracy: 47.56% Submissions: 8647 Points: 1 Given two arrays A and B of size N and M respectively. The task is to find union between these two arrays. Union of the two arrays can be defined as the set containing distinct elements from both the arrays. If there are repetitions, then only one occurrence of element should be printed in union. Example 1: Input: 5 3 1 2 3 4 5 1 2 3 Output: 5 Explanation: 1, 2, 3, 4 and 5 are the elements which comes in the union set of both arrays.  S...

Problem no 7: Cyclically rotate the array by one.

  Cyclically rotate an array by one Basic Accuracy: 77.14% Submissions: 4605 Points: 1 Given an array, rotate the array by one position in clock-wise direction.   Example 1: Input: N = 5 A[] = {1, 2, 3, 4, 5} Output: 5 1 2 3 4   Example 2: Input: N = 8 A[] = {9, 8, 7, 6, 4, 2, 1, 3} Output: 3 9 8 7 6 4 2 1     Answer:   void rotate(int arr[], int n) { int num=arr[n-1]; for(int i=n-1;i>0;i--) { arr[i]=arr[i-1]; } arr[0]=num; for(int i=0;i<n;i++)...

Problem no 4: sort the array of 0's,1's,and 2's.

Sort an array of 0s, 1s and 2s Easy Accuracy: 51.36% Submissions: 42188 Points: 2 Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order. Example 1: Input: N = 5 arr[]= {0 2 1 2 0} Output: 0 0 1 2 2 Explanation: 0s 1s and 2s are segregated into ascending order. Answer: without using sorting algorithm. void sort012(int a[], int n) {         int countofzero=0;    int countofone=0;    int countoftwo=0;      for(int i=0;i<n;i++)    {       if(a[i]==0)     ...

Problem no 5 :Move all the negative elements to left side of the array.

QUESTION: Move all negative numbers to beginning and positive to end with constant extra space Difficulty Level :EASY   An array contains both positive and negative numbers in random order. Rearrange the array elements so that all negative numbers appear before all positive numbers. Examples :   Input: -12, 11, -13, -5, 6, -7, 5, -3, -6 Output: -12 -13 -5 -7 -3 -6 11 6 5 ANSWER:                   void rearrange(int arr[],int n) {     int j=0;     for(int i=0;i<n;i++)     {  ...

Middle of 3 Elements

Question:                     Given three distinct numbers A, B and C. Find the number with value in middle (Try to do it with minimum comparisons). Example 1: Input: A = 978, B = 518, C = 300 Output: 518 Explanation: Since 518>300 and 518<978, so 518 is the middle element. ANSWER:   int middle(int A, int B, int C){         int arr[3];         arr[0]=A;         arr[1]=B;         arr[2]=C;          sort(arr, arr + 3);          return arr[1];     }

kadanes algorithm.

 Question is: Given an array arr of N integers. Find the contiguous sub-array with maximum sum.   Example 1: Input: N = 5 arr[] = {1,2,3,-2,5} Output: 9 Explanation: Max subarray sum is 9 of elements (1, 2, 3, -2, 5) which is a contiguous subarray. Example 2: Input: N = 4 arr[] = {-1,-2,-3,-4} Output: -1 Explanation: Max subarray sum is -1  of element (-1)     THE CODE TO SOLVE THIS QUESTION IS:      int maxSubarraySum(int arr[], int n){ int max_so_far=INT_MIN; int max_uptohere=0; for(int i=0;i<n;i++) { max_uptohere=max_uptohere+arr[i]; if(max_uptohere<0) { max_uptohere=0; } if(max_so_far<max_uptohere) { max_so_far=max_uptohere; } } return max_so_far; }