You are on page 1of 5

Logics for problem set -1

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;

// There should be at least two elements


if (n < 2) {
return -1;
}

first = second = INT_MIN;


for (i = 0; i < n; i++) {

// If current element is greater


// than first then update both
// first and second
if (arr[i] > first) {
second = first;
first = arr[i];
}

// If arr[i] is in between first


// and second then update second
else if (arr[i] > second && arr[i] != first) {
second = arr[i];
}
}
if (second == INT_MIN)
return -1;
else
return 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;
}

bool isToepliz(int A[MAX][MAX],int N,int M)


{

for (int i = 0; i < M; i++)


{
if (!ok(A, 0, i,N,M))
return false;
}

for (int i = 1; i < N; i++)


{

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;
}

//We use a counter variable to count number of set bits.


int count = 0;
while(n>0)
{
//Increment of counter variable if we get 1 as the
rightmost bit.
count+=(n&1);
//Right Shift n to remove the rightmost bit.
n>>=1;
}
//returning true if number of set bits is 1 otherwise false.
return count==1;

}
};

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;
}

//We use a counter variable to count number of set bits.


int count = 0;
while(n>0)
{
//Increment of counter variable if we get 1 as the
rightmost bit.
count+=(n&1);
//Right Shift n to remove the rightmost bit.
n>>=1;
}
//returning true if number of set bits is 1 otherwise false.
return count==1;

}
};
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;

//Right Shift n by 1 to remove the rightmost bit.


n >>= 1;

}
return count;
}

// Function to find number of bits needed to be flipped to convert


A to B
int countBitsFlip(int a, int b){

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;

//returning the number of set bits in resultant.


return countSetBits(ans);

}
};

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;

for(int i = 0; i < n;i++)


{
//if size of priority queue becomes equal to k,
if(q.size() == k)
{
//if top element is smaller than arr[i], we pop the
front
//element from priority queue and push arr[i] in
priority queue.
if(q.top() < arr[i])
{
q.pop();
q.push(arr[i]);
}
}
//else we just push arr[i] in priority queue.
else
q.push(arr[i]);
}

//while priority queue is not empty, we keep storing the top


element
//in list and keep popping it from the priority queue.
while(!q.empty())
v.push_back(q.top()), q.pop();

//reversing the list and returning it.


reverse(v.begin(),v.end());
return v;
}
};

You might also like