Skip to content

Commit da4d70c

Browse files
committed
🚀 03-Jul-2020
1 parent d17d0f7 commit da4d70c

5 files changed

+303
-0
lines changed
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Given a sorted array A of size N and a number X, you need to find the number of occurrences of X in A.
3+
4+
Input:
5+
The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each testcase contains two lines of input: The first line contains N and X(element whose occurrence needs to be counted). The second line contains the elements of the array separated by spaces.
6+
7+
Output:
8+
For each testcase, print the count of the occurrences of X in the array, if count is zero then print -1.
9+
10+
Constraints:
11+
1 ≤ T ≤ 100
12+
1 ≤ N ≤ 105
13+
1 ≤ A[i] ≤ 103
14+
1 <= X <= 103
15+
16+
Example:
17+
Input:
18+
2
19+
7 2
20+
1 1 2 2 2 2 3
21+
7 4
22+
1 1 2 2 2 2 3
23+
Output:
24+
4
25+
-1
26+
27+
Explanation:
28+
Testcase 1: 2 occurs 4 times in 1 1 2 2 2 2 3
29+
Testcase 2: 4 is not present in 1 1 2 2 2 2 3
30+
*/
31+
32+
33+
34+
35+
36+
37+
38+
#include<bits/stdc++.h>
39+
using namespace std;
40+
41+
int first(int a[], int n, int x){
42+
int l=0, r=n-1, mid, res=-1;
43+
while(l<=r){
44+
mid=l+(r-l)/2;
45+
if(a[mid]==x){
46+
res=mid;
47+
r=mid-1;
48+
} else if(a[mid]<x) l=mid+1;
49+
else r=mid-1;
50+
}
51+
return res;
52+
}
53+
54+
int last(int a[], int n, int x){
55+
int l=0, r=n-1, mid, res=-1;
56+
while(l<=r){
57+
mid=l+(r-l)/2;
58+
if(a[mid]==x){
59+
res=mid;
60+
l=mid+1;
61+
} else if(a[mid]<x) l=mid+1;
62+
else r=mid-1;
63+
}
64+
return res;
65+
}
66+
67+
int main(){
68+
ios_base::sync_with_stdio(false);
69+
cin.tie(NULL);
70+
cout.tie(NULL);
71+
int t;
72+
cin>>t;
73+
while(t--){
74+
int n, x;
75+
cin>>n>>x;
76+
int a[n];
77+
for(int i=0;i<n;i++) cin>>a[i];
78+
int f=first(a, n, x);
79+
int l=last(a, n, x);
80+
if(f>=0)
81+
cout<<l-f+1<<endl;
82+
else cout<<"-1"<<endl;
83+
}
84+
85+
return 0;
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Given a sorted array A of size N. The array is rotated 'K' times. Find the value of 'K'.
3+
4+
Input:
5+
The first line contains an integer T, depicting total number of test cases. T testcases follow. Each testcase contains two lines of input. The first line contains an integer N depicting the size of array. The next line contains elements of the array separated by spaces.
6+
7+
Output:
8+
For each testcase, print the value of K.
9+
10+
Constraints:
11+
1 <= T <= 100
12+
1 <= N <=107
13+
0 <= Ai <= 1018
14+
15+
Example:
16+
Input
17+
2
18+
5
19+
5 1 2 3 4
20+
5
21+
1 2 3 4 5
22+
Output
23+
1
24+
0
25+
26+
Explanation:
27+
Testcase1: 5 1 2 3 4. The original sorted array is 1 2 3 4 5. We can see that the array was rotated 1 times to the right.
28+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
2+
3+
For example:
4+
Given binary tree [3,9,20,null,null,15,7],
5+
3
6+
/ \
7+
9 20
8+
/ \
9+
15 7
10+
return its bottom-up level order traversal as:
11+
[
12+
[15,7],
13+
[9,20],
14+
[3]
15+
]
16+
17+
18+
19+
20+
21+
22+
23+
24+
/**
25+
* Definition for a binary tree node.
26+
* struct TreeNode {
27+
* int val;
28+
* TreeNode *left;
29+
* TreeNode *right;
30+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
31+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
32+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
33+
* };
34+
*/
35+
class Solution {
36+
public:
37+
38+
vector<vector<int>> levelOrderBottom(TreeNode* root) {
39+
vector<vector<int> > res;
40+
if(!root) return res;
41+
queue<TreeNode*> q;
42+
int size;
43+
q.push(root);
44+
while(!q.empty()){
45+
size=q.size();
46+
vector<int> tmp;
47+
while(size--){
48+
TreeNode* curr=q.front();
49+
q.pop();
50+
if(curr->left) q.push(curr->left);
51+
if(curr->right) q.push(curr->right);
52+
tmp.push_back(curr->val);
53+
}
54+
res.push_back(tmp);
55+
}
56+
reverse(res.begin(), res.end());
57+
return res;
58+
}
59+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
2+
3+
For example:
4+
Given binary tree [3,9,20,null,null,15,7],
5+
3
6+
/ \
7+
9 20
8+
/ \
9+
15 7
10+
return its bottom-up level order traversal as:
11+
[
12+
[15,7],
13+
[9,20],
14+
[3]
15+
]
16+
17+
18+
19+
20+
21+
22+
23+
24+
/**
25+
* Definition for a binary tree node.
26+
* struct TreeNode {
27+
* int val;
28+
* TreeNode *left;
29+
* TreeNode *right;
30+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
31+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
32+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
33+
* };
34+
*/
35+
class Solution {
36+
public:
37+
38+
vector<vector<int>> levelOrderBottom(TreeNode* root) {
39+
vector<vector<int> > res;
40+
if(!root) return res;
41+
queue<TreeNode*> q;
42+
int size;
43+
q.push(root);
44+
while(!q.empty()){
45+
size=q.size();
46+
vector<int> tmp;
47+
while(size--){
48+
TreeNode* curr=q.front();
49+
q.pop();
50+
if(curr->left) q.push(curr->left);
51+
if(curr->right) q.push(curr->right);
52+
tmp.push_back(curr->val);
53+
}
54+
res.push_back(tmp);
55+
}
56+
reverse(res.begin(), res.end());
57+
return res;
58+
}
59+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
2+
3+
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
4+
5+
You are given a target value to search. If found in the array return its index, otherwise return -1.
6+
7+
You may assume no duplicate exists in the array.
8+
9+
Your algorithm's runtime complexity must be in the order of O(log n).
10+
11+
Example 1:
12+
13+
Input: nums = [4,5,6,7,0,1,2], target = 0
14+
Output: 4
15+
Example 2:
16+
17+
Input: nums = [4,5,6,7,0,1,2], target = 3
18+
Output: -1
19+
20+
21+
22+
23+
24+
25+
26+
27+
class Solution {
28+
public:
29+
30+
int pivot(vector<int>&nums){
31+
int l=0, r=nums.size()-1, mid, n=nums.size();
32+
while(l<=r){
33+
mid=l+(r-l)/2;
34+
int prev=(mid-1+n)%n;
35+
int next=(mid+1)%n;
36+
if(nums[mid]<nums[prev] && nums[mid]<nums[next])
37+
return mid;
38+
else if(nums[mid]<nums[r]) r=mid-1;
39+
else l=mid+1;
40+
}
41+
return -1;
42+
}
43+
44+
int bSearch(vector<int> &nums, int l, int r, int target){
45+
int mid;
46+
while(l<=r){
47+
mid=l+(r-l)/2;
48+
if(nums[mid]==target) return mid;
49+
else if(nums[mid]<target) l=mid+1;
50+
else r=mid-1;
51+
}
52+
return -1;
53+
}
54+
55+
56+
int search(vector<int>& nums, int target) {
57+
int n=nums.size();
58+
if(n<=0) return -1;
59+
if(n==1){
60+
if(nums[0]==target) return 0;
61+
else return -1;
62+
}
63+
64+
int piv=pivot(nums);
65+
int left=bSearch(nums,0, piv-1, target);
66+
int right=bSearch(nums,piv, n-1, target);
67+
if(left>=0) return left;
68+
else if(right>=0) return right;
69+
else return -1;
70+
}
71+
};

0 commit comments

Comments
 (0)