Professional Documents
Culture Documents
1)
//Back-end complete function template for C++
class Solution{
public:
// Function returns the second
// largest elements
int print2largest(int arr[], int n) {
int i, first, second;
2)
class Solution{
public:
vector<int> valueEqualToIndex(int arr[], int n) {
vector<int> v;
for (int i = 0; i < n; i++) {
if (arr[i] == i + 1) {
v.push_back(i + 1);
}
}
return v;
}
};
3)
//Back-end complete function Template for C++
class Solution {
public:
string armstrongNumber(int n){
int a=n%10,b= (n/10)%10,c = n/100;
if(a*a*a + b*b*b +c*c*c == n)
return "Yes";
else
return "No";
}
};
4)
class Solution{
public:
// swap k'th element from beginning and end
void swapKth(int *arr, int n, int k) {
swap(arr[k - 1], arr[n - k]);
}
};
5)
bool ok(int mat[MAX][MAX], int i, int j,int N,int M)
{
int res = mat[i][j];
while (++i < N && ++j < M)
{
if (mat[i][j] != res)
return false;
}
return true;
}
if (!ok(A, i, 0,N,M))
return false;
}
return true;
}
6)
class Solution{
public:
// Function to check if given number n is a power of two.
bool isPowerofTwo(long long n){
if(n==0){
return false;
}
}
};
7)
class Solution{
public:
// Function to check if given number n is a power of two.
bool isPowerofTwo(long long n){
if(n==0){
return false;
}
}
};
8)
class Solution{
public:
// Function to count set bits
int countSetBits(unsigned int n)
{
//We use a counter variable
unsigned int count = 0;
while(n)
{
//AND operation of n and 1 gives us the rightmost bit.
//counter variable is increased if we get 1 as the rightmost
bit.
count += n & 1;
}
return count;
}
int ans = 0;
//XOR operation gives set bits only when there are dissimilar
bits.
//We store the value of XOR operation of a and b.
ans = a^b;
}
};
9) class Solution {
public:
int closestNumber(int N , int M) {
int q=N/M;
int n1=q*M;
int n2;
if(N*M>0)
n2=(M*(q+1));
else
n2=(M*(q-1));
if(abs(n2-N)>abs(N-n1))
return n1;
else
return n2;
}
};
10)
class Solution
{
public:
//Function to return k largest elements from an array.
vector<int> kLargest(int arr[], int n, int k)
{
//implementing MinHeap using priority queue.
priority_queue<int, vector<int>, greater<int> > q;
vector<int> v;