JXNVCE ALGO-LOG YouJin Jung

GFG MOBILE NUMERIC KEYPAD PROBLEM

#include <iostream>
 
using namespace std;

int row[] = {0, 0, -1, 0, 1};
int col[] = {0, -1, 0, 1, 0};

int getCountUtil(char keypad[][3], int i, int j, int n) {
    if (keypad == NULL || n <= 0)
        return 0;
    if (n == 1)
        return 1;
 
    int k=0, move=0, ro=0, co=0, totalCount = 0;
    for (move=0; move<5; move++) {
        ro = i + row[move];
        co = j + col[move];
        if (ro >= 0 && ro <= 3 && co >=0 && co <= 2 && keypad[ro][co] != '*' && keypad[ro][co] != '#') {
            totalCount += getCountUtil(keypad, ro, co, n-1);
        }
    }
 
    return totalCount;
}

int getCount(char keypad[][3], int n) {
    if (keypad == NULL || n <= 0)
        return 0;
    if (n == 1)
        return 10;
 
    int i=0, j=0, totalCount = 0;
    for (i=0; i<4; i++) {
        for (j=0; j<3; j++) {
            if (keypad[i][j] != '*' && keypad[i][j] != '#') {
                totalCount += getCountUtil(keypad, i, j, n);
            }
        }
    }
    return totalCount;
}

int main(){
   /*char keypad[4][3] = [{'1','2','3'},
                        {'4','5','6'},
                        {'7','8','9'},
                        {'*','0','#'}];*/
   printf("Count for numbers of length %d: %dn", 1, getCount(keypad, 1));
   printf("Count for numbers of length %d: %dn", 2, getCount(keypad, 2));
   printf("Count for numbers of length %d: %dn", 3, getCount(keypad, 3));
   printf("Count for numbers of length %d: %dn", 4, getCount(keypad, 4));
   printf("Count for numbers of length %d: %dn", 5, getCount(keypad, 5));
 
   return 0;
}

GFG SWAP TWO NUMBERS -

#include <bits/stdc++.h>

using namespace std;
 
int main() {
    int x = 10, y = 5;
    x = x + y; // x now becomes 15
    y = x - y; // y becomes 10
    x = x - y; // x becomes 5
    cout << "After Swapping: x =" << x << ", y=" << y;
}

GFG SWAP TWO NUMBERS -

swapping numbers without temporary variable

#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
    int x = 10, y = 5; // 10=1010, 5=0101
    x = x ^ y; // x now becomes 15 (1111)
    y = x ^ y; // y becomes 10 (1010)
    x = x ^ y; // x becomes 5 (0101)
    cout << "After Swapping: x =" << x << ", y=" << y;
    return 0;
}

BOJ-17298

#include <iostream>
#include <stack>
#include <vector>
using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
  cin.tie(NULL); cout.tie(NULL);

	int n;
	cin >> n;
	
	vector<int> arr(n + 1);
	stack<int> st;
	vector<int> nge(n + 1, -1);

	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
	}

	for (int i = 1; i <= n; i++) {
		while (!st.empty() && arr[st.top()] < arr[i]) {
			nge[st.top()] = arr[i];
			st.pop();
		}
		st.push(i);
	}

	for (int i = 1; i <= n; i++)
		cout << nge[i] << " ";
    
	cout << endl;
}

Gfg Tokenizing String


layout: post title: 2022-02-15 TOKENIZING STRING —

tokenizing string with stringstream! 🙆🏼‍♀️

#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
    string line = "Hello World !! !!";
    vector <string> tokens;
    stringstream check(line);
    string s;

    while(getline(check, s, ' '))
        tokens.push_back(s);
     
    for(int i = 0; i < tokens.size(); i++)
        cout << tokens[i] << '\n';
}