Skip to content

Commit 2fb620b

Browse files
committed
2 parents cc84e12 + 1343f42 commit 2fb620b

18 files changed

+1734
-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+
}

Arrays/RunningSumOf1dArray.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*Question:-
2+
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
3+
4+
Return the running sum of nums.
5+
6+
7+
8+
Example 1:
9+
10+
Input: nums = [1,2,3,4]
11+
Output: [1,3,6,10]
12+
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
13+
Example 2:
14+
15+
Input: nums = [1,1,1,1,1]
16+
Output: [1,2,3,4,5]
17+
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
18+
Constraints:
19+
20+
1 <= nums.length <= 1000
21+
-10^6 <= nums[i] <= 10^6
22+
*/
23+
#include<bits/stdc++.h>
24+
using namespace std;
25+
vector<int> runningSum(vector<int>& nums) {
26+
for(int i=1;i<nums.size();i++){
27+
nums[i]+=nums[i-1];
28+
}
29+
return nums;//returning the array
30+
}
31+
int main(){
32+
vector<int> nums;//to store elements
33+
int n,element;
34+
cin>>n;
35+
for (int i = 0; i < n; i++)
36+
{
37+
cin>>element;
38+
nums.push_back(element);//storeing the elements
39+
}
40+
vector<int> result=runningSum(nums);//calling the fuction to return the running sum of array
41+
for (int i = 0; i < result.size(); i++)
42+
{
43+
cout<<result[i]<<",";//print the elements of running sum array
44+
}
45+
46+
return 0;
47+
}

Arrays/move_zeroes_end_1traversal.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//Move all zeros to End
2+
//Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
3+
//Approach 2
4+
//O(n) O(1)
5+
6+
7+
// i/p: n=5, 0 1 0 3 12
8+
// o/p: 1 3 12 0 0
9+
10+
// Here we replace another loop with count
11+
12+
#include<iostream>
13+
using namespace std;
14+
void moveToEnd(int arr[],int n)
15+
{
16+
int count=0;
17+
for(int i=0;i<n;i++)
18+
{
19+
if(arr[i]!=0)
20+
{
21+
//if we get 0 we ignore it
22+
//and if we get non zero we swap with count which point at 0
23+
swap(arr[i],arr[count]);
24+
count++;
25+
26+
}
27+
}
28+
}
29+
30+
int main()
31+
{
32+
int n,i;
33+
cout<<"Enter the number of elements in array\n";
34+
cin>>n;
35+
int arr[n];
36+
cout<<"Enter the elements in array\n";
37+
for(i=0;i<n;i++)
38+
cin>>arr[i];
39+
moveToEnd(arr,n);
40+
cout<<"Array having zeroes at end\n";
41+
for(i=0;i<n;i++)
42+
cout<<arr[i]<<" ";
43+
44+
}

Arrays/move_zeroes_end_2traversal.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//Move all zeros to End
2+
//Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
3+
//Approach 1
4+
//O(n^2) O(1)
5+
6+
// i/p: n=5, 0 1 0 3 12
7+
// o/p: 1 3 12 0 0
8+
9+
/*
10+
Brute froce approach
11+
n=5
12+
traversals :
13+
0 0 1 2 3
14+
1 0 0 2 3
15+
1 2 0 0 3
16+
1 2 3 0 0
17+
*/
18+
19+
#include<iostream>
20+
using namespace std;
21+
void moveToEnd(int arr[],int n)
22+
{
23+
for(int i=0;i<n;i++)
24+
{
25+
//We 1st find is there any 0 present
26+
if(arr[i]==0)
27+
{
28+
for(int j=i+1;j<n;j++){
29+
//if non zero then swap with ith 0
30+
if(arr[j]!=0)
31+
{
32+
//swap both
33+
swap(arr[i],arr[j]);
34+
break;
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
42+
int main()
43+
{
44+
int n,i;
45+
cout<<"Enter the number of elements in array\n";
46+
cin>>n;
47+
int arr[n];
48+
cout<<"Enter the elements in array\n";
49+
for(i=0;i<n;i++)
50+
cin>>arr[i];
51+
moveToEnd(arr,n);
52+
cout<<"Array having zeroes at end\n";
53+
for(i=0;i<n;i++)
54+
cout<<arr[i]<<" ";
55+
56+
}

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+
'''

0 commit comments

Comments
 (0)