BCSE102 – Structured and Object Oriented Programming
Mr. Ganesh is a II year student in VIT Chennai. He scored well in many subjects but he is not
an expert in Data Structures and Algorithms. His exam is scheduled for next week. As per the
blue print, many questions are asked from the array. So, he collected the previous year
question papers, one of the repeated questions is that you need to find the pairs in array with
the given sum. Please help Ganesh to solve the problem.
Sample test case
5 2 3 4 1 6 7
Sum to be checked: 7
Pairs: 5 2
3 4
1 6
Count: 3
Constraints:
0< n<100
0<arr<1000
Input format
First line has the value of n that is the total elements in array
Second line indicates the elements of array
Third line tells about the elements to be checked
Output format
Output contains many lines as number of pairs with each pair written in each line
Last line of output consists of total count of pairs
Testcase 1
Input
5234167
7
Expected output
[5 2]
[3 4]
[1 6]
Total Number of Pairs:3
Testcase 2
Input
10 15 20 25 30 35 40 45 50
50
Output
[10 40]
[15 35]
[20 30]
Total Number of Pairs: 3
Saravanan with his friends going to the theatre for a movie. The seating arrangement is
triangular in size. Theatre staff insisted the audience to sit in odd if the seat number is odd
and in even row if the seat number is even. But the instruction is very confusing for
Saravanan and his friends. Help him with the seat layout so that they can sit in correct seat.
Constraints.
4<=N<=20
Input format
Only line of input has single integer value representing the number of rows in the theatre.
Output format
Print the layout based on the number of rows specified in input
INPUT (STDIN)
17
EXPECTED OUTPUT
24
135
2468
13579
2 4 6 8 10 12
1 3 5 7 9 11 13
2 4 6 8 10 12 14 16
1 3 5 7 9 11 13 15 17
2 4 6 8 10 12 14 16 18 20
1 3 5 7 9 11 13 15 17 19 21
2 4 6 8 10 12 14 16 18 20 22 24
1 3 5 7 9 11 13 15 17 19 21 23 25
2 4 6 8 10 12 14 16 18 20 22 24 26 28
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33
Test Case 2
INPUT (STDIN)
EXPECTED OUTPUT
24
135
2468
13579
2 4 6 8 10 12
1 3 5 7 9 11 13
2 4 6 8 10 12 14 16
1 3 5 7 9 11 13 15 17
Given two binary strings, A (of length 10) and B (of length 5), output 1 if B is a substring of
A and 0 otherwise.
Please note, that the solution may only be submitted in the following languages:
Brainf**k, Whitespace and Intercal.
Input
24 lines consisting of pairs of binary strings A and B separated by a single space.
Output
The logical value of: 'B is a substring of A'.
Example
First two lines of input:
1010110010 10110
1110111011 10011
First two lines of output:
1
0
Problem Description
Develop a program that reads in ten characters and displays the number of distinct characters
and the distinct characters separated by exactly one space (i.e., if a character appears multiple
times, it is displayed only once). After the input, the array contains the distinct characters in
uppercase. Note: both uppercase (A) and lowercase (a) letters are considered as the same
character.
Input Format
First line contains the number of characters n.
Second line contains n characters separated by space.
Output Format
First line displays the number of distinct characters.
Second line displays the array of distinct characters in uppercase.
Test Case 1
Input
10
ABCBAFEDBA
Output
ABCFED
Test Case 2
Input
BcAXx
Output
BCAX
Test Case 3
Input
12
XyBcApqaPCRS
Output
XYBCAPQRS
(Hint: Read a number and store it to an array if it is new. If the number is already in the array,
ignore it.)
Write the following method that tests whether the array has four consecutive numbers with
the same value.
public static boolean isConsecutiveFour(int[] values)
Write a test program that prompts the user to enter a series of integers and displays if the
series contains four consecutive numbers with the same value.
Input Format
First line contains the input size n.
Each of next n lines contains the values in the series
Output Format
The list has consecutive fours
Test Case 1
input
10
3678888958
Output
The list has consecutive fours
input
8
34555545
Output
The list has consecutive fours
input
8
34529554
Output
The list has no consecutive fours
Problem Description
The bean machine, also known as a quincunx or the Galton box, is a device for statistics
experiments named after English scientist Sir Francis Galton. It consists of an upright board
with evenly spaced nails (or pegs) in a triangular form, as shown in Figure. Balls are dropped
from the opening of the board. Every time a ball hits a nail, it has a 50% chance of falling to
the left or to the right. The piles of balls are accumulated in the slots at the bottom of the
board.
Write a program that simulates the bean machine. Your program should prompt the user to
enter the number of the balls and the number of the slots in the machine. Simulate the falling
of each ball by printing its path. For example, the path for the ball in Figure (b) is LLRRLLR
and the path for the ball in Figure (c) RLRRLRR. Display the final buildup of the balls in the
slots in a histogram.
Input Format
First line contains the number of balls N
Second line contains number of slots in the bean machine S
Output Format
Prints the path of each ball while falling in N lines.
Next line contains the final buildup of the balls in the slots in a histogram.
Test Case 1:
Input
5
8
Output
LRLRLRR
RRLLLRR
LLRLLRR
RRLLLLL
LRLRRLR
O
O
OOO
Test Case 2
Input
3
8
Output
RRLLLRL
RRLLLLL
LLLRLLL
O
OO
OOO
(Hint: Create an array named slots. Each element in slots stores the number
of balls in a slot. Each ball falls into a slot via a path. The number of Rs in
a path is the position of the slot where the ball falls. For example, for the path
LRLRLRR, the ball falls into slots[4], and for the path is RRLLLLL, the ball
falls into slots[2].)
Problem Description
There is a collection of input strings and a collection of query strings. For each query string,
determine how many times it occurs in the list of input strings. Return an array of the results.
Example
Strings=[ab, abc,ab]
Queries=[ab,bc,abc]
There are 2 instances of 'ab’, 0 of 'bc' and 1 of 'abc'. For each query, add an element to the
return array,
Input Format
The first line contains and integer n, the size of strings[] .
Each of the next n lines contains a string strings[i] .
The next line contains q , the size of queries[].
Each of the next q lines contains a string queries[i]
Output Format
Instances of queries[] array in separate lines
Public Test case 1
Input
aba
baba
aba
xzxb
aba
xzxb
ab
Output
2
1
0
Public Test Case 2
3
def
de
fgh
3
de
lmn
fgh
Output
1
0
1
Problem Description
Given an array A[] of n distinct elements. Let S1 and S2 be the smallest and the next
smallest element in the interval [L,H] where
Where , are the bitwise operators AND, OR and XOR respectively. Your task is to
find the maximum possible value of R.
Input Format
First line contains integer n .
Second line contains n integers, representing elements of the array A[].
Output Format
Print the value of maximum possible value of S.
Private Test Case
Sample Input
5
96352
Sample Output
15
Explanation
Consider the interval [1,2] the result will be maximum.
=15
Public Test Case 1:
Sample Input
10
5472913680
Sample Output
The two roots of a quadratic equation ax2 + bx + c = 0 can be obtained using the following
formula: b2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the
equation has two real roots. If it is zero, the equation has one root. If it is negative,
the equation has no real roots. Write a program that prompts the user to enter values for a, b,
and c and displays the result based on the discriminant. If the discriminant is positive, display
two roots. If the discriminant is 0, display one root. Otherwise, display “The equation
has no real roots”. Note that you can use Math.pow(x, 0.5) to compute 2x.
Sample run
Enter a, b, c: 1.0 3 1
The equation has two roots -0.381966 and -2.61803
Write a program that prompts the user to enter an integer for today’s day of the week
(Sunday is 0, Monday is 1, …, and Saturday is 6). Also prompt the user to enter the number
of days after today for a future day and display the future day of the week.
sample run:
Enter today's day: 1
Enter the number of days elapsed since today: 3
Today is Monday and the future day is Thursday
Enter today's day: 0
Enter the number of days elapsed since today: 31
Today is Sunday and the future day is Wednesday
An ISBN-10 (International Standard Book Number) consists of 10 digits:
d1d2d3d4d5d6d7d8d9d10. The last digit, d10, is a checksum, which is calculated from the
other nine digits using the following formula:
(d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11
If the checksum is 10, the last digit is denoted as X according to the ISBN-10 convention.
Write a program that prompts the user to enter the first 9 digits and displays the 10-digit
ISBN (including leading zeros). Your program should read the input as an integer. Here are
sample runs:
Enter the first 9 digits of an ISBN as integer: 013601267
The ISBN-10 number is 0136012671
Enter the first 9 digits of an ISBN as integer: 013031997
The ISBN-10 number is 013031997X
Write a program that prompts the user to enter a three-digit integer and determines whether it
is a palindrome number. A number is palindrome if it reads the same from right to left and
from left to right.
Sample run
Enter a three-digit integer: 121
121 is a palindrome
Enter a three-digit integer: 123
123 is not a palindrome
A shipping company uses the following function to calculate the cost (in dollars) of shipping
based on the weight of the package (in pounds).
Write a program that prompts the user to enter the weight of the package and display the
shipping cost. If the weight is greater than 20, display a message “the package cannot be
shipped.”
Write a program that reads three edges for a triangle and computes the perimeter if the input
is valid. Otherwise, display that the input is invalid. The input is valid if the sum of every pair
of two edges is greater than the remaining edge.
Write a program that lets the user guess whether the flip of a coin results in heads or tails.
The program randomly generates an integer 0 or 1, which represents head or tail. The
program prompts the user to enter a guess and reports whether the guess is correct or
incorrect.
Write a program that displays a random coordinate in a rectangle. The rectangle is centered at
(0, 0) with width 100 and height 200.
Write a program in C to display the such a pattern for n number of rows using a number
which will start with the number 1 and the first and a last number of each row will be 1
Write a C program to remove the duplicates from a given array of integers