Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions disappearednumbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> result;// array to store the missing numbers
int n = nums.size();
for(int i = 0 ; i < n ; i++){
int index = abs(nums[i]) - 1; // get the index corresponding to the current number
if(nums[index] > 0){
nums[index] *= -1;//if +ve make it -ve
}
}
for(int i = 0 ; i < n ; i++){
if(nums[i]>0){
result.push_back(i+1);//if +ve add to the result array
}else{
nums[i] *= -1;//restores the values back to +ve
}
}
return result;

}
};
54 changes: 54 additions & 0 deletions gameoflife.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m = board.size();
int n = board[0].size();

for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
int count = countalive(board,i,j);

if((board[i][j] == 1) && (count < 2 || count > 3)){
board[i][j] = 2;
}
if((board[i][j] == 0) && (count == 3)){
board[i][j] = 3;
}
}
}

for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
if(board[i][j] == 2){
board[i][j] = 0;
}
if(board[i][j] == 3){
board[i][j] = 1;
}
}
}
}

private :
int countalive(vector<vector<int>>& board,int i,int j){
int count = 0;


vector<vector<int>> direction = {
{0,-1},{-1,0},{1,0},
{-1,-1},{-1,1},{1,-1},{1,1},{0,1}
};

for(auto &dir : direction){
int nrow = i + dir[0];
int ncolumn = j + dir[1];

if(nrow >= 0 && ncolumn >= 0 &&
nrow < board.size() && ncolumn < board[0].size() &&
(board[nrow][ncolumn] == 1 || board[nrow][ncolumn] == 2)){
count++;
}
}
return count;
}
};