Skip to content

Commit bd38a2c

Browse files
committed
🚀 04-Jul-2020
1 parent da4d70c commit bd38a2c

4 files changed

+293
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Given a sorted array arr[] of size N without duplicates, and given a value x. Find the floor of x in given array. Floor of x is defined as the largest element K in arr[] such that K is smaller than or equal to x.
3+
4+
Input:
5+
First line of input contains number of testcases T. For each testcase, first line of input contains number of elements in the array and element whose floor is to be searched. Last line of input contains array elements.
6+
7+
Output:
8+
Output the index of floor of x if exists, else print -1.
9+
10+
Constraints:
11+
1 ≤ T ≤ 100
12+
1 ≤ N ≤ 107
13+
1 ≤ arr[i] ≤ 1018
14+
0 ≤ X ≤ arr[n-1]
15+
16+
Example:
17+
Input:
18+
3
19+
7 0
20+
1 2 8 10 11 12 19
21+
7 5
22+
1 2 8 10 11 12 19
23+
7 10
24+
1 2 8 10 11 12 19
25+
26+
Output:
27+
-1
28+
1
29+
3
30+
31+
Explanation:
32+
Testcase 1: No element less than 0 is found. So output is "-1".
33+
Testcase 2: Number less than 5 is 2, whose index is 1(0-based indexing).
34+
*/
35+
36+
37+
38+
39+
40+
41+
42+
43+
#include<bits/stdc++.h>
44+
using namespace std;
45+
46+
int getFloor(long long a[], int n, int x){
47+
int l=0, h=n-1, mid, res=-1;
48+
while(l<=h){
49+
mid=l+(h-l)/2;
50+
if(a[mid]==x){
51+
return mid;
52+
h=mid-1;
53+
}
54+
else if(a[mid]<x){
55+
res=mid; // possible result
56+
l=mid+1;
57+
} else h=mid-1;
58+
}
59+
return res;
60+
}
61+
62+
int main(){
63+
ios_base::sync_with_stdio(false);
64+
cin.tie(NULL);
65+
cout.tie(NULL);
66+
int t;
67+
cin>>t;
68+
while(t--){
69+
int n, x;
70+
cin>>n>>x;
71+
long long a[n];
72+
for(int i=0;i<n;i++) cin>>a[i];
73+
cout<<getFloor(a, n, x)<<endl;
74+
}
75+
76+
return 0;
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
There are 8 prison cells in a row, and each cell is either occupied or vacant.
2+
3+
Each day, whether the cell is occupied or vacant changes according to the following rules:
4+
5+
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
6+
Otherwise, it becomes vacant.
7+
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
8+
9+
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
10+
11+
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
12+
13+
14+
15+
Example 1:
16+
17+
Input: cells = [0,1,0,1,1,0,0,1], N = 7
18+
Output: [0,0,1,1,0,0,0,0]
19+
Explanation:
20+
The following table summarizes the state of the prison on each day:
21+
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
22+
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
23+
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
24+
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
25+
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
26+
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
27+
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
28+
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
29+
30+
Example 2:
31+
32+
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
33+
Output: [0,0,1,1,1,1,1,0]
34+
35+
36+
Note:
37+
38+
cells.length == 8
39+
cells[i] is in {0, 1}
40+
1 <= N <= 10^9
41+
42+
43+
44+
45+
46+
47+
48+
49+
class Solution {
50+
public:
51+
vector<int> prisonAfterNDays(vector<int>& cells, int N) {
52+
int n=cells.size();
53+
vector<int> res=cells;
54+
55+
bool first=true;
56+
if(N>14) N=(N%14)+14;
57+
while(N--){
58+
for(int i=1;i<n-1;i++){
59+
if(cells[i-1]==1 && cells[i+1]==1) res[i]=1;
60+
else if(cells[i-1]==0 && cells[i+1]==0) res[i]=1;
61+
else res[i]=0;
62+
}
63+
if(first){
64+
res[0]=0;
65+
res[n-1]=0;
66+
first=false;
67+
}
68+
cells=res;
69+
}
70+
71+
return res;
72+
}
73+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
2+
3+
Your algorithm's runtime complexity must be in the order of O(log n).
4+
5+
If the target is not found in the array, return [-1, -1].
6+
7+
Example 1:
8+
9+
Input: nums = [5,7,7,8,8,10], target = 8
10+
Output: [3,4]
11+
Example 2:
12+
13+
Input: nums = [5,7,7,8,8,10], target = 6
14+
Output: [-1,-1]
15+
16+
17+
Constraints:
18+
19+
0 <= nums.length <= 10^5
20+
-10^9 <= nums[i] <= 10^9
21+
nums is a non decreasing array.
22+
-10^9 <= target <= 10^9
23+
24+
25+
26+
27+
28+
29+
30+
31+
class Solution {
32+
public:
33+
int first(vector<int> &a, int n, int item){
34+
int l=0, r=n-1, mid, res=-1;
35+
while(l<=r){
36+
mid=l+(r-l)/2;
37+
if(a[mid]==item){
38+
res=mid; // can be a possible result
39+
r=mid-1;
40+
}
41+
else if(a[mid]<item) l=mid+1;
42+
else r=mid-1;
43+
}
44+
return res;
45+
}
46+
47+
int last(vector<int> &a, int n, int item){
48+
int l=0, r=n-1, mid, res=-1;
49+
while(l<=r){
50+
mid=l+(r-l)/2;
51+
if(a[mid]==item){
52+
res=mid; // can be a possible result
53+
l=mid+1;
54+
}
55+
else if(a[mid]<item) l=mid+1;
56+
else r=mid-1;
57+
}
58+
return res;
59+
}
60+
61+
vector<int> searchRange(vector<int>& a, int target) {
62+
vector<int>res;
63+
int n=a.size();
64+
int f=first(a, n, target);
65+
int l=last(a, n, target);
66+
res.push_back(f);
67+
res.push_back(l);
68+
return res;
69+
}
70+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
There are 8 prison cells in a row, and each cell is either occupied or vacant.
2+
3+
Each day, whether the cell is occupied or vacant changes according to the following rules:
4+
5+
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
6+
Otherwise, it becomes vacant.
7+
(Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.)
8+
9+
We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0.
10+
11+
Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.)
12+
13+
14+
15+
Example 1:
16+
17+
Input: cells = [0,1,0,1,1,0,0,1], N = 7
18+
Output: [0,0,1,1,0,0,0,0]
19+
Explanation:
20+
The following table summarizes the state of the prison on each day:
21+
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
22+
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
23+
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
24+
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
25+
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
26+
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
27+
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
28+
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
29+
30+
Example 2:
31+
32+
Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000
33+
Output: [0,0,1,1,1,1,1,0]
34+
35+
36+
Note:
37+
38+
cells.length == 8
39+
cells[i] is in {0, 1}
40+
1 <= N <= 10^9
41+
42+
43+
44+
45+
46+
47+
48+
49+
class Solution {
50+
public:
51+
vector<int> prisonAfterNDays(vector<int>& cells, int N) {
52+
int n=cells.size();
53+
vector<int> res=cells;
54+
55+
bool first=true;
56+
if(N>14) N=(N%14)+14;
57+
while(N--){
58+
for(int i=1;i<n-1;i++){
59+
if(cells[i-1]==1 && cells[i+1]==1) res[i]=1;
60+
else if(cells[i-1]==0 && cells[i+1]==0) res[i]=1;
61+
else res[i]=0;
62+
}
63+
if(first){
64+
res[0]=0;
65+
res[n-1]=0;
66+
first=false;
67+
}
68+
cells=res;
69+
}
70+
71+
return res;
72+
}
73+
};

0 commit comments

Comments
 (0)