Skip to content

Commit 4e53568

Browse files
authored
Merge branch 'master' into new-usera
2 parents 60a10aa + 632976f commit 4e53568

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2216
-7
lines changed

Ad-Hoc/(CODECHEF) ChefAndStrings.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
// your code goes here
6+
int t =1;
7+
cin >> t;
8+
while(t--){
9+
long long n;
10+
cin >> n;
11+
int arr[n];
12+
for(long long i=0;i<n;i++){
13+
cin >> arr[i];
14+
}
15+
long long sum=0;
16+
for(long long i=0;i<n-1;i++){
17+
long long ma = max(arr[i+1],arr[i]);
18+
long long mi= min(arr[i+1],arr[i]);
19+
if(ma!=mi){
20+
sum += abs((ma-1) -mi);
21+
}
22+
}
23+
cout << sum<<endl;
24+
}
25+
return 0;
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <map>
4+
using namespace std;
5+
6+
int main () {
7+
int t;
8+
string tempStr;
9+
bool isMagical;
10+
scanf ("%d", &t);
11+
for (int z = 0; z < t; z++) {
12+
isMagical = true;
13+
cin >> tempStr;
14+
for (int i = 0; i < tempStr.length(); i++)
15+
if (tempStr[i] != tempStr[tempStr.length()-1-i])
16+
isMagical = false;
17+
if (isMagical)
18+
cout << "YES\n";
19+
else
20+
cout << "NO\n";
21+
}
22+
}

Ad-Hoc/(SPOJ)BISHOPS.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//https://www.spoj.com/problems/BISHOPS/
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
5+
void mul(string s,string l,char *res)
6+
{
7+
int len1=s.length(),len2=l.length(),i,j;
8+
int carry=0,index=-1,maxin=-1;
9+
for(i=len1-1;i>=0;i--)
10+
{
11+
index=len1-i-2;
12+
for(j=len2-1;j>=0;j--)
13+
{
14+
int val=carry+(s[i]-'0')*(l[j]-'0');
15+
res[++index]+=val%10;
16+
carry=val/10;
17+
if(res[index]>'9')
18+
{
19+
res[index]=res[index]-'9'-1+'0';
20+
carry++;
21+
}
22+
}
23+
while(carry)
24+
{
25+
res[++index]+=carry%10;
26+
carry/=10;
27+
}
28+
if(index>maxin)
29+
maxin=index;
30+
}
31+
res[++maxin]='\0';
32+
}
33+
34+
int main()
35+
{
36+
string n;
37+
while(cin>>n)
38+
{
39+
if(n=="1"||n=="0")
40+
{
41+
if(n=="1")
42+
cout<<1;
43+
else
44+
cout<<0;
45+
cout<<endl;
46+
continue;
47+
}
48+
string s="2";
49+
char res[500];
50+
int i;
51+
for(i=0;i<500;i++)
52+
res[i]='0';
53+
mul(n,s,res);
54+
int len=strlen(res);
55+
i=0;
56+
int carry=0;
57+
res[0]-=2;
58+
while(i<len&&res[i]-carry<'0')
59+
{
60+
{
61+
res[i]=res[i]+10-carry;
62+
carry=1;
63+
}
64+
i++;
65+
}
66+
res[i]-=carry;
67+
i=len-1;
68+
while(i>=0&&res[i]=='0')
69+
i--;
70+
while(i>=0)
71+
{
72+
cout<<res[i--];
73+
}
74+
cout<<endl;
75+
}
76+
return 0;
77+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class WordDictionary {
2+
vector<WordDictionary*> children;
3+
bool isEndOfWord;
4+
public:
5+
/** Initialize your data structure here. */
6+
WordDictionary(): isEndOfWord(false) {
7+
children=vector<WordDictionary*>(26,nullptr);
8+
}
9+
10+
/** Adds a word into the data structure. */
11+
void addWord(string word) {
12+
WordDictionary *curr = this;
13+
for(char c:word){
14+
if(curr->children[c-'a'] == nullptr){
15+
curr->children[c-'a'] = new WordDictionary();
16+
}
17+
curr = curr->children[c-'a'];
18+
}
19+
curr->isEndOfWord =true;
20+
}
21+
22+
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
23+
bool search(string word) {
24+
WordDictionary *curr = this;
25+
for(int i=0;i<word.length();++i){
26+
char c= word[i];
27+
if(c=='.'){
28+
for(auto ch:curr->children){
29+
if(ch && ch->search(word.substr(i+1))) return true;
30+
}
31+
return false;
32+
}
33+
34+
if(curr->children[c-'a']==nullptr) return false;
35+
curr=curr->children[c-'a'];
36+
}
37+
return curr && curr->isEndOfWord;
38+
}
39+
};
40+
41+
/**
42+
* Your WordDictionary object will be instantiated and called as such:
43+
* WordDictionary* obj = new WordDictionary();
44+
* obj->addWord(word);
45+
* bool param_2 = obj->search(word);
46+
*/

Backtracking/Solve_Sudoku.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define UNASSIGNED 0
5+
6+
#define N 9
7+
8+
bool FindUnassignedLocation(int grid[N][N],
9+
int &row, int &col);
10+
11+
bool isSafe(int grid[N][N], int row,
12+
int col, int num);
13+
14+
bool SolveSudoku(int grid[N][N])
15+
{
16+
int row, col;
17+
18+
if (!FindUnassignedLocation(grid, row, col))
19+
20+
return true;
21+
22+
for (int num = 1; num <= 9; num++)
23+
{
24+
25+
if (isSafe(grid, row, col, num))
26+
{
27+
grid[row][col] = num;
28+
29+
if (SolveSudoku(grid))
30+
return true;
31+
32+
grid[row][col] = UNASSIGNED;
33+
}
34+
}
35+
return false;
36+
}
37+
38+
bool FindUnassignedLocation(int grid[N][N],
39+
int &row, int &col)
40+
{
41+
for (row = 0; row < N; row++)
42+
for (col = 0; col < N; col++)
43+
if (grid[row][col] == UNASSIGNED)
44+
return true;
45+
return false;
46+
}
47+
48+
bool UsedInRow(int grid[N][N], int row, int num)
49+
{
50+
for (int col = 0; col < N; col++)
51+
if (grid[row][col] == num)
52+
return true;
53+
return false;
54+
}
55+
56+
bool UsedInCol(int grid[N][N], int col, int num)
57+
{
58+
for (int row = 0; row < N; row++)
59+
if (grid[row][col] == num)
60+
return true;
61+
return false;
62+
}
63+
64+
bool UsedInBox(int grid[N][N], int boxStartRow,
65+
int boxStartCol, int num)
66+
{
67+
for (int row = 0; row < 3; row++)
68+
for (int col = 0; col < 3; col++)
69+
if (grid[row + boxStartRow]
70+
[col + boxStartCol] == num)
71+
return true;
72+
return false;
73+
}
74+
75+
bool isSafe(int grid[N][N], int row,
76+
int col, int num)
77+
{
78+
/
79+
return !UsedInRow(grid, row, num) &&
80+
!UsedInCol(grid, col, num) && !UsedInBox(grid, row - row % 3, col - col % 3, num) && grid[row][col] == UNASSIGNED;
81+
}
82+
83+
void printGrid(int grid[N][N])
84+
{
85+
for (int row = 0; row < N; row++)
86+
{
87+
for (int col = 0; col < N; col++)
88+
cout << grid[row][col] << " ";
89+
cout << endl;
90+
}
91+
}
92+
93+
int main()
94+
{
95+
// 0 means unassigned cells
96+
int grid[N][N] = {{3, 0, 6, 5, 0, 8, 4, 0, 0},
97+
{5, 2, 0, 0, 0, 0, 0, 0, 0},
98+
{0, 8, 7, 0, 0, 0, 0, 3, 1},
99+
{0, 0, 3, 0, 1, 0, 0, 8, 0},
100+
{9, 0, 0, 8, 6, 3, 0, 0, 5},
101+
{0, 5, 0, 0, 9, 0, 6, 0, 0},
102+
{1, 3, 0, 0, 0, 0, 2, 5, 0},
103+
{0, 0, 0, 0, 0, 0, 0, 7, 4},
104+
{0, 0, 5, 2, 0, 6, 3, 0, 0}};
105+
if (SolveSudoku(grid) == true)
106+
printGrid(grid);
107+
else
108+
cout << "No solution exists";
109+
110+
return 0;
111+
}

Backtracking/rat_in_a _maze.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <bits/stdc++.h>
2+
3+
#define N 4
4+
5+
bool solveMazeUtil(
6+
int maze[N][N], int x,
7+
int y, int sol[N][N]);
8+
9+
void printSolution(int sol[N][N])
10+
{
11+
for (int i = 0; i < N; i++)
12+
{
13+
for (int j = 0; j < N; j++)
14+
printf(" %d ", sol[i][j]);
15+
printf("\n");
16+
}
17+
}
18+
19+
bool isSafe(int maze[N][N], int x, int y)
20+
{
21+
if (
22+
x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1)
23+
return true;
24+
25+
return false;
26+
}
27+
28+
bool solveMaze(int maze[N][N])
29+
{
30+
int sol[N][N] = {{0, 0, 0, 0},
31+
{0, 0, 0, 0},
32+
{0, 0, 0, 0},
33+
{0, 0, 0, 0}};
34+
35+
if (solveMazeUtil(
36+
maze, 0, 0, sol) == false)
37+
{
38+
printf("Solution doesn't exist");
39+
return false;
40+
}
41+
42+
printSolution(sol);
43+
return true;
44+
}
45+
46+
bool solveMazeUtil(
47+
int maze[N][N], int x,
48+
int y, int sol[N][N])
49+
{
50+
if (
51+
x == N - 1 && y == N - 1 && maze[x][y] == 1)
52+
{
53+
sol[x][y] = 1;
54+
return true;
55+
}
56+
57+
if (isSafe(maze, x, y) == true)
58+
{
59+
sol[x][y] = 1;
60+
61+
if (solveMazeUtil(
62+
maze, x + 1, y, sol) == true)
63+
return true;
64+
65+
if (solveMazeUtil(
66+
maze, x, y + 1, sol) == true)
67+
return true;
68+
69+
sol[x][y] = 0;
70+
return false;
71+
}
72+
73+
return false;
74+
}
75+
76+
int main()
77+
{
78+
int maze[N][N] = {{1, 0, 0, 0},
79+
{1, 1, 0, 1},
80+
{0, 1, 0, 0},
81+
{1, 1, 1, 1}};
82+
83+
solveMaze(maze);
84+
return 0;
85+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
vector<int> cum;
3+
int csum;
4+
public:
5+
Solution(vector<int>& w) {
6+
csum=0;
7+
for(int x : w){
8+
csum += x;
9+
cum.push_back(csum);
10+
}
11+
12+
}
13+
14+
int pickIndex() {
15+
int idx = rand() % csum;
16+
int res = upper_bound(cum.begin(),cum.end(),idx) - cum.begin();
17+
return res;
18+
// return binarySearch(idx+1);
19+
}
20+
};

0 commit comments

Comments
 (0)