You are on page 1of 5

PRACTICE PROBLEMS

1) Given an array Arr of size N, print second largest distinct element from an


array.

Example 1:

Input:
N=6
Arr[] = {12, 35, 1, 10, 34, 1}
Output: 34
Explanation: The largest element of the
array is 35 and the second largest element
is 34.

2) Given an array Arr of N positive integers. Your task is to find the elements


whose value is equal to that of its index value ( Consider 1-based indexing ).

Note: There can be more than one element in the array which have the same value
as its index. You need to include every such element's index. Follows 1-
based indexing of the array.

Example 1:

Input:
N=5
Arr[] = {15, 2, 45, 12, 7}
Output: 2
Explanation: Only Arr[2] = 2 exists here.

3) For a given 3 digit number, find whether it is armstrong number or


not. An Armstrong number of three digits is an integer such that the sum of
the cubes of its digits is equal to the number itself. Return "Yes" if it is a
armstrong number else return "No".
NOTE: 371 is an Armstrong number since 33 + 73 + 13 = 371

Example 1:

Input: N = 153
Output: "Yes"
Explanation: 153 is an Armstrong number
since 13 + 53 + 33 = 153.
Hence answer is "Yes".

4) Given an array Arr of size N, swap the Kth element from beginning


with Kth element from end.

Example 1:

Input:
N = 8, K = 3
Arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output: 1 2 6 4 5 3 7 8
Explanation: Kth element from beginning is
3 and from end is 6.

5) A Toeplitz (or diagonal-constant) matrix is a matrix in which each


descending diagonal from left to right is constant, i.e., all elements in a
diagonal are same.
Given a matrix A of order N X M your task is to complete the
function isToeplitz which returns true if the matrix is Toeplitz otherwise
returns false
Example 1:

Input: 3 3 6 7 8 4 6 7 1 4 6
Output: 1
Explanation:
The test case represents a 3x3 matrix which looks like
678
467
146
Output: 1(True) as values in all diagonals are same.

6) Given a non-negative integer N. The task is to check if N is a power of 2.


More formally, check if N can be expressed as 2x for some x.

Example 1:

Input: N = 1
Output: YES
Explanation:1 is equal to 2
raised to 0 (20 = 1).

7) Given an array Arr of N elements and a integer K. Your task is to return the


position of first occurence of K in the given array.
Note: Position of first element is considered as 1.

Example 1:

Input:
N = 5, K = 16
Arr[] = {9, 7, 2, 16, 4}
Output: 4
Explanation: K = 16 is found in the
given array at position 4.

8) You are given two numbers A and B. The task is to count the number of
bits needed to be flipped to convert A to B.

Example 1:

Input: A = 10, B = 20
Output: 4
Explanation:
A  = 01010
B  = 10100
As we can see, the bits of A that need
to be flipped are 01010. If we flip
these bits, we get 10100, which is B.

9) Given non-zero two integers N and M. The problem is to find the number


closest to N and divisible by M. If there are more than one such number,
then output the one having maximum absolute value.

Example 1:

Input:
N = 13 , M = 4
Output:
12
Explanation:
12 is the Closest Number to
13 which is divisible by 4.

10) Given an array of N positive integers, print k largest elements from


the array. 

Example 1:

Input:
N = 5, k = 2
arr[] = {12,5,787,1,23}
Output: 787 23
Explanation: First largest element in
the array is 787 and the second largest
is 23.

Logic Of these practice set will given later in c

You might also like