Skip to content

Commit c697b74

Browse files
committed
🚀 24-Jun-2020
1 parent 0d955d7 commit c697b74

File tree

5 files changed

+266
-0
lines changed

5 files changed

+266
-0
lines changed

algorithms/sorting/merge_sort.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void merge(int a[], int l, int mid, int r){
5+
int i, j, k;
6+
int n1=mid-l+1; // left array a[l....mid]
7+
int n2=r-(mid+1)+1; // right array a[mid+1.....r]
8+
int L[n1], R[n2]; // temp arrays
9+
10+
for(int i=0;i<n1;i++)
11+
L[i]=a[l+i];
12+
for(int j=0;j<n2;j++)
13+
R[j]=a[mid+1+j];
14+
15+
i=0, j=0, k=l;
16+
while(i<n1 && j<n2){
17+
if(L[i]<=R[j]){
18+
a[k]=L[i];
19+
i++;
20+
} else {
21+
a[k]=R[j];
22+
j++;
23+
}
24+
k++;
25+
}
26+
// adding remaining elements
27+
while(i<n1){
28+
a[k]=L[i];
29+
i++;
30+
k++;
31+
}
32+
while(j<n2){
33+
a[k]=R[j];
34+
j++;
35+
k++;
36+
}
37+
}
38+
39+
void mergeSort(int a[], int l, int r){
40+
if(l<r){
41+
int mid=l+(r-l)/2;
42+
mergeSort(a, l, mid);
43+
mergeSort(a, mid+1, r);
44+
merge(a, l, mid, r);
45+
}
46+
}
47+
48+
int main(){
49+
int n;
50+
cin>>n;
51+
int a[n];
52+
for(int i=0;i<n;i++) cin>>a[i];
53+
mergeSort(a, 0, n-1);
54+
for(int i=0;i<n;i++) cout<<a[i]<<" ";
55+
return 0;
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.
2+
3+
4+
5+
6+
7+
Example 1:
8+
9+
10+
11+
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
12+
Output: true
13+
Example 2:
14+
15+
16+
17+
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
18+
Output: false
19+
20+
21+
Constraints:
22+
23+
2 <= coordinates.length <= 1000
24+
coordinates[i].length == 2
25+
-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
26+
coordinates contains no duplicate point.
27+
28+
29+
30+
31+
32+
class Solution {
33+
public:
34+
bool checkStraightLine(vector<vector<int>>& coords) {
35+
int n=coords.size();
36+
if(n<=2) return true;
37+
int x1=coords[0][0];
38+
int y1=coords[0][1];
39+
int x2=coords[1][0];
40+
int y2=coords[1][1];
41+
double dy=y2-y1;
42+
double dx=x2-x1;
43+
for(int i=0;i<n-1;i++){
44+
x1=coords[i][0];
45+
y1=coords[i][1];
46+
x2=coords[i+1][0];
47+
y2=coords[i+1][1];
48+
int tdy=y2-y1;
49+
int tdx=x2-x1;
50+
if(dy*tdx!=dx*tdy) return false;
51+
}
52+
return true;
53+
}
54+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Given a complete binary tree, count the number of nodes.
2+
3+
Note:
4+
5+
Definition of a complete binary tree from Wikipedia:
6+
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
7+
8+
Example:
9+
10+
Input:
11+
1
12+
/ \
13+
2 3
14+
/ \ /
15+
4 5 6
16+
17+
Output: 6
18+
19+
20+
21+
22+
23+
24+
25+
26+
/**
27+
* Definition for a binary tree node.
28+
* struct TreeNode {
29+
* int val;
30+
* TreeNode *left;
31+
* TreeNode *right;
32+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
33+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
34+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
35+
* };
36+
*/
37+
class Solution {
38+
public:
39+
40+
void countNodesUtil(TreeNode* root, int &cnt){
41+
if(!root) return;
42+
countNodesUtil(root->left, cnt);
43+
cnt++;
44+
countNodesUtil(root->right, cnt);
45+
}
46+
47+
int countNodes(TreeNode* root) {
48+
int cnt=0;
49+
countNodesUtil(root, cnt);
50+
return cnt;
51+
}
52+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Given a complete binary tree, count the number of nodes.
2+
3+
Note:
4+
5+
Definition of a complete binary tree from Wikipedia:
6+
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
7+
8+
Example:
9+
10+
Input:
11+
1
12+
/ \
13+
2 3
14+
/ \ /
15+
4 5 6
16+
17+
Output: 6
18+
19+
20+
21+
22+
23+
24+
25+
26+
/**
27+
* Definition for a binary tree node.
28+
* struct TreeNode {
29+
* int val;
30+
* TreeNode *left;
31+
* TreeNode *right;
32+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
33+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
34+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
35+
* };
36+
*/
37+
class Solution {
38+
public:
39+
40+
void countNodesUtil(TreeNode* root, int &cnt){
41+
if(!root) return;
42+
countNodesUtil(root->left, cnt);
43+
cnt++;
44+
countNodesUtil(root->right, cnt);
45+
}
46+
47+
int countNodes(TreeNode* root) {
48+
int cnt=0;
49+
countNodesUtil(root, cnt);
50+
return cnt;
51+
}
52+
};

mathematical/check_if_prime.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
For a given number N check if it is prime or not. A prime number is a number which is only divisible by 1 and itself.
3+
4+
Input:
5+
First line contains an integer, the number of test cases 'T'. T testcases follow. Each test case should contain a positive integer N.
6+
7+
Output:
8+
For each testcase, in a new line, print "Yes" if it is a prime number else print "No".
9+
10+
Constraints:
11+
1 <= T <= 100
12+
1 <= N <= 109
13+
14+
Example:
15+
Input:
16+
1
17+
5
18+
Output:
19+
Yes
20+
21+
Explanation:
22+
Testcase 1: 5 is the prime number as it is divisible only by 1 and 5.
23+
*/
24+
25+
26+
27+
28+
#include<bits/stdc++.h>
29+
using namespace std;
30+
31+
bool prime(long int n){
32+
if(n<=1) return false;
33+
if(n<=3) return true;
34+
if(n%2==0 || n%3==0) return false;
35+
for(long int i=5;i<=sqrt(n);i+=6){
36+
if(n%i==0 || n%(i+2)==0) return false;
37+
}
38+
return true;
39+
}
40+
int main(){
41+
ios_base::sync_with_stdio(false);
42+
cin.tie(NULL);
43+
int t;
44+
cin>>t;
45+
while(t--){
46+
long int n;
47+
cin>>n;
48+
prime(n)? cout<<"Yes\n" : cout<<"No\n";
49+
}
50+
51+
return 0;
52+
}

0 commit comments

Comments
 (0)