Skip to content

Commit 9034cec

Browse files
Add files via upload
1 parent 47dfe22 commit 9034cec

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

ActivitySelection.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Given N activities with their start and finish day given in array start[ ] and end[ ]. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a given day.
3+
Note : Duration of the activity includes both starting and ending day.
4+
5+
Example 1:
6+
Input:
7+
N = 2
8+
start[] = {2, 1}
9+
end[] = {2, 2}
10+
Output:
11+
1
12+
Explanation:
13+
A person can perform only one of the
14+
given activities.
15+
16+
Example 2:
17+
Input:
18+
N = 4
19+
start[] = {1, 3, 2, 5}
20+
end[] = {2, 4, 3, 6}
21+
Output:
22+
3
23+
Explanation:
24+
A person can perform activities 1, 2
25+
and 4.
26+
27+
Your Task :
28+
You don't need to read input or print anything. Your task is to complete the function activityselection() which takes array start[ ], array end[ ] and integer N as input parameters and returns the maximum number of activities that can be done.
29+
30+
Expected Time Complexity : O(N * Log(N))
31+
Expected Auxilliary Space : O(N)
32+
33+
Constraints:
34+
1 ≤ N ≤ 2*105
35+
1 ≤ start[i] ≤ end[i] ≤ 109
36+
*/
37+
38+
class Solution
39+
{
40+
public:
41+
static bool cmp(pair<int,int> &a, pair<int,int> &b){
42+
if(a.first==b.first) return a.second<b.second;
43+
44+
return a.first<b.first;
45+
}
46+
47+
int activitySelection(vector<int> start, vector<int> end, int n)
48+
{
49+
vector<pair<int,int>> v;
50+
for(int i=0;i<n;i++){
51+
v.push_back({start[i],end[i]});
52+
}
53+
sort(v.begin(), v.end(), cmp);
54+
int result=0, endDate=-1;
55+
for(int i=0;i<n;i++){
56+
if(v[i].first>endDate){
57+
result++;
58+
endDate=v[i].second;
59+
}else{
60+
if(v[i].second<endDate) endDate=v[i].second;
61+
}
62+
}
63+
return result;
64+
}
65+
};
66+

CheckWhetherKthBitIsSetOrNot.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Given a number N and a bit number K, check if Kth index bit of N is set or not. A bit is called set if it is 1. Position of set bit '1' should be indexed starting with 0 from LSB side in binary representation of the number.
3+
Note: Index is starting from 0.
4+
Example 1:
5+
6+
Input: N = 4, K = 0
7+
Output: No
8+
Explanation: Binary representation of 4 is 100,
9+
in which 0th index bit from LSB is not set.
10+
So, return false.
11+
Example 2:
12+
13+
Input: N = 4, K = 2
14+
Output: Yes
15+
Explanation: Binary representation of 4 is 100,
16+
in which 2nd index bit from LSB is set.
17+
So, return true.
18+
Example 3:
19+
20+
Input: N = 500, K = 3
21+
Output: No
22+
Explanation: Binary representation of 500 is
23+
111110100, in which 3rd index bit from LSB is not set.
24+
So, return false.
25+
26+
Your task:
27+
You don't have to read input or print anything. Your task is to complete the function checkKthbit that takes n and k as parameters and returns either true (if kth bit is set) or false(if kth bit is not set).
28+
29+
Expected Time Complexity: O(1).
30+
Expected Auxiliary Space: O(1).
31+
*/
32+
33+
34+
class Solution
35+
{
36+
public:
37+
bool checkKthBit(int n, int k)
38+
{
39+
if(n&(1<<k)){
40+
return true;
41+
}
42+
return false;
43+
}
44+
};
45+

LongestConsecutive1's.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Given a number N. Find the length of the longest consecutive 1s in its binary representation.
3+
4+
Example 1:
5+
Input: N = 14
6+
Output: 3
7+
Explanation:
8+
Binary representation of 14 is
9+
1110, in which 111 is the longest
10+
consecutive set bits of length is 3.
11+
12+
Example 2:
13+
Input: N = 222
14+
Output: 4
15+
Explanation:
16+
Binary representation of 222 is
17+
11011110, in which 1111 is the
18+
longest consecutive set bits of length 4.
19+
20+
Your Task:
21+
You don't need to read input or print anything. Your task is to complete the function maxConsecutiveOnes() which returns the length of the longest consecutive 1s in the binary representation of given N.
22+
23+
Expected Time Complexity: O(log N).
24+
Expected Auxiliary Space: O(1).
25+
*/
26+
27+
//User function Template for C++
28+
29+
/* Function to calculate the longest consecutive ones
30+
* N: given input to calculate the longest consecutive ones
31+
*/
32+
class Solution
33+
{
34+
public:
35+
int maxConsecutiveOnes(int N)
36+
{
37+
int cnt=0, mx=-1;
38+
while(N){
39+
if(N&1) cnt++;
40+
else{
41+
mx=max(mx,cnt);
42+
cnt=0;
43+
}
44+
N>>=1;
45+
}
46+
mx=max(mx,cnt);
47+
return mx;
48+
}
49+
};

0 commit comments

Comments
 (0)