Skip to content

Commit 8d12299

Browse files
Merge branch 'codemistic:main' into rotate-a-linked-list
2 parents 35c2092 + 81fc0b1 commit 8d12299

File tree

61 files changed

+2778
-0
lines changed

Some content is hidden

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

61 files changed

+2778
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
#include <climits>
4+
using namespace std;
5+
6+
void cal(vector<int> &arr,vector<int> &res,int l,int u)
7+
{
8+
int sz=(u-l+1);
9+
int min1=INT_MAX,max1=0;
10+
for (int i=l;i<=u;i++)
11+
{
12+
if (arr[i]<min1)
13+
min1=arr[i];
14+
if (arr[i]>max1)
15+
max1=arr[i];
16+
}
17+
if (sz==1)
18+
{
19+
res[0]=(res[0]<min1)?res[0]:min1;
20+
res[1]=(res[1]>max1)?res[1]:max1;
21+
}
22+
else
23+
{
24+
for (int i=l;i<=u;i++)
25+
{
26+
if (arr[i]<res[0] && arr[i]!=min1)
27+
res[0]=(res[0]<arr[i])?res[0]:arr[i];
28+
if (arr[i]>res[1] && arr[i]!=max1)
29+
res[1]=(res[1]>arr[i])?res[1]:arr[i];
30+
}
31+
}
32+
}
33+
34+
void show(vector<int> &arr,vector<int> &res,int l,int u)
35+
{
36+
int size1=(u-l+1);
37+
if (size1>4)
38+
{
39+
int mid=l+(u-l)/2;
40+
show(arr,res,l,mid);
41+
show(arr,res,mid+1,u);
42+
}
43+
cal(arr,res,l,u);
44+
}
45+
46+
int main()
47+
{
48+
int n=0;
49+
cout<<"PLEASE ENTER THE NUMBER OF ARRAY ELEMENTS"<<endl;
50+
cin>>n;
51+
vector<int> arr(n,0),res(2,0);
52+
res[0]=INT_MAX;
53+
res[1]=0;
54+
cout<<"PLEASE ENTER THE ARRAY ELEMENTS"<<endl;
55+
for (int i=0;i<n;i++)
56+
cin>>arr[i];
57+
show(arr,res,0,n-1);
58+
cout<<"THE SECOND SMALLEST NUMBER IS- "<<res[0]<<endl
59+
cout<<"THE SECOND LARGEST NUMBER IS- "<<res[1]<<endl;
60+
return 0;
61+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int numDecodings(string s) {
4+
if(s[0] == '0')return 0;
5+
int n = s.length();
6+
vector<int> dp(n+2,0);
7+
dp[n+1] = 1;
8+
dp[n] = 1;
9+
for(int i = n-1; i >= 0; i--){
10+
int ans = 0;
11+
int num = s[i] - '0';
12+
if(num > 0)ans += dp[i+1];
13+
if(i+1 < s.length() && num > 0){
14+
num = num*10+(s[i+1] - '0');
15+
if(num > 0 && num <= 26)ans += dp[i+2];
16+
}
17+
dp[i] = ans;
18+
}
19+
return dp[0];
20+
}
21+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<int> shuffle(vector<int>& nums, int n) {
4+
5+
vector<int> ans;
6+
7+
int i=0;
8+
int slice=nums.size()/2-1;
9+
int j=slice+1;
10+
while(i<slice || j<nums.size()){
11+
ans.push_back(nums[i]);
12+
ans.push_back(nums[j]);
13+
i++;
14+
j++;
15+
}
16+
return ans;
17+
}
18+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://leetcode.com/problems/wiggle-subsequence/
2+
3+
class Solution {
4+
public:
5+
int wiggleMaxLength(vector<int>& nums) {
6+
if(nums.size()< 2)
7+
return nums.size();
8+
int ans = 1;
9+
int f = -1;
10+
for(int i=1; i < nums.size(); i++){
11+
if(nums[i] > nums[i-1]){
12+
if(f==-1 || f==0) ans++;
13+
f = 1;
14+
}else if(nums[i] < nums[i-1]){
15+
if(f==1 || f==-1)ans++;
16+
f=0;
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int findLengthOfLCIS(vector<int>& nums) {
4+
int count=1;
5+
int count1=1;
6+
for(int i=0;i<nums.size()-1;i++)
7+
{
8+
if(nums[i+1]>nums[i])
9+
{count++;
10+
count1=max(count,count1);}
11+
else
12+
count=1;
13+
14+
}
15+
return count1;
16+
17+
}
18+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!-- creating node type own data structure -->
2+
struct Node {
3+
int data;
4+
struct Node *next;
5+
6+
Node(int x) {
7+
data = x;
8+
next = NULL;
9+
}
10+
};
11+
*/
12+
//Function to merge two sorted linked list.
13+
Node* sortedMerge(Node* head1, Node* head2)
14+
{
15+
// code here
16+
if(!head1) return head2;
17+
if(!head2) return head1;
18+
Node* dummy= new Node(-1);
19+
Node* ptr1= head1,*ptr2=head2;
20+
Node* tail=dummy;
21+
while(ptr1&&ptr2){
22+
if(ptr1->data<=ptr2->data){
23+
tail->next= ptr1;
24+
tail=ptr1;
25+
ptr1=ptr1->next;
26+
}
27+
else{
28+
tail->next=ptr2;
29+
tail=ptr2;
30+
ptr2=ptr2->next;
31+
}
32+
}
33+
if(ptr1) tail->next=ptr1;
34+
if(ptr2) tail->next=ptr2;
35+
return dummy->next;
36+
}

CPP/Problems/pow(x,n).cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
//binary exponentiation
4+
double power(double x, int n){
5+
if(n==0)return 1;
6+
7+
double d=power(x,n/2);
8+
if(n&1)return x*d*d;
9+
return d*d;
10+
}
11+
double myPow(double x, int n) {
12+
13+
if(n>0){return power(x,n);}
14+
return 1/power(x,n);//a^-1=1/a
15+
}
16+
};

CPP/Problems/remove-element.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} val
4+
* @return {number}
5+
*/
6+
var removeElement = function (nums, val) {
7+
8+
let count = 0;
9+
10+
for (let i = 0; i < nums.length; i++) {
11+
12+
if (nums[i] !== val) {
13+
nums[count++] = nums[i];
14+
}
15+
}
16+
return count;
17+
};
18+
//Leetcode

CPP/graph_tree/LCA_Binary-tree.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// C++ Program for Lowest Common Ancestor in a Binary Tree
2+
// A O(n) solution to find LCA of two given values n1 and n2
3+
#include <iostream>
4+
#include <vector>
5+
6+
using namespace std;
7+
8+
// A Binary Tree node
9+
struct Node
10+
{
11+
int key;
12+
struct Node *left, *right;
13+
};
14+
15+
// Utility function creates a new binary tree node with given key
16+
Node * newNode(int k)
17+
{
18+
Node *temp = new Node;
19+
temp->key = k;
20+
temp->left = temp->right = NULL;
21+
return temp;
22+
}
23+
24+
// Finds the path from root node to given root of the tree, Stores the
25+
// path in a vector path[], returns true if path exists otherwise false
26+
bool findPath(Node *root, vector<int> &path, int k)
27+
{
28+
// base case
29+
if (root == NULL) return false;
30+
31+
// Store this node in path vector. The node will be removed if
32+
// not in path from root to k
33+
path.push_back(root->key);
34+
35+
// See if the k is same as root's key
36+
if (root->key == k)
37+
return true;
38+
39+
// Check if k is found in left or right sub-tree
40+
if ( (root->left && findPath(root->left, path, k)) ||
41+
(root->right && findPath(root->right, path, k)) )
42+
return true;
43+
44+
// If not present in subtree rooted with root, remove root from
45+
// path[] and return false
46+
path.pop_back();
47+
return false;
48+
}
49+
50+
// Returns LCA if node n1, n2 are present in the given binary tree,
51+
// otherwise return -1
52+
int findLCA(Node *root, int n1, int n2)
53+
{
54+
// to store paths to n1 and n2 from the root
55+
vector<int> path1, path2;
56+
57+
// Find paths from root to n1 and root to n2. If either n1 or n2
58+
// is not present, return -1
59+
if ( !findPath(root, path1, n1) || !findPath(root, path2, n2))
60+
return -1;
61+
62+
/* Compare the paths to get the first different value */
63+
int i;
64+
for (i = 0; i < path1.size() && i < path2.size() ; i++)
65+
if (path1[i] != path2[i])
66+
break;
67+
return path1[i-1];
68+
}
69+
70+
// Driver program to test above functions
71+
int main()
72+
{
73+
// Let us create the Binary Tree shown in above diagram.
74+
Node * root = newNode(1);
75+
root->left = newNode(2);
76+
root->right = newNode(3);
77+
root->left->left = newNode(4);
78+
root->left->right = newNode(5);
79+
root->right->left = newNode(6);
80+
root->right->right = newNode(7);
81+
cout << "LCA(4, 5) = " << findLCA(root, 4, 5);
82+
cout << "\nLCA(4, 6) = " << findLCA(root, 4, 6);
83+
cout << "\nLCA(3, 4) = " << findLCA(root, 3, 4);
84+
cout << "\nLCA(2, 4) = " << findLCA(root, 2, 4);
85+
return 0;
86+
}
87+
88+
89+
/*
90+
Output
91+
LCA(4, 5) = 2
92+
LCA(4, 6) = 1
93+
LCA(3, 4) = 1
94+
LCA(2, 4) = 2
95+
Time Complexity: O(n). The tree is traversed twice, and then path arrays are compared.
96+
Auxiliary Space: O(n). Extra Space for path1 and path2.
97+
98+
*/
99+
100+
//contributed by Sourav Naskar

CPP/hashing/quadratic_probing.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
using namespace std;
3+
int hashfunction(int key)
4+
{
5+
return key % 13;
6+
}
7+
int main()
8+
{
9+
int a[] = {26, 28, 31, 34, 35, 25, 31, 31, 31, 25};
10+
int h[13];
11+
int n = 13;
12+
for (int i = 0; i < 13; i++)
13+
h[i] = -999;
14+
for (int i = 0; i < 10; i++)
15+
{
16+
if (h[hashfunction(a[i])] != -999)
17+
{
18+
int j = 1;
19+
int pcount = 1;
20+
int k;
21+
k = hashfunction(a[i]);
22+
while (pcount < n / 2 && h[(k + j) % 13] != -999)
23+
{
24+
k = (k + j) % 13;
25+
cout << a[i] << "->" << k << " ";
26+
j = j + 2;
27+
pcount++;
28+
}
29+
cout << endl;
30+
if (h[(k + j) % 13] == -999)
31+
h[(k + j) % 13] = a[i];
32+
}
33+
else
34+
h[hashfunction(a[i])] = a[i];
35+
}
36+
for (int i = 0; i < 13; i++)
37+
cout << h[i] << " ";
38+
}

CPP/map/mapConcepts.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
```
2+
#include<bits/stdc++.h>>
3+
using namespace std;
4+
int main() {
5+
6+
//Map: Commom use- counts frequency of various objects
7+
//Maps are special arrays in which the indices(keys) of elements can be negative or very big or even strings!
8+
9+
// map<key_datatype, value_datatype> m;
10+
11+
map<string, int>m; //defines a map in which the keys of elements are strings.
12+
m["hello"]=50;
13+
m["world"]=12;
14+
cout<<m["hello"]<<" "<<m["world"]; //50 12
15+
16+
//Maps are very similar to sets, in sets the values are unique and sorted. In maps, the keys are unique and sorted(lexicographically).
17+
return 0;
18+
}
19+
```

0 commit comments

Comments
 (0)