Skip to content

Commit d17d0f7

Browse files
committed
🚀 02-Jul-2020
1 parent 545a85c commit d17d0f7

File tree

4 files changed

+248
-0
lines changed

4 files changed

+248
-0
lines changed

array/binary_search.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
/*
3+
Given a sorted array arr[] of N integers and a number K is given. The task is to check if the element K is present in the array or not.
4+
5+
Input:
6+
First line of input contains number of testcases T. For each testcase, first line of input contains number of elements in the array and the number K seperated by space. Next line contains N elements.
7+
8+
Output:
9+
For each testcase, if the element is present in the array print "1" (without quotes), else print "-1" (without quotes).
10+
11+
Constraints:
12+
1 <= T <= 100
13+
1 <= N <= 106
14+
1 <= K <= 106
15+
1 <= arr[i] <= 106
16+
17+
Example:
18+
Input:
19+
2
20+
5 6
21+
1 2 3 4 6
22+
5 2
23+
1 3 4 5 6
24+
25+
Output:
26+
1
27+
-1
28+
29+
Explanation:
30+
Testcase 1: Since, 6 is present in the array at index 4 (0-based indexing), so output is 1.
31+
Testcase 2: Since, 2 is not present in the array, so output is -1.
32+
*/
33+
34+
35+
36+
37+
38+
39+
40+
#include<bits/stdc++.h>
41+
using namespace std;
42+
43+
int bSearch(int a[], int n, int item){
44+
int l=0, r=n-1, mid;
45+
while(l<=r){
46+
mid=l+(r-l)/2;
47+
if(a[mid]==item) return 1; // found
48+
else if(a[mid]>item) r=mid-1;
49+
else l=mid+1;
50+
}
51+
return -1;
52+
}
53+
54+
int main(){
55+
ios_base::sync_with_stdio(false);
56+
cin.tie(NULL);
57+
cout.tie(NULL);
58+
int t;
59+
cin>>t;
60+
while(t--){
61+
int n, item;
62+
cin>>n>>item;
63+
int a[n];
64+
for(int i=0;i<n;i++) cin>>a[i];
65+
cout<<bSearch(a, n, item)<<endl;
66+
}
67+
68+
return 0;
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Given a sorted array with possibly duplicate elements, the task is to find indexes of first and last occurrences of an element x in the given array.
3+
4+
Note: If the number x is not found in the array just print '-1'.
5+
6+
Input:
7+
The first line consists of an integer T i.e number of test cases. The first line of each test case contains two integers n and x. The second line contains n spaced integers.
8+
9+
Output:
10+
Print index of the first and last occurrences of the number x with a space in between.
11+
12+
Constraints:
13+
1<=T<=100
14+
1<=n,a[i]<=1000
15+
16+
Example:
17+
Input:
18+
2
19+
9 5
20+
1 3 5 5 5 5 67 123 125
21+
9 7
22+
1 3 5 5 5 5 7 123 125
23+
24+
Output:
25+
2 5
26+
6 6
27+
*/
28+
29+
30+
31+
32+
33+
34+
35+
#include<bits/stdc++.h>
36+
using namespace std;
37+
38+
int first(int a[], int n, int item){
39+
int l=0, r=n-1, mid, res=-1;
40+
while(l<=r){
41+
mid=l+(r-l)/2;
42+
if(a[mid]==item){
43+
res=mid; // can be a possible result
44+
r=mid-1;
45+
}
46+
else if(a[mid]<item) l=mid+1;
47+
else r=mid-1;
48+
}
49+
return res;
50+
}
51+
52+
int last(int a[], int n, int item){
53+
int l=0, r=n-1, mid, res=-1;
54+
while(l<=r){
55+
mid=l+(r-l)/2;
56+
if(a[mid]==item){
57+
res=mid; // can be a possible result
58+
l=mid+1;
59+
}
60+
else if(a[mid]<item) l=mid+1;
61+
else r=mid-1;
62+
}
63+
return res;
64+
}
65+
66+
int main(){
67+
ios_base::sync_with_stdio(false);
68+
cin.tie(NULL);
69+
cout.tie(NULL);
70+
int t;
71+
cin>>t;
72+
while(t--){
73+
int n, item;
74+
cin>>n>>item;
75+
int a[n];
76+
for(int i=0;i<n;i++) cin>>a[i];
77+
int f=first(a, n, item);
78+
int l=last(a, n, item);
79+
if(f>=0)
80+
cout<<f<<" "<<l<<endl;
81+
else cout<<"-1"<<endl;
82+
}
83+
84+
return 0;
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
2+
3+
Given n, find the total number of full staircase rows that can be formed.
4+
5+
n is a non-negative integer and fits within the range of a 32-bit signed integer.
6+
7+
Example 1:
8+
9+
n = 5
10+
11+
The coins can form the following rows:
12+
¤
13+
¤ ¤
14+
¤ ¤
15+
16+
Because the 3rd row is incomplete, we return 2.
17+
Example 2:
18+
19+
n = 8
20+
21+
The coins can form the following rows:
22+
¤
23+
¤ ¤
24+
¤ ¤ ¤
25+
¤ ¤
26+
27+
Because the 4th row is incomplete, we return 3.
28+
29+
30+
31+
32+
33+
34+
35+
36+
class Solution {
37+
public:
38+
int arrangeCoins(int n) {
39+
int full=0, i=1;
40+
while(n>=0){
41+
n-=i;
42+
if(n>=0) full++;
43+
i++;
44+
}
45+
return full;
46+
}
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
2+
3+
Given n, find the total number of full staircase rows that can be formed.
4+
5+
n is a non-negative integer and fits within the range of a 32-bit signed integer.
6+
7+
Example 1:
8+
9+
n = 5
10+
11+
The coins can form the following rows:
12+
¤
13+
¤ ¤
14+
¤ ¤
15+
16+
Because the 3rd row is incomplete, we return 2.
17+
Example 2:
18+
19+
n = 8
20+
21+
The coins can form the following rows:
22+
¤
23+
¤ ¤
24+
¤ ¤ ¤
25+
¤ ¤
26+
27+
Because the 4th row is incomplete, we return 3.
28+
29+
30+
31+
32+
33+
34+
35+
36+
class Solution {
37+
public:
38+
int arrangeCoins(int n) {
39+
int full=0, i=1;
40+
while(n>=0){
41+
n-=i;
42+
if(n>=0) full++;
43+
i++;
44+
}
45+
return full;
46+
}
47+
};

0 commit comments

Comments
 (0)