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
23 changes: 23 additions & 0 deletions Problem Solving/linearSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include <iostream>
using namespace std;

int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}

int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = search(arr, n, x);
(result == -1)? cout<<"Element is not present in array"
: cout<<"Element is present at index " <<result;
return 0;
}
1 change: 1 addition & 0 deletions Problem Solving/nqueen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

116 changes: 116 additions & 0 deletions Problem Solving/ratinamaze.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* C/C++ program to solve Rat in
a Maze problem using backtracking */
#include <stdio.h>

// Maze size
#define N 4

bool solveMazeUtil(
int maze[N][N], int x,
int y, int sol[N][N]);

/* A utility function to print
solution matrix sol[N][N] */
void printSolution(int sol[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf(" %d ", sol[i][j]);
printf("\n");
}
}

/* A utility function to check if x,
y is valid index for N*N maze */
bool isSafe(int maze[N][N], int x, int y)
{
// if (x, y outside maze) return false
if (
x >= 0 && x < N && y >= 0
&& y < N && maze[x][y] == 1)
return true;

return false;
}

/* This function solves the Maze problem
using Backtracking. It mainly uses
solveMazeUtil() to solve the problem.
It returns false if no path is possible,
otherwise return true and prints the path
in the form of 1s. Please note that there
may be more than one solutions, this
function prints one of the feasible
solutions.*/
bool solveMaze(int maze[N][N])
{
int sol[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };

if (solveMazeUtil(
maze, 0, 0, sol)
== false) {
printf("Solution doesn't exist");
return false;
}

printSolution(sol);
return true;
}

/* A recursive utility function
to solve Maze problem */
bool solveMazeUtil(
int maze[N][N], int x,
int y, int sol[N][N])
{
// if (x, y is goal) return true
if (
x == N - 1 && y == N - 1
&& maze[x][y] == 1) {
sol[x][y] = 1;
return true;
}

// Check if maze[x][y] is valid
if (isSafe(maze, x, y) == true) {
// mark x, y as part of solution path
sol[x][y] = 1;

/* Move forward in x direction */
if (solveMazeUtil(
maze, x + 1, y, sol)
== true)
return true;

/* If moving in x direction
doesn't give solution then
Move down in y direction */
if (solveMazeUtil(
maze, x, y + 1, sol)
== true)
return true;

/* If none of the above movements
work then BACKTRACK: unmark
x, y as part of solution path */
sol[x][y] = 0;
return false;
}

return false;
}

// driver program to test above function
int main()
{
int maze[N][N] = { { 1, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 0 },
{ 1, 1, 1, 1 } };

solveMaze(maze);
return 0;
}
82 changes: 82 additions & 0 deletions Problem Solving/reverse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Iterative C++ program to reverse
// a linked list
#include <iostream>
using namespace std;

/* Link list node */
struct Node {
int data;
struct Node* next;
Node(int data)
{
this->data = data;
next = NULL;
}
};

struct LinkedList {
Node* head;
LinkedList()
{
head = NULL;
}

/* Function to reverse the linked list */
void reverse()
{
// Initialize current, previous and
// next pointers
Node* current = head;
Node *prev = NULL, *next = NULL;

while (current != NULL) {
// Store next
next = current->next;

// Reverse current node's pointer
current->next = prev;

// Move pointers one position ahead.
prev = current;
current = next;
}
head = prev;
}

/* Function to print linked list */
void print()
{
struct Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}

void push(int data)
{
Node* temp = new Node(data);
temp->next = head;
head = temp;
}
};

/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
LinkedList ll;
ll.push(20);
ll.push(4);
ll.push(15);
ll.push(85);

cout << "Given linked list\n";
ll.print();

ll.reverse();

cout << "\nReversed Linked list \n";
ll.print();
return 0;
}
112 changes: 112 additions & 0 deletions Problem Solving/sudokuSolver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@

// A recursive program in C++
// to solve Sudoku problem
#include <bits/stdc++.h>
using namespace std;

// UNASSIGNED is used for empty
// cells in sudoku grid
#define UNASSIGNED 0

// N is used for the size of Sudoku grid.
// Size will be NxN
#define N 9

// Checks whether it will be legal
// to assign num to the given row, col
bool isSafe(int grid[N][N])
{
// Hashmap for row column and boxes
unordered_map<int, int>
row_[9], column_[9], box[3][3];
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
// mark the element in row column and box
row_[row][grid[row][col]] += 1;
column_[col][grid[row][col]] += 1;
box[row / 3][col / 3][grid[row][col]] += 1;

// if an element is already
// present in the hashmap
if (
box[row / 3][col / 3][grid[row][col]] > 1
|| column_[col][grid[row][col]] > 1
|| row_[row][grid[row][col]] > 1)
return false;
}
}
return true;
}

/* A utility function to print grid */
void printGrid(int grid[N][N])
{
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++)
cout << grid[row][col] << " ";
cout << endl;
}
}

/* Takes a partially filled-in grid and attempts
to assign values to all unassigned locations in
such a way to meet the requirements for
Sudoku solution (non-duplication across rows,
columns, and boxes) */
bool SolveSudoku(
int grid[N][N], int i, int j)
{
// if the index reached the end
if (i == N - 1 && j == N) {
// if the matrix is safe
if (isSafe(grid)) {
// print and stop
printGrid(grid);
return true;
}

// else try other cases
return false;
}

// end of a row move to next row
if (j == N) {
i++;
j = 0;
}

// if the element is non zero keep as it is
if (grid[i][j] != UNASSIGNED)
return SolveSudoku(grid, i, j + 1);

// consider digits 1 to 9
for (int num = 1; num <= 9; num++) {
// assign and call recursively
grid[i][j] = num;

if (SolveSudoku(grid, i, j + 1))
return true;

grid[i][j] = 0;
}
return false;
}

// Driver Code
int main()
{
// 0 means unassigned cells
int grid[N][N] = { { 3, 1, 6, 5, 7, 8, 4, 9, 2 },
{ 5, 2, 9, 1, 3, 4, 7, 6, 8 },
{ 4, 8, 7, 6, 2, 9, 5, 3, 1 },
{ 2, 6, 3, 0, 1, 5, 9, 8, 7 },
{ 9, 7, 4, 8, 6, 0, 1, 2, 5 },
{ 8, 5, 1, 7, 9, 2, 6, 4, 3 },
{ 1, 3, 8, 0, 4, 7, 2, 0, 6 },
{ 6, 9, 2, 3, 5, 1, 8, 7, 4 },
{ 7, 4, 5, 0, 8, 6, 3, 1, 0 } };
if (SolveSudoku(grid, 0, 0) != true)
cout << "No solution exists";

return 0;
}
43 changes: 43 additions & 0 deletions rainWaterTapping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// C++ implementation of the approach
#include<bits/stdc++.h>
using namespace std;

// Function to return the maximum
// water that can be stored
int maxWater(int arr[], int n)
{

// To store the maximum water
// that can be stored
int res = 0;

// For every element of the array
for (int i = 1; i < n-1; i++) {

// Find the maximum element on its left
int left = arr[i];
for (int j=0; j<i; j++)
left = max(left, arr[j]);

// Find the maximum element on its right
int right = arr[i];
for (int j=i+1; j<n; j++)
right = max(right, arr[j]);

// Update the maximum water
res = res + (min(left, right) - arr[i]);
}

return res;
}

// Driver code
int main()
{
int arr[] = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
int n = sizeof(arr)/sizeof(arr[0]);

cout << maxWater(arr, n);

return 0;
}
Loading