0% found this document useful (0 votes)
54 views2 pages

Rootquotient Coding

Uploaded by

Thirishaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views2 pages

Rootquotient Coding

Uploaded by

Thirishaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

No of Islands Coding Question :

class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int m = grid.size(), n = m ? grid[0].size() : 0, islands = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
islands++;
eraseIslands(grid, i, j);
}
}
}
return islands;
}
private:
void eraseIslands(vector<vector<char>>& grid, int i, int j) {
int m = grid.size(), n = grid[0].size();
if (i < 0 || i == m || j < 0 || j == n || grid[i][j] == '0') {
return;
}
grid[i][j] = '0';
eraseIslands(grid, i - 1, j);
eraseIslands(grid, i + 1, j);
eraseIslands(grid, i, j - 1);
eraseIslands(grid, i, j + 1);
}
};

Missing Number Coding Question:


int missingNumber(vector<int>& nums) {
int n=nums.size();
int res=n;
for(int i=0;i<n;i++){
res^=i;
res^=nums[i];
}
return res;
}

N Bags and K Divisons Coding Question:


#include <iostream>
#include <vector>

std::vector<int> distributeBags(int n, int k) {


// Initialize a vector with k divisions all starting with 0 bags
std::vector<int> divisions(k, 0);

// Distribute n bags across k divisions


for (int i = 0; i < n; ++i) {
divisions[i % k] += 1;
}

return divisions;
}

int main() {
int n = 10; // Number of bags
int k = 3; // Number of divisions
std::vector<int> result = distributeBags(n, k);

// Print the distribution of bags across the divisions


std::cout << "Distribution of bags: ";
for (int i = 0; i < result.size(); ++i) {
std::cout << result[i] << " ";
}
std::cout << std::endl;

return 0;
}

Reciprocal of Alphabet Coding Question:


void reciprcalString(string word)
{
char ch;
for (int i = 0; i < word.length(); i++) {
// converting uppercase character
// To reciprocal character
// display the character
if (isupper(word[i])) {
ch = 'Z' - word[i] + 'A';
cout << ch;
}

// converting lowercase character


// To reciprocal character
// display the character
else if (islower(word[i])) {
ch = 'z' - word[i] + 'a';
cout << ch;
}

else {
cout << word[i];
}
}
}

You might also like