Programs With Sol

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28
At a glance
Powered by AI
Some key algorithms described are to find the count of a specific character in a string, reverse a given number, and print a matrix in spiral form.

An algorithm is described that uses a character count array to iterate through the string and increment the count for each character. It returns the character with the maximum count.

An algorithm is described that uses modulo and integer division to extract each digit of the number and add it to the reverse in reverse order to get the reversed number.

1). A company wishes to encode its data. The data is in the form of a string and is case sensitive.

They
wish to encode the data with respect to a specific character. They wish to count the number of times the
character reoccurs in the given data so that they can encode the data accordingly.

Write an algorithm to find the count of the specter in the given data.
Input

The first line of the input consists of a string data representing the data to be encoded.
The next line of the input consists of a character coder representing the character to be counted in the
data.
Output
Print an integer representing the count of the specific character.
Example
Input:
haveagoodday a
output:
3
Explanation:
The character “a” occurs thrice in the data. So, the output is 3.

Sample Testcases
Testcase 1 Input

Solution
char getMaxOccuringChar(char* str)
{

// Create array to keep the count of individual

// characters and initialize the array as 0

int count[ASCII_SIZE] = {0};

// Construct character count array from the input

// string.

int len = strlen(str);

int max = 0; // Initialize max count

char result; // Initialize result


// Traversing through the string and maintaining

// the count of each character

for (int i = 0; i < len; i++) {

count[str[i]]++;

if (max < count[str[i]]) {

max = count[str[i]];

result = str[i];

return result;
}

2). A company wishes to transmit data to another server. The data consists of numbers only. To secure
the data during transmission, they plan to reverse the data first. Write an algorithm to reverse the
data.

Sample Testcases
Testcase: 1 Input
52646459
Testcase: 1 Output
95464625

Solution

#include <stdio.h>

int main()
{
int n, reverse = 0;

printf("Enter a number to reverse\n");


scanf("%d", &n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}

printf("Reverse of entered number is = %d\n", reverse);

return 0;
}

3). Print a given matrix in spiral form


Given a 2D array. Print it in spiral form. See the following examples. Please comment down the code in
other languages as well below.
Input:
1 23 4
5 67 8
9 10 11 12
13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Input:
123 456
7 8 9 10 11 12
13 14 15 16 17 18
Output:
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11
Sample Testcases
Testcase: 1 Input
34
1234
5678
9 10 11 12
Testcase: 1 Output
1 2 3 4 8 12 11 10 9 5 6 7 6

Solution

#include <stdio.h>
#define R 3
#define C 6

void spiralPrint(int m, int n, int a[R][C])


{
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/

while (k < m && l < n) {


/* Print the first row from the remaining rows */
for (i = l; i < n; ++i) {
printf("%d ", a[k][i]);
}
k++;

/* Print the last column from the remaining columns */


for (i = k; i < m; ++i) {
printf("%d ", a[i][n - 1]);
}
n--;

/* Print the last row from the remaining rows */


if (k < m) {
for (i = n - 1; i >= l; --i) {
printf("%d ", a[m - 1][i]);
}
m--;
}

/* Print the first column from the remaining columns */


if (l < n) {
for (i = m - 1; i >= k; --i) {
printf("%d ", a[i][l]);
}
l++;
}
}
}

/* Driver program to test above functions */


int main()
{
int a[R][C] = { { 1, 2, 3, 4, 5, 6 },
{ 7, 8, 9, 10, 11, 12 },
{ 13, 14, 15, 16, 17, 18 } };

spiralPrint(R, C, a);
return 0;
}
4). PATTERN
PROGRAM TO PRINT NUMBER PATTERN 2
1
123
12345
1234567
123456789
1234567
12345
123
1
Sample Testcases
Testcase 1 Input
5
Testcase 1 Output
1
123
12345
1234567
123456789
1234567
12345
123
1

Solution
#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

// Iterate through upper half triangle of the pattern


for(i=1; i<=N; i++)
{
for(j=1; j<=(i * 2 - 1); j++)
{
printf("%d", j);
}

printf("\n");
}

// Iterate through lower half triangle of the pattern


for(i=N-1; i>=1; i--)
{
for(j=1; j<=(i * 2 - 1); j++)
{
printf("%d", j);
}

printf("\n");
}

return 0;
}

5). Pattern – star Pyramid


C program to print following pyramid pattern of stars n=5
*
***
*****
*******
*********
Input Format
Input contains n
Output format
Print the pattern
Constraints
1 < = n < = 20
Sample Testcases
Testcase 1 Input
5

Testcase 1 Output
*
***
*****
*******
*********

Solution

#include<stdio.h>
#define MAX 5

int main()
{
int i,j;
int space=4;
/*run loop (parent loop) till number of rows*/
for(i=0;i< MAX;i++)
{
/*loop for initially space, before star printing*/
for(j=0;j< space;j++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf("* ");
}

printf("\n");
space--; /* decrement one space after one row*/
}
return 0;
}

6). PROGRAM TO PRINT THE GIVEN NUMBER PATTERN


1
24
369
4 8 12 16
5 10 15 20 25
Sample Testcases
Testcase 1 Input
5
Testcase 1 Output
1
24
369
4 8 12 16
5 10 15 20 25

7). Compete Cell


There is a colony of 8 cells arranged in a straight line where each day every cell compees with its
adjacent cells(neighbor). Each day, for each cell, if its neighbours are both active or both inactive, the cell
becomes inactive the next day, otherwise it becomes active the next day.
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be
assumed to be always inactive. Even after updating the cell state. Consider its previous state for
updating the state of other cells. Update the cell information of all cells simultaneously. Write a function
cellCompete which takes takes one 8 element array of integers cells representing the current state of 8
cells and one integer days representing the number of days to simulate. An integer value of 1 represents
an active cell and value of 0 represents an inactive cell.

Input Format
Input will have 8 array values and the no of days
Output Format
Print the array

Constraints
Array size is 8 integers
Sample Testcases
Testcase 1 Input
10000100
1
Testcase 1 Output
01001010

8). Count the occurrence of substring


Find the occurrence of a sub string in a parent string
Input Format
Input contains the string and the sub string
Output Format
Print the count
Constraints
1<=substring_length <= string_length<= 1000
Sample Testcases
Testcase 1 input
hgjghjhab
ab
Testcase 1 Output
1
Solution
include <stdio.h>
#include <string.h>
char str[100], sub[100];
int count = 0, count1 = 0;

void main()
{
int i, j, l, l1, l2;

printf("\nEnter a string : ");


scanf("%[^\n]s", str);

l1 = strlen(str);

printf("\nEnter a substring : ");


scanf(" %[^\n]s", sub);

l2 = strlen(sub);

for (i = 0; i < l1;)


{
j = 0;
count = 0;
while ((str[i] == sub[j]))
{
count++;
i++;
j++;
}
if (count == l2)
{
count1++;
count = 0;
}
else
i++;
}
printf("%s occurs %d times in %s", sub, count1, str);
}

9). Least Recently Used


\The LeastRecentlyUsed(LRU) cache algorithm exists the element from the cache(when it’s full) that was
leastrecentlyused. After an element is requested from the cache, it should be added to the cache(if not
already there) and considered the most recently used element in the cache.

Initially, the cache is empty. The input to the function LruCountMiss shall consist of an integer max_
cache_size, an array pages and its length len. The function should return an integer for the number of
cache misses using the LRU cache algorithm. Assume that the array pages always has pages numbered
from 1 to 50
Int lruCountMiss(int max_cache_size, int*pages, int len)
{/ /write tour code }

Input Format
Input consists oof an integer max_cache_size, array length len and an array pages
Output Format
An integeger for the number of cache misses using the LRU cache algorithm
Constraints
Should write a function
Sample Testcases

Testcase 1 Input
3 16
7012030423032120
Testcase 1 Output
11

Solution
include<stdio.h>
#include<conio.h>
int fr[3];
void main()
{
void display();
int p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
int index,k,l,flag1=0,flag2=0,pf=0,frsize=3;
clrscr();
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0,flag2=0;
for(i=0;i<3;i++)
{
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<3;i++)
{
if(fr[i]==p[k])
fs[i]=1;
}
}
for(i=0;i<3;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=p[j];
pf++;
}
display();
}
printf("\n no of page faults :%d",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<3;i++)
printf("\t%d",fr[i]);
}

10). Pattern
Write a ‘C’ program to print the pattern.
For n = 5
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
Input Format
Input represent the value n
Output Format
Print the pattern
Constraints
1 <= n <= 100
Sample Testcases
Testcase 1 Input
4
Testcase 1 Output
1
3*2
4*5*6
10*9*8*7

Testcase 2 Input
46
Testcase 1 Output
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
21*20*19*18*17*16
22*23*24*25*26*27*28
36*35*34*33*32*31*30*29
37*38*39*40*41*42*43*44*45
55*54*53*52*51*50*49*48*47*46
56*57*58*59*60*61*62*63*64*65*66

11). Pattern
You are given an integer N and a start value start, print2*N lines in the following manner
If N= 4 and start = 3, then the pattern would be:
3
44
555
6666
6666
555
44
3
The input to the method Increment PatternPrint of class IncrementPattern shall consist
of a positive integer start value start and an integer N (Assume 0 < N < 100).
Do not return anything from the method. Print the required pattern
Each line of the output shall consist of ‘numerals’ only. There should be no spaces.
Input Format
Input contains two integers n and s
Out Format
Print the required format
Constraints
1<=n,s<=100
Sample Testcases

Testcase 1 Input
35

Testcase 1Output
5
66
777
777
66
5
Testcase 2 Input
25

Testcase 1Output
5
66
66
5
12). Pattern
Given an integer N, Print N lines in the following manner –
For e.g. if N=6:
1111112
3222222
3333334
5444444
5555556
7666666
And so on.
The input to the method patternPrint of class NumberPattern shall consist of an integer
(Assume 1<N<100) representing the number of lines to be printed. Do not return anything from the
method.
Input Format
Input contains n
Output Format
Print the pattern
Constraints
1<=n <=50
Sample Testcases
Testcase 1 Input
4
Testcase 1 Output
11112
32222
33334
54444
13).Pattern
You are given an integer N. print 2W lines in the following manner-If N = 4, then the pattern would be.
1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
The input to the method triangle Pattern Print of class Triangle Pattern shall consist of an integer N
(Assume O<=N<= 1000)
Test Case 1:
Input: 3
Expected Output:
1
2*3
4*5*6
4*5*6
2*3
1
Test Case 2:
Input:5
Expected Return Value:
1
2*3
4*5*6
7*8*9*10
11*12*13*14*15
11*12*13*14*15
7*8*9*10
4*5*6
2*3
1
Input Format
Input contains the value n
Output Format
Print the required format
Constraints
1<=n<=55
Sample Testcases
Testcase 1 Input
4
Testcase 1Output
1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
14). A company provides network encryption for secure data transfer. The data string is encrypted
prior to transmission and gets decrypted at the receiving end. But due to some technical error, the
encrypted data is lost and the received string is different from the original string by 1 Character.
Arnold, a network administrator, is tasked with finding the character that got lost in the network so
that the bug does not harm other data that is being transferred through the network.
Write an algorithm to help Arnold find the character that was missing at the receiving end but present at
the sending end.
Sample Testcases
Testcase 1 input

abcdefghij abcdefghi
Testtcase 1 output
j
Testtcase 2 Input
Aaaabaaaa aaaaaaaa
Testtcase 2 output
B
15). Circular Linked List
Write a function to insert an integer into a circular linked_list whose elements are sorted in ascending
order 9smallest to largest). The input to the function insertedList is a pointer start to some node in the
circular list and an integer n between 0 and 100. Return a pointer to the newly inserted node. The
structure to follow for a node of the circular linked list is
Struct CNode;
Typedef struct CNode cnode;
Struct CNode
{
I nt value;
Conode* next;
};
Cnode* insertSortedList(conde* start,int n)
{
/ /WRITE YOUR CODE HERE

}/ /
Input Format
Input contains the values. -1 determines the end of the input
Output Format
Print the list

Sample Testcases

Testcase 1 Input
5 4 1 2 3 -1
Time Limit: 0 ms

Testcase 1 Output
34512

16). Helf Reversed Lind List


Reverse the second half of an input linked list. If the input linked list contained odd number of elements,
consider the middlemost element too min the second half
Input Format
Input contains the numbers . -1 denotes the end
Output Format
Print the list
Sample Testcases
Testcase 1 Input
5 7 1 4 3 2 -1
Testcase 1 output
571234
Testcase 2 Input
8 45 12 67 91 4 2 78 16 26 30 7 1 -1
Testcase 2 Output
8 45 12 67 91 4 1 7 30 26 16 78 2
17). Maximum number of 0’s
A n-by-n matrix of 0’s and 1’s is given, where all 1’s in each row come before all 0’s, find the most
efficient way to return the row with the maximum number of 0’s.

Semple Testcases
Testcase 1 input
33
111
101
100
Testcase 1 Output
2
18). Moshak Mouse
Mooshak the mouse has been placed in a maze. There is a huge chunk of cheese somewhere in the
maze. The maze is represented as two dimensional array of integers, where 0 represents
Walls. 1 repersents paths where mooshak can move and and 9 represents the huge chunk of cheese.

Mooshak starts in the top left corner at 0. Write a method is Path of class Maze Path to determine if
Mooshak can reach the huge chunk of cheese. The input to is Path consists of a two dimensional array
gnd for the maze matrix. The method should return 1 if there is a path from Mooshak to the cheese. and
0 if not Mooshak is not allowed to leave the maze or climb on walls.

EX: 8 by 8(8*8) matrix maze where Mooshak can get the cheese.
10111001
10001111
10000000
10109011
11101001
10101101
10000101
11111111

Input Format
The input to is Path consists of a two dimensional array size and the values for the maze matrix
Output Format
Retun 1 if there is a path and 0 if not
Sample Testcases
Testcase 1 Input
8
11111001
10011111
11100001
10101011
11101001
11101109
10000001
11111111
Testcase 1 Output
1
19). PATTERN
PROGRAM TO PRINT THE GIVEN NUMBER PATTERN
N=5
1
11
101
1001
11111
Input Format
Input Contains n
Output Format
Print thepattern
Sample Testcases
Testcase 1 Input
5
Testcase 1 Output
1
11
101
1001
11111

Solution
#include <stdio.h>
int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

for(i=1; i<=N; i++)


{
for(j=1; j<=i; j++)
{
if(i==1 || i==N || j==1 || j==i)
{
printf("1");
}
else
{
printf("0");
}
}

printf("\n");
}

return 0;
}

20).Pattern
Given an integer N. Print N lines in the following manner –
If N = 4
The pattern generated would be –
1*2*3*4*17*18*19*20
-5*6*7*14*13*16
.. ……8*9*12*13*
………….10*11
The input to the function trapepeziumPatternPrint shall consist of an integer N (Assume o<=N <= 100)
Do not return anything from the function. Print the required pattern using cout
Each line of the output shall consist of ‘numerals’. “and ‘-‘ only There should be no spaces.
Useful Commands:-
Cout prints the content to the screen.
Input Format
Input contains n
Output Format
Print the pattern
Constraints
1 <= n <= 50
Sample Testcases
Testcase 1 Input
4
Testcase 1 Output

1*2*3*4*17*18*19*20
-5*6*7*14*15*16
.. ……8*9*12*13*
………….10*11

SOLUTION
Trapezium Problem-SimpleWay2Code

C Program Solution:

#include <stdio.h>
int main(void) {

int num = 4;
int space;
int i, j, lterm, rterm;

// The terms on the LHS of the pattern

lterm = 1;

// The terms on the RHS of the pattern

rterm = num * num + 1;

for (i = num; i > 0; i--) {

// To print number of spaces

for(space = num;space > i;space--)


printf(" ");

for(j = 1; j <= i; j++) {

printf("%d",lterm);

printf("*");

lterm++;

for (j = 1; j <= i; j++) {


printf("%d",rterm);

if (j < i)

printf("*");

rterm++;

// To get the next term on RHS of the Pattern

rterm = rterm - (i - 1) * 2 - 1;

printf("\n");

}
return 0;
}

21). Prime Numbers Below the Range


Given a positive integer num, write a program to print all the prime numbers from 2 to num.
A prime number is a number that is divisible only by 1 and the number itself.
The input to the method prime Print of class Prime consists of the input number num. Print all the prime
numbers from 2 to num, each separated by a single space. Do not return anything from the method.
Please ensure that the output should only consist of numbers separated by a single space.
For example
Num: 11
Output; 235711
Make sure that your class and method are public. Do not accept any input from the console.
They shall be passed as arguments to the method itself.
Useful Commands:
a%b returns the remainder when a is divided by b.
System. Out. Print() prints the content within the brackets to the screen.
Testcase 1:
Input:
11
Excepted return value
2 3 5 7 11
Testcase 2:
Input
4
Excepted return value
23
Input Format
Input contains the value n
Output Format
Print the numbers separated by space
Constraints
1<= n<= 10000007
Sample Testcases
Testcase 1 Input
10
Testcase 1 Output
2357
22). Program to check if two given matrices are identical
Sample Testcases
Testcase 1 input
33
111
222
333
111
222
333
Testcase 1 output
Matrices are identical
Testcase 2 Inlut
25
12345
6 7 8 9 10
11111
23455
Testcase2 Output
Matrices are not identical
23). Pythagorean triplet
A Pythagorean triplet is a set of three integers a, b and c such that a 2 + b2 = c2. Given a limit,
Generate all Pythagorean Triples with values smaller than given limit. Limit = 20.
First, understanding the question is important. In this question, The output required is
345
8 6 10
5 12 13
15 8 17
12 16 20
This is because, 32 + 42 =52
Sample Testcases
Testcase 1 Input
20
Testcase 1 output
345
8 6 10
5 12 13
15 8 17
12 16 20
Testcase 2 Input
70
Testcase 2 output
345
8 6 10
5 12 13
15 8 17
12 16 20
7 24 25
24 10 26
21 20 29
16 30 34
9 40 41
35 12 37
33 34 40

Solution
#include<stdio.h>
main(){
int initial,final,a,b,c;
printf("Enter the range in which you want to search for Pythagorean Triplets:\nInitial: ");
scanf("%d",&initial);
printf("\nFinal: ");
scanf("%d",&final);
printf("The Pythogorean Triplets in the given range are as
follows:\n____________________________________________________________\n");
for(a=initial;a<=final;a++){
for(b=a;b<=final;b++){
for(c=b;c<=final;c++){
if(c*c==a*a+b*b){
printf("%d , %d , %d\n",a,b,c);
}
}
}
}
}
PAPER – 7
1). Writex
Nature provides us with much. But when we abuse nature, We risk disaster. Write a response explaining
how we harm ourselves when we harm the environment.
Directions
Write essay on the given topic
Keywords
1). Matrix Rotation
An image is representing m*n matrix of integers, where each integer represents a pixel Value. Write an
algorithm to rotate an image by 90 degree left or right according to the value of flag variable. If r=the flag
value is 0, then rotate to the left and if flag value is 1, then rotate to the right
Case 1:
Flag = 1
Input:
231
463
54 2
Output: 5 4 2
463
231
Case 2:
Flag = 0
Input:
21
34
Output:
41
32
Complete rotatePixellmage( Int **arr, int m ,int n , int flag) to get the desired output.
Input Format
The input to the method consist of four arguments
Img, a matrix of integers representing the pixels of the image
Rows, an integer representing the no of rows(m)
Columns, an integer representing the no of columns(n)
Flag, an integer representing the rotation of the image

Output Format
Return a matrix of integers representing the pixels of the image rotated according to the value of the flag
variable
Constraints
1≤ array- size≤ 1000
Sample Testcases
Testcase 1 input
331
231
463
542
Testcase 1 Output
542
463
231
1). An e-commerce website wishes to find the lucky customer who will be eligible for full value cash
back. For this purpose, a number N is fed to the system. It will return another number that is calculated
by an algorithm. In the algorithm, a sequence is generated, in which each number is the sum of the two
preceding numbers. Initially the sequence will have two 1’s in it. The system will return the Nth number
from the generated sequence which is treated as the order
ID. The lucky customer will be the one who has placed that order.
Write an algorithm to help the website find the lucky customer.
Sample Testcases
Testcase 1 Input
8
Testcase 1 output
21

PAPER – 6
1). An e-commerce website wishes to find the lucky customer who will be eligible for full value cash
back. For this purpose, a number N is fed to the system. It will return another number that is calculated
by an algorithm. In the algorithm, a sequence is generated, in which each number is the sum of the two
preceding numbers. Initially the sequence will have two 1’s in it. The system will return the Nth number
from the generated sequence which is treated as the order
ID. The lucky customer will be the one who has placed that order.
Write an algorithm to help the website find the lucky customer.
Sample Testcases
Testcase 1 Input
8
Testcase 1 output
21

PAPER – 4
1). You are given an integer N, print N+1 lines in the following manner
Case 1: If N=3, then the pattern would be –
333
313
323
333
Case 2: If N=4, then the pattern would be-
44444
44144
44244
44344
44444
Testcase 1:
2). In a science research lab, combining two nuclear chemicals produces a maximum energy that is the
product of the energy of the two chemicals. The energy values of the chemicals can be negative or
positive. The scientist wishes to calculate the sum of the maximized energies of the two elements when
the reaction happens.
Write an algorithm to find the total energy produced by the chemicals when they combine
Sample Testcases
Testcase 1 Input
6
-2 7 6 9 -3 -4
Testcase 1 Output
16

PAPER – 3
1). Remove Vowels
Given a string str, write a program to eliminate all the vowels from it.
The list of vowels In the English alphabet is : {a,e,I,o,u,A,E,I,O,U}

The Input to the function eliminate VowelString shall consist of a string str (containing only English
letters) and returns a pointer to a string which does not contain vowels.
Example:
Input =”abcdefghijklmnopqrstuvwxyz” I
Output=”bcdfghjklmnpqrstvwxyz”
Useful Commands:
Strlen() is used to calculate the length of the string. The statement – int len + strlen ( Str);
Returns the length of the string str
Input Fornst
Input contains the string
Out Format
Print the altered string
Constraints
1< + string_length<+ 1000
Sample Testcases
Testcase 1 Input
gAztkTJkCcmUVphMtGEDcWMMLSccLPvrMyLKTYYhkCYfZAiTDJKUSEfSWnntw
Testcase 1 Output
gztkTJkCcmVphMtGDcWMMLSccLPvrMyLKTYYhkCYFZTDJKSfSwnntWVywLKXt

2). A company is transmitting data to another server. The data is in the from of numbers. To secure the
data during transmission, They plan to obtain a security key that will be sent along with the sata. The
security key is identified as the count of the repeating digits in the data’
Write an algorithm to find the security key for the data.
Sample Testcases
Testcase 1 Input
1234234345
Testcase 1 Output
3
PAPER – 2
LightsOn
The Government of SumpLand has launched a special metro train which has N number of compartments
of different lengths as per the needs of the people in SumoLand. The metro has to cross a dark
underpass of length L. Due to the scarcity of electricity, it may be only a few compartments in which the
light are initially turned On and the rest have the lights turned OFF. While the metro passes through the
underpass, there should be light in atleast one of the compartment that is under the underpass at a
given point of time. If not, then some of the lights have to be turned ON in the train. Due to the scarcity
of electricity the number of compartments that have the light turned ON should be minimized
Write an algorithm to calculate the minimum number of compartments in which the light has to be
turned ON
Input Format
The input to the function/method consists of four arguments –
numCompart, an integer representing the number of compartments(N).
lenUnderPass , an integer representing the length of the bark underpass(L)
lenComparts, a list of integers representing the length of the metro compartments.
numLightON, a list of integers of 0s and 1s representing the compartments in which the lights are
initially turned ON and turned OFF

Input Format
Return an integer representing the minimum number of compartments in which the light is turned ON
Constraints
1<= numCmpart <= 106
1<= lenUnderPass <= 109
1<= lenComparts[i] <= 109
0<= numLightOn[j] <= 1
0<= i < numCompart
0<= j < numCompart

Sample Testcases
Testcase 1 Input

7 10
5345999
1000100
Testcase 1 Output
2

Time Limit: 0 ms

2). REVERSE THE ORDER OF WORDS


PROGRAM TO REVERSE THE ORDER OF WORSDS IN A GIVEN STRING. Complete the function/method
reverseWord(char string[], int len) to get the desired output.

Input Format
Input contains astring
Output Format
Print the reversed string
Sample Testcases

Testcase 1 Input
This is a test sentence

Testcase 1 Output
Sentence test a is This

PAPER – 1
1).Given a binary Tree having positive and negative nodes, the task is to find maximum sum level in it.
Sample Testcases
Testcase 1 input
1 2 3 4 5 -1
Testcase 1 output
9

You might also like