Programs With Sol
Programs With Sol
Programs With Sol
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)
{
// string.
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;
return 0;
}
Solution
#include <stdio.h>
#define R 3
#define C 6
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);
printf("\n");
}
printf("\n");
}
return 0;
}
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;
}
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
void main()
{
int i, j, l, l1, l2;
l1 = strlen(str);
l2 = strlen(sub);
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
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);
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;
lterm = 1;
printf("%d",lterm);
printf("*");
lterm++;
if (j < i)
printf("*");
rterm++;
rterm = rterm - (i - 1) * 2 - 1;
printf("\n");
}
return 0;
}
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
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