#5 Functions
#5 Functions
Add
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 64 megabytes
Input
Only one line contains two numbers X and Y (0 ≤ X, Y ≤ 105 ).
Output
Print the summation value.
Example
standard input standard output
5 2 7
Page 1 of 1
,
Print
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 64 megabytes
Input
Only one line contains a number N (1 ≤ N ≤ 103 ).
Output
print numbers form 1 to N separated by a single space.
Example
standard input standard output
5 1 2 3 4 5
Note
Don’t use any leading or trilling spaces.
Page 1 of 1
Wonderful Number
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 64 megabytes
Note:
A number is wonderful if this number is odd and its binary representation is palindrome.
Solve this problem using two functions.
Input
Only one line contains a number N (0 ≤ N ≤ 109).
Output
Print "YES" if the number is wonderful otherwise, print "NO".
Examples
standard input standard output
3 YES
4 NO
Note
For more information visit Palindrome Number: https://en.wikipedia.org/wiki/Palindromic_number
Page 1 of 1
,
Prime Function
Input file: standard input
Output file: standard output
Time limit: 2 seconds
Memory limit: 256 megabytes
A prime number is a number that is greater than 1 and has only two factors which are 1 and itself. The
first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.
Given a number N . Determine whether N is prime or not.
Note: Solve this problem using function.
Input
First line will contain a number T (1 ≤ T ≤ 103 ) number of test cases.
Next T lines will contain a number N (1 ≤ N ≤ 109 ).
Output
Print “YES” if the Nth number is prime otherwise, print “NO”.
Example
standard input standard output
3 YES
2 NO
4 NO
8
Note
Don’t use an array.
Page 1 of 1
,
Swap
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 64 megabytes
Input
Only one line contains two numbers X and Y (0 ≤ X, Y ≤ 105 ).
Output
Print X and Y separated by a space after swapping.
Example
standard input standard output
5 2 2 5
Page 1 of 1
,
Equation
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Given two numbers X and N . Print the result of the following equation:
S = (X 0 − 1) + (X 2 ) + (X 4 ) + (X 6 ) + ............... + (X N )
Note: Solve this problem using function and don’t use built-in function.
Input
Only one line contains two numbers X and N (0 ≤ X, N ≤ 10)
Output
Print the equation result S.
Example
standard input standard output
5 5 650
Note
First Test :
(50 - 1) + 52 + 54 = 0 + 25 + 625 = 650.
Page 1 of 1
,
Given an array A of size N . Print the minimum and the maximum number in the array.
Note: Solve this problem using function.
Input
First line will contain a number N (1 ≤ N ≤ 103 ) number of elements.
Second line will contain N numbers (0 ≤ Xi ≤ 105 ).
Output
Print the minimum and the maximum number separated by a space.
Example
standard input standard output
5 1 95
10 13 95 1 3
Page 1 of 1
N Times
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Input
The first line contains a number T (1 ≤ T ≤ 50) the number of test cases.
Next T lines contains a number N and a character C (1 ≤ N ≤ 100).
Output
Print T lines, for every line print the character(C) N times separated by space.
Examples
standard input standard output
2 n
1 n O O O O O
5 O
1 z z z z z z z z
8 z
Page 1 of 1
Swapping With Matrix
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Given a matrix A of size N ∗ N . Print the Matrix after doing the following operations:
Input
First line contains three numbers N , X and Y (1 ≤ N ≤ 500, 1 ≤ X, Y ≤ N ).
Next N lines contain N numbers (−104 ≤ Ai,j ≤ 104 ).
Output
Print the answer required above.
Example
standard input standard output
4 3 1 1 7 7 2
1 2 3 -5 0 4 -5 3
-5 4 0 3 3 2 1 -5
7 7 1 2 5 6 40 11
40 6 5 11
Page 1 of 1
Average
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Input
First line will contain a number N (1 ≤ N ≤ 104 ) length of the array.
Second line will contain N numbers (1 ≤ Ai ≤ 103 ).
Output
Print the average(mean) of the array with 6 digits after the decimal point.
Examples
standard input standard output
3 2.6666667
1.0 2.0 5.0
4 5.2500000
1.0 7.0 4.0 9.0
Page 1 of 1
Shift Right
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Given an array A of size N and a number X. Print the array elements after shifting right the elements
X times.
For example: if A = [1, 2, 3] then after shifting it to the right for 1 time A = [3, 1, 2].
Note: Solve this problem using function.
Input
First line will contain two number N and X (1 ≤ N ≤ 104 , 1 ≤ X ≤ 100).
Second line will contain N number (1 ≤ Ai ≤ 105 ).
Output
Print the array after shifting right its elements X times.
Examples
standard input standard output
2 2 2 1
2 1
5 21 5 1 2 3 4
1 2 3 4 5
Page 1 of 1
New Array
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Given two arrays A and B of size N . Print a new array C that holds the concatenation of array B followed
by array A.
Note: Solve this problem using function.
Input
First line will contain a number N (1 ≤ N ≤ 103 ).
Second line will contain N numbers (1 ≤ Ai ≤ 105 ) array A elements.
Third line will contain N numbers (1 ≤ Bi ≤ 105 ) array B elements.
Output
Print array C elements separated by space.
Example
standard input standard output
2 3 4 1 2
1 2
3 4
Page 1 of 1
Distinct Numbers
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Given an array A of size N . Print the number of distinct numbers in the array.
Note: Solve this problem using function.
Input
First line will contain a number N (0 ≤ N ≤ 103 ) number of elements.
Second line will contain N numbers (−103 ≤ Ai ≤ 103 ).
Output
Print the number of distinct numbers in the array.
Examples
standard input standard output
3 2
1 2 2
5 4
1 8 9 2 9
Page 1 of 1
Shift Zeros
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Given an array A of size N . Print the array elements after shifting all zeroes in array A to the right.
Note: Solve this problem using function.
Input
First line will contain a number N (1 ≤ N ≤ 103 ) number of elements.
Second line will contain N numbers (0 ≤ Ai ≤ 103 ).
Output
Print the array after shifting right all its zeros.
Examples
standard input standard output
4 2 5 0 0
2 0 0 5
5 1 5 7 4 0
1 5 0 7 4
Page 1 of 1
Five in One
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
5. Get the number that has the maximum number of divisors, and if there are more than one
number that has the maximum number of divisors , print the maximum of them.
Note:
*A palindrome number is a number that reads the same forward or backward.
For example: 12321, 101 are palindrome numbers, while 1201, 221 are not.
*A prime number is a number that is greater than 1 and has only two factors which are 1 and itself.
In other words : prime number divisible only by 1 and itself.
Be careful that 1 is not prime .
The first few prime numbers are
Page 1 of 3
Input
First line will contain a number N (1 ≤ N ≤ 100) number of elements.
Second line will contain N numbers (1 ≤ Ai ≤ 100).
Output
Print five lines as following:
“The maximum number : X “ where X is the maximum number.
“The minimum number : X “ where X is the minimum number.
“The number of prime numbers : X “ where X is the number of prime numbers.
“The number of palindrome numbers : X “ where X is the number of palindrome numbers.
“The number that has the maximum number of divisors : X “ where X is the number that has the
Page 2 of 3
maximum number of divisors.
Don’t print any extra spaces.
Examples
standard input standard output
4 The maximum number : 8
1 2 5 8 The minimum number : 1
The number of prime numbers : 2
The number of palindrome numbers : 4
The number that has the maximum number of divisors : 8
5 The maximum number : 83
8 2 14 1 83 The minimum number : 1
The number of prime numbers : 2
The number of palindrome numbers : 3
The number that has the maximum number of divisors : 14
Note
In the second example :
the minimum number is 1.
the maximum number is 83 .
the prime numbers are [2,83].
the palindrome numbers are [1,2,8].
1 has one divisor [1], 2 has two divisors are [1,2],
8 has four divisors [1,2,4,8],14 has also four divisors [1,2,7,14],and 83 has two divisors [1,83].
then 8 and 14 have the maximum number of divisors so we print the maximum one 14.
Page 3 of 3