diff --git a/coinprob.cpp b/coinprob.cpp new file mode 100644 index 0000000..0cb30d5 --- /dev/null +++ b/coinprob.cpp @@ -0,0 +1,41 @@ + +#include +using namespace std; + +// All denominations of Indian Currency +int deno[] = { 1, 2, 5, 10, 20, + 50, 100, 500, 1000 }; +int n = sizeof(deno) / sizeof(deno[0]); + +void findMin(int V) +{ + sort(deno, deno + n); + + // Initialize result + vector ans; + + // Traverse through all denomination + for (int i = n - 1; i >= 0; i--) { + + // Find denominations + while (V >= deno[i]) { + V -= deno[i]; + ans.push_back(deno[i]); + } + } + + // Print result + for (int i = 0; i < ans.size(); i++) + cout << ans[i] << " "; +} + +// Driver program +int main() +{ + int n = 93; + cout << "Following is minimal" + << " number of change for " << n + << ": "; + findMin(n); + return 0; +} diff --git a/nqueen.cpp b/nqueen.cpp new file mode 100644 index 0000000..0b5be8a --- /dev/null +++ b/nqueen.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + +int isSafe(int **arr, int x, int y, int n) +{ + //check for column + for (int row = 0; row < x; row++) + { + if (arr[row][y] == 1) + { + return false; + } + } + + // for easy we are declaring variable + int row = x; + int col = y; + + //check for upper right diagonal + while (row >= 0 && col <= n) + { + if (arr[row][col] == 1) + { + return false; + } + row--; + col++; + } + //reintialixing + row = x; + col = y; + //check for upper left diagonal + while (row >= 0 && col >= 0) + { + if (arr[row][col] == 1) + { + return false; + } + row--; + col++; + } + return true; +} +//not taking y because we will not check for the same row + +int nQueen(int **arr, int x, int n) +{ + + //we have placed all the queens + if (x >= n) + { + return true; + } + for (int col = 0; col < n; col++) + { + if (isSafe(arr, x, col, n)) + { + arr[x][col] = 1; + + //now check for the other rows for queen placing + if (nQueen(arr, x + 1, n)) + { + return true; + } + + arr[x][col] = 0; //backtracking step if any one was found false + } + } + return false; +} + +int main() +{ + + int n; + cin >> n; + + int **arr = new int *[n]; + for (int i = 0; i < n; i++) + { + arr[i] = new int[n]; + for (int j = 0; j < n; j++) + { + arr[i][j] = 0; + } + } + + if (nQueen(arr, 0, n)) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cout << arr[i][j] << " "; + } + cout << endl; + } + } + return 0; +} \ No newline at end of file diff --git a/palindromeORnot.cpp b/palindromeORnot.cpp new file mode 100644 index 0000000..03c02b9 --- /dev/null +++ b/palindromeORnot.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; + +int main() +{ + + string str; + cin >> str; + int flag = 1; + int n = str.length(); + for (int i = 0; i < str.length(); i++) + { + if (str[i] != str[n - i - 1]) + { + flag = 0; + break; + } + } + cout << flag; +} \ No newline at end of file diff --git a/printallduplicates.cpp b/printallduplicates.cpp new file mode 100644 index 0000000..4fc5522 --- /dev/null +++ b/printallduplicates.cpp @@ -0,0 +1,28 @@ +//using map +#include +using namespace std; + +void fun(string str) +{ + + int n = str.length(); + map count; + for (int i = 0; i < n; i++) + { + count[str[i]]++; + } + + for (auto it : count) + { + if (it.second > 1) + cout << it.first << " count = " << it.second << "\n"; + } +} + +int main() +{ + string str; + cin >> str; + + fun(str); +} \ No newline at end of file diff --git a/rat_maze.cpp b/rat_maze.cpp new file mode 100644 index 0000000..c87eb4f --- /dev/null +++ b/rat_maze.cpp @@ -0,0 +1,79 @@ +#include +using namespace std; + +bool isSafe(int **arr, int x, int y, int n) +{ + + if (x < n && y < n && arr[x][y] == 1) + { + return true; + } + return false; +} + +bool ratInMaze(int **arr, int x, int y, int n, int **sol) +{ + if ((x == (n - 1)) && (y == (n - 1))) + { + sol[x][y] = 1; + return true; + } + if (isSafe(arr, x, y, n)) + { + sol[x][y] = 1; + if (ratInMaze(arr, x + 1, y, n, sol)) + { + return true; + } + if (ratInMaze(arr, x, y + 1, n, sol)) + { + return true; + } + sol[x][y] = 0; //bactracking + return false; + } + return false; +} + +int main() +{ + + int n; + cin >> n; + int **arr = new int *[n]; + for (int i = 0; i < n; i++) + { + arr[i] = new int[n]; + } + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cin >> arr[i][j]; + } + } + + int **sol = new int *[n]; + for (int i = 0; i < n; i++) + { + sol[i] = new int[n]; + for (int j = 0; j < n; j++) + { + sol[i][j] = 0; + } + } + + if (ratInMaze(arr, 0, 0, n, sol)) + { + for (int i = 0; i < n; i++) + { + for (int j = 0; j < n; j++) + { + cout << sol[i][j] << " "; + } + cout << endl; + } + } + + return 0; +} \ No newline at end of file diff --git a/reversestring.cpp b/reversestring.cpp new file mode 100644 index 0000000..0e9cb56 --- /dev/null +++ b/reversestring.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +int main() +{ + + string str; + cin >> str; + + int n = str.length(); + int end = n - 1; + int start = 0; + + while (start < n / 2) + { + int temp = str[start]; + str[start] = str[end]; + str[end] = temp; + + start++; + end--; + } + cout << str; +} \ No newline at end of file diff --git a/stringsarerotation.cpp b/stringsarerotation.cpp new file mode 100644 index 0000000..b7372d9 --- /dev/null +++ b/stringsarerotation.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int main() +{ + + string s1, s2; + cin >> s1 >> s2; + + int l1 = s1.length(); + int l2 = s2.length(); + + if (l1 != l2) + cout << "NO" << endl; + + else + { + string temp = s1 + s2; + + if (temp.find(s2) != string::npos) + { + cout << "YES" << endl; + } + + else + cout << "NO" << endl; + } + + return 0; +} \ No newline at end of file