Posts

Shortest Path Length - Ascending Order

 Shortest Path Length - Ascending Order  PROBLEM STATEMENT : The program must accept an integer matrix of size R*C as the input. The matrix contains at least two consecutive non-zero digits starting from 1. The remaining elements in the matrix are 0s. The program must print the length of the shortest path that starts at 1 and goes over all the other non-zero digits in ascending order. Only horizontal and vertical movements are allowed.  Boundary Condition(s):   2 <= R, C <= 50  Input Format:   The first line contains R and C separated by a space.  The next R lines, each contains C integer values separated by a space.  Output Format:   The first line contains the length of the shortest path.  Example Input/Output 1: Input: ( ) 6 6 0 0 4 0 0 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 Output: 11 Explanation:   The minimum distance between 1 and 2 is 2.  The minimum distance between 2 and 3 is...

Zero Insert After K Times One

 Zero Insert After K Times One  PROBLEM STATEMENT : Given a bitstream of length N consisting of 0s and 1s, insert 0 after 1 has appeared K times consecutively.  Input Format:  The first line contains N and K separated by a space.  The second line contains the bitstream with 1s and 0s with each value separated by a space.  Output Format:   In the first line, the bitstream with the 0 inserted after 1s has appeared K times with each value separated by a space.  Boundary Conditions:   1 <= K <= 1000  2 <= N <= 1000  Example Input/Output 1: Input: ( ) 12 2 1 0 1 1 0 1 1 0 1 1 1 1 Output: 1 0 1 1 0 0 1 1 0 0 1 1 0 1 1 0 SOLUTION : C (Programming Language) Copy Code #include<stdio.h> #include<stdlib.h> int main() { int n,k,cnt=0,i,j,a[2002]; scanf("%d%d",&n,&k); for(i=0;i<n;++i) { scanf("%d",&a[i]); } //Count of 1 in first step for(i=0;i<k;++i...

Keyboard Switch Count

 Keyboard Switch Count  PROBLEM STATEMENT : There are three rows in a keyboard containing the alphabets. The alphabets in each row are given as the input. The string S containing the alphabets to be typed is passed as the input. The program must print the number of times, the switch must happen to type the alphabets. Assume initially the position is in the middle row.  Note: Assume S contains only lower case alphabets and no special characters will be present in S.  Boundary Condition(s):   1 <= Length of S <= 100  Input Format:   The first three lines contain the alphabets in the three rows of a keyboard.  The fourth line contains S.  Outupt Format:   The first line contains an integer representing the switch count.  Example Input/Output 1: Input: ( ) qwertyuiop asdfghjkl zxcvbnm coding Output: 8 Explanation:  The string to be typed is coding.  c - bottom row. Hence switch count is 1.  o - top row. ...

Bomb Blast - No Water

  Bomb Blast - No Water  PROBLEM STATEMENT : A matrix of size R*C containing positive integer values is passed as the input. An integer value which is a multiple of 5 is a bomb and will destroy all four adjacent cells (in the following order - left, right, top and bottom ). But if the adjacent cell is water (the integer value will be a multiple of 3), then the bomb will stop destroying the adjacent cells. If a cell is destroyed then the value of that cell becomes 0. After the bomb is blasted then the cell value becomes 0 . If the cell value is both a multiple of 5 and 3, then consider that as water. Finally, the program must print the values in the matrix as the output.  Note: The bombs are triggered from right to left starting from the last row.  Boundary Condition(s):   1 <= R, C <= 100  Input Format:   The first line contains R and C separated by a space.  The next R lines contain C integer values each.  Output Format:   Th...

Four Substrings to Matrix

 Four Substrings to Matrix  PROBLEM STATEMENT : The program must accept N string values of equal length L as the input. The program must divide each string into four substrings of equal length. Then the program must form a character matrix of size R*C , where R = (N*3) and C = (L/4)*3. Then the program must fill the character matrix based on the following conditions.  - The top-left, top-right, bottom-left, bottom-right and middle submatrices of size (R/3)*(C/3) must be filled with the asterisks.  - The top-middle submatrix of size (R/3)*(C/3) must be filled with the 1st substrings of the N string values.  - The middle-right submatrix of size (R/3)*(C/3) must be filled with the 2nd substrings of the N string values.  - The bottom-middle submatrix of size (R/3)*(C/3) must be filled with the 3rd substrings of the N string values.  - The middle-left submatrix of size (R/3)*(C/3) must be filled with the 4th substrings of the N string values.  Finally...

Shirt - Matching Pairs

 Shirt - Matching Pairs PROBLEM STATEMENT : A shop to increase sales during a festival has an offer that a customer will get a discount if the customer buys shirts having same size in pairs. Any customer who buys will choose N shirts and the size of the shirt is denoted by S(i) where 1 <= i <=N. Two shirts S(i) and S(j) are matching and form a pair only if S(i) = S(j).  The program must print the number of pairs eligible for the discount.  Input Format:  The first line will contain the value of N  The second line will contain the the size of N shirts S(1) to S(N) with each size separated by a space. Output Format:  The first line will contain the number of matching pairs eligible for the discount.  Constraints:  2 <= N <= 100  Example Input/Output 1: Input: ( ) 9 10 20 20 10 10 30 44 10 20 Output: 3 Explanation:  The matching pairs are (10,10) (20,20) (10,10).  Example Input/Output 2: Input: ( ) 6 42 44 40...

Two Strings Interchange Vowels

 Two Strings Interchange Vowels  PROBLEM STATEMENT : The program must accept two string values S1, S2 (both having equal number of vowels) and interchange the vowels in the string values in their order of occurrence.  Boundary Condition(s):  1 <= Length of S1, S2 <= 1000  Input Format:   The first line contains S1.  The second line contains S2.  Output Format:   The first line contains S1 with the vowels interchanged.  The second line contains S2 with the vowels interchanged.  Example Input/Output 1: Input: ( ) apple rain Output: appli raen Explanation:   Here S1 = apple and S2 = rain .  After interchanging the vowels in the string values in their order of occurrence, the string values become  S1 = appli and S2 = raen .  Example Input/Output 2: Input: ( ) PANCAKE Umbrella Output: PUNCeKa AmbrAllE SOLUTION : C (Programming Language) Copy Code #include<stdio.h> #include<s...