Applied Maths

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

FizzBuzz

Write a program that prints the numbers in the given range. But for multiples of three
print “Fizz” instead of the number and for the multiples of five print “Buzz”. For
numbers which are multiples of both three and five print “FizzBuzz”. Print a new line
after each string or number.

Input Format First line will be the number of testcases, T. Next line will have T
integers, denoted by N.

Output Format For each testcase, print the number from 1 to N. But follow the rules
given in the problem statement.

Constraints

1 <= T <= 10

N is an integer.

Please read the below instructions carefully

1. You can choose any language from the given list to write your solution.
2. All input to the programming solution is to STDIN and output is to STDOUT.
3. You don't have to manually give the input to your program, just take the input
from STDIN and the code evaluation engine will provide the input to your
program.
4. For example if you are coding in C, and the first input is an integer then simply
do scanf('%d', &i) assuming you are reading that integer to a variable
named i. Similarly if you are using C++ simply do cin >> i
5. There are two different type of test cases. First type is the sample input and
output for which you know both the input and output. You can look at them
under the problem statement.
6. When you click Compile and Test the code will be compiled and tested only
on the sample input that is shown to you. Compile and Test is for you to
understand if you solution is compiling and running against the sample input.
5 When you click submit, your code will be judged on multiple test cases
which are hidden. These tests are not available for you to see them so you
will not know the input on which your code will be tested. But it is assured that
all inputs will be in the given constraint limit and in the given format as stated
in the problem statement.
7. In order for your code to get accepted, it must clear all the judge test cases. In
cases where partial marking is allowed, you will awarded partial marks for the
number of test cases your solution passes.
8. Please note that getting green mark when you hit 'Compile and Test' does not
indicate anything on the correctness of your program. It just indicates that
your code correctly compiled and ran successfully against the sample input. It
can still fail for other test inputs that visible to you.
9. Do not output anything, except what it is asked for in the output
section. Note that you have to output only in the way that is mentioned. Any
extra strings in the output will be treated as wrong answer. Even an extra
space can lead to the answer not being accepted.
10. Don't assume any constraints on the input based on the sample input that you
see, the actual test cases will be much larger in size. But they will always be
within the constraints mentioned in the problem.
11. To understand how the code is evaluated visit the judge page. There is also a
sample code in each language there.

To further understand how the judge works, look at one of the actual test input file
and the corresponding expected output for this problem

Input File

Expected Output

You can also look at the solution that passes all test cases for this problem.

View C Solution

View Java Solution

Sample Input
2
3 15
Sample Output
1
2
Fizz
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

class TestClass {
public static void main(String args[] ) throws Exception {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


int t = Integer.parseInt(br.readLine());
int a[] = new int[t];
StringTokenizer st = new StringTokenizer(br.readLine());
int i = 0;
while(st.hasMoreTokens()){
a[i++] = Integer.parseInt(st.nextToken());
}

for (int j = 0; j < t; ++j) {


int num = a[j];
for (int k = 1; k <= num; ++k) {
if (k % 15 == 0)
System.out.println("FizzBuzz");
else if (k % 3 == 0)
System.out.println("Fizz");
else if (k % 5 == 0)
System.out.println("Buzz");
else
System.out.println(k);
}
}

}
}
K Vowels
You are given a string S of length N and an integer K. The string contains alphabets,
denoting different types of candies. The lower case and upper case alphabets
denote same type of candies, i.e. A and a denotes same type of candy. There are
some special candies denoted by vowels. You want to eat all the candies but there
are two restrictions.

1. You have to pick continous candies, i.e. a substring of the given string S.
2. The picked candies must have exactly K distinct special candies.

You have to find the maximum number of candies you can eat.

NOTE: The vowels are 'a', 'e', 'i', 'o' and 'u'. String S only contains lower and upper
case alphabets.

INPUT:
The first line of input contains an integer T denoting the number of test cases.

Each testcase consists of two lines of input.

The first line of each testcase consist of two integers N and K.

The next line of the testcase contains the string S.

OUTPUT:
Print the maximum number of candies you can eat.

Constraints:

 1 ≤ T ≤ 10
 1 ≤ N ≤ 106
 0≤K≤5
Special Array Operation
You are given an array A of size N. You can perform an operation in which you will
remove the largest and the smallest element from the array and add their difference
back into the array. So, the size of the array will decrease by 1 after each oepration.
You are given Qtasks and in each task, you are given an integer K. For each task,
you have to tell sum of all the elements in the array after K operations.

Input:

First line contains two space-separated integers N and Q, denoting the number of
elements in array and number of queries respectively.

Next line contains N space-separated integers denoting elements of the array.

Next Q lines contain a single integer K.

Output:

For each task, print answer in a new line.

Constraints:

2 <= N <= 105

1 <= Q <= 105

0 <= A[i] <= 109

0 <= K < N

Sample Input
5 2
3 2 1 5 4
1
2
Sample Output
13
9
Explanation

After 1st operation, the array will become, A = [3,2,4,4]. So, sum of elements = 13.

After 2nd operation, the array will become, A = [3,2,4]. So, sum of elements = 9.

You might also like