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
41 changes: 41 additions & 0 deletions coinprob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

#include <bits/stdc++.h>
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<int> 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;
}
100 changes: 100 additions & 0 deletions nqueen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <bits/stdc++.h>
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;
}
20 changes: 20 additions & 0 deletions palindromeORnot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <bits/stdc++.h>
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;
}
28 changes: 28 additions & 0 deletions printallduplicates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//using map
#include <bits/stdc++.h>
using namespace std;

void fun(string str)
{

int n = str.length();
map<char, int> 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);
}
79 changes: 79 additions & 0 deletions rat_maze.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <bits/stdc++.h>
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;
}
24 changes: 24 additions & 0 deletions reversestring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <bits/stdc++.h>
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;
}
30 changes: 30 additions & 0 deletions stringsarerotation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
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;
}