Skip to content

Commit 1343f42

Browse files
authored
Merge branch 'smv1999:master' into master
2 parents 39be21a + 2ae3107 commit 1343f42

File tree

15 files changed

+1587
-0
lines changed

15 files changed

+1587
-0
lines changed

Arrays/Fair Playoff.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Problem Statement:
2+
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament.
3+
4+
It is known that in a match between two players, the one whose skill is greater will win. The skill of the i-th player is equal to si and all skill levels are pairwise different (i. e. there are no two identical values in the array s).
5+
6+
The tournament is called fair if the two players with the highest skills meet in the finals.
7+
8+
Determine whether the given tournament is fair.
9+
10+
Example:
11+
12+
Input:
13+
4
14+
3 7 9 5
15+
4 5 6 9
16+
5 3 8 1
17+
6 5 3 2
18+
19+
Output:
20+
YES
21+
NO
22+
YES
23+
NO
24+
*/
25+
26+
#include<iostream>
27+
using namespace std;
28+
29+
int main()
30+
{
31+
long int T;
32+
cin>>T;
33+
34+
while(T--)
35+
{
36+
int s[4], i, f1 = 0, f2 = 0, sm = 0, no = 0, yes = 0;
37+
for(i = 0; i < 4; i++)
38+
cin>>s[i]; //a 1-D array to store the skills of 4 players
39+
40+
if(s[0] > s[1]) //comparing first player with second
41+
f1 = s[0];
42+
43+
else
44+
f1 = s[1];
45+
46+
if(s[2] > s[3]) //comparing third player with fourth
47+
f2 = s[2];
48+
49+
else
50+
f2 = s[3];
51+
52+
53+
if(f2 - f1 > 0)
54+
sm = f1;
55+
else
56+
sm = f2;
57+
58+
59+
for(i = 0; i < 4; i++)
60+
{
61+
if(s[i] > sm) //to check if the finals were fair or not
62+
yes++; //assign 'yes' if the players with highest skills are in finals
63+
else
64+
no++; //assign 'no' if the players with highest skills were not in finals
65+
}
66+
if(yes == 1)
67+
cout<<"YES"<<endl; //print 'YES' for a fair playoff
68+
else
69+
cout<<"NO"<<endl; //print 'NO' for an unfair playoff
70+
}
71+
return 0;
72+
}

Backtracking/N_Queens.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
'''
2+
Aim: To place N queens in a N*N Chessboard such that no two queens
3+
attack each other. A queen is said to be attacked by another queen
4+
if they share same diagonal(right/left), Row or Column.
5+
Intution: Since there could be only one queen in each row, we can assume the
6+
N*N chessboard to be a 1d array which each index denotes one of the
7+
row and the row value denotes the column. Now in each row, we will
8+
put a queen and check whether it is possible or not. If possible, then
9+
we recursively check for the next row. If its not possible to place
10+
a queen in any of the column is a particular row, then we backtrack
11+
and try next Column.
12+
13+
'''
14+
15+
# Main function argument =size of the board
16+
def n_queens(board_size):
17+
18+
# Occupied Diagonals and Columns
19+
# For right and left Diagonal respectively
20+
diagonal1 = {}
21+
diagonal2 = {}
22+
Col = {}
23+
24+
ans = place_queen(0, [], board_size, diagonal1, diagonal2, Col)
25+
26+
return ans
27+
28+
# Recursive Function to check and place the queens
29+
def place_queen(row, a, n, diagonal1, diagonal2, Col):
30+
31+
# If the answer is found, row will be equal to the size of the board i.e. n
32+
if(row == n):
33+
return a
34+
R = row + 1
35+
36+
for C in range(1, n + 1):
37+
# Check that particular Column is free to place a queen or not
38+
if((C not in Col) and ((R + C) not in diagonal1) and ((R - C) not in diagonal2)):
39+
40+
# Add the Column and their respective Diagonals to the dictionary
41+
# to mark they are Occupied
42+
Col[C] = 0
43+
diagonal1[R + C] = 0
44+
diagonal2[R - C] = 0
45+
chk = place_queen(
46+
row + 1, a + [(row, C - 1)], n, diagonal1, diagonal2, Col)
47+
48+
# If the answer is found, Stop the recursion
49+
if chk:
50+
return chk
51+
52+
# Deleaating the Column and Diagonals to vacant that place
53+
del diagonal1[R + C]
54+
del Col[C]
55+
del diagonal2[R - C]
56+
57+
return False
58+
59+
60+
# -------------------------------Driver Code-------------------------------
61+
62+
if __name__ == "__main__":
63+
n = int(input("Enter the Board Size: "))
64+
answer = n_queens(n)
65+
66+
if not answer:
67+
print("Queens cannot be placed in the given Chessboard")
68+
69+
else:
70+
print("Queens are Placed in the chessboard")
71+
print("Position :", *answer)
72+
73+
'''
74+
Sample Working:
75+
76+
Enter the Board Size: 3
77+
Queens cannot be placed in the given Chessboard
78+
79+
Enter the Board Size: 4
80+
Queens are Placed in the chessboard
81+
Position : (0, 1) (1, 3) (2, 0) (3, 2)
82+
83+
0 1 2 3
84+
+-------+-------+-------+-------+
85+
| | | | |
86+
0 | | X | | |
87+
| | | | |
88+
+---------------+-------+-------+
89+
| | | | |
90+
1 | | | | X |
91+
| | | | |
92+
+-------+-------+-------+-------+
93+
| | | | |
94+
2 | X | | | |
95+
| | | | |
96+
+-------+-------+-------+-------+
97+
| | | | |
98+
3 | | | X | |
99+
| | | | |
100+
+-------+-------+-------+-------+
101+
102+
Enter the Board Size: 8
103+
Queens are Placed in the chessboard
104+
Position : (0, 0) (1, 4) (2, 7) (3, 5) (4, 2) (5, 6) (6, 1) (7, 3)
105+
106+
COMPLEXITY:
107+
108+
Time Complexity: O(2^N)
109+
Space complexity: O(N)
110+
111+
'''

Backtracking/Subset_Sum.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
Aim: From a list of integers, check and return a set of integers whose sum
3+
will be equal to the target value K.
4+
5+
'''
6+
7+
# Main Recursive function to find the desired Subset Sum
8+
def Subset_Sum(li, target, ans=[]):
9+
10+
# Base Cases
11+
if target == 0 and ans != []:
12+
return ans
13+
14+
elif li == []:
15+
return False
16+
17+
# li[0] is not included in the answer Subset
18+
temp = Subset_Sum(li[1:], target, ans)
19+
if temp:
20+
return temp
21+
22+
# li[0] included in the answer Subset
23+
temp = Subset_Sum(li[1:], target - li[0], ans + [li[0]])
24+
25+
return temp
26+
27+
# --------------------------- DRIVER CODE------------------------------
28+
29+
if __name__ == "__main__":
30+
31+
li = [int(i) for i in input("Enter the List of Integers: ").split()]
32+
Target = int(input("Enter the Target value: "))
33+
34+
ans = Subset_Sum(li, Target)
35+
if not ans:
36+
print("No Subset Sum matched to the Target")
37+
else:
38+
print("The Required Subset is : ", *ans)
39+
40+
'''
41+
42+
Sample Input:
43+
Enter the List of Integers: -1 2 6 7 -4 7 5 -2
44+
Enter the Target value: 0
45+
46+
Sample Output:
47+
The Required Subset is : 6 -4 -2
48+
49+
Explanation:
50+
6 - 4 - 2 = 0, the required result
51+
52+
COMPLEXITY:
53+
54+
Time Complexity: O(2^N)
55+
Space complexity: O(N)
56+
57+
'''

Backtracking/graph_coloring.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*Write a program to implement the graph coloring problem with m=3 using back tracking method. Consider the
2+
following adjacency matrix for the graph.
3+
1 2 3 4
4+
1 0 1 1 1
5+
2 1 0 1 0
6+
3 1 1 0 1
7+
4 1 0 1 0
8+
*/
9+
#include<stdio.h>
10+
#define v 4
11+
void printsol(int color[]) {
12+
int i;
13+
printf("The assigned colors are: ");
14+
for (i = 0; i < v; i++)
15+
printf(" %d ", color[i]);
16+
printf("\n");
17+
}
18+
19+
int isSafe(int adj[v][v], int color[]) {
20+
int i,j;
21+
for (i = 0; i < v; i++)
22+
for (j = i + 1; j < v; j++)
23+
if (adj[i][j] && color[j] == color[i]){
24+
return 1;
25+
}
26+
return 0;
27+
}
28+
int graphcolor(int adj[v][v], int m, int i, int color[v]){
29+
int j;
30+
if (i == v){ // If we got all the vertex colored we check if it is safe
31+
if (isSafe(adj, color) == 0) {
32+
// If yes we print solution and return 0
33+
printsol(color);
34+
return 0;
35+
} else{
36+
return 1;
37+
}
38+
}
39+
//Recursively colors all the vertex
40+
for(j=1;j<=m;j++){
41+
color[i] = j;
42+
if (graphcolor(adj,m,i+1,color) == 0){ // Since we returned 0 i.e we got one solution we return the func
43+
return 0;
44+
}
45+
color[i] = 0; // If we didn't get safe solu (i.e return value is 1) we start again
46+
}
47+
return 1;
48+
}
49+
50+
int main(){
51+
int i,j,m;
52+
printf("Enter the number of colors [m] : ");
53+
scanf("%d",&m);
54+
int adj[v][v];
55+
printf("\nEnter values of the Adjacency Matrix: \n");
56+
for(i=0;i<v;i++){
57+
for(j=0;j<v;j++){
58+
printf("\nEnter the value of adj[%d][%d]: ",i,j);
59+
scanf("%d",&adj[i][j]);
60+
}
61+
printf("\n");
62+
}
63+
printf("\n\n");
64+
printf("Adjacency Matrix: \n");
65+
for(i=0;i<v;i++){
66+
for(j=0;j<v;j++){
67+
printf("%d ",adj[i][j]);
68+
}
69+
printf("\n");
70+
}
71+
printf("\nThe value of m is: %d \nThe value of v is: %d\n",m,v);
72+
73+
int color[v]; //Keeps record of the color, each vertex is colored with
74+
for (i = 0; i < v; i++){
75+
color[i] = 0; //first set all the nodes color as 0
76+
}
77+
78+
if (graphcolor(adj, m, 0, color) == 1){
79+
printf("Solution does not exist");
80+
}
81+
return 0;
82+
}
83+

DSA 450 GFG/TowerOfHanoi.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
/* Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
3+
4+
1. Only one disk can be moved at a time.
5+
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
6+
3. No disk may be placed on top of a smaller disk.
7+
*/
8+
9+
#include<iostream>
10+
using namespace std;
11+
12+
void towerofHanoi(int n, char src, char helper ,char dest){// number of element , source, helper, destination
13+
14+
15+
if(n==0){
16+
return; //base case
17+
}
18+
19+
towerofHanoi(n-1, src, dest, helper);// Move n-1 disks from source to helper
20+
cout<<"Move from "<<src<<" to "<<helper<<endl;
21+
towerofHanoi(n-1, dest, helper, src);// Move n-1 disks from destination to helper
22+
23+
24+
25+
}
26+
27+
int main(){
28+
29+
towerofHanoi(3, 'A', 'B', 'C');// A = Source , B = helper, C = destination.
30+
31+
return 0;
32+
33+
34+
}
35+
/* Input = 3
36+
37+
Output:
38+
39+
Move from A to B
40+
Move from A to C
41+
Move from B to C
42+
Move from A to B
43+
Move from C to A
44+
Move from C to B
45+
Move from A to B
46+
*/

0 commit comments

Comments
 (0)