Skip to content

Commit 5885ab4

Browse files
committed
local
1 parent a755171 commit 5885ab4

23 files changed

+532
-356
lines changed

18-Programming-4kids/10_homework_01_answer.cpp

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int main() {
5+
int n, q, a[200];
6+
7+
cin >> n;
8+
for (int i = 0; i < n; i++)
9+
cin >> a[i];
10+
11+
cin >> q;
12+
while (q--) {
13+
int num;
14+
cin >> num;
15+
16+
int pos = -1;
17+
// search from the end
18+
for (int i = n-1; i >= 0; --i) {
19+
if (a[i] == num) {
20+
pos = i;
21+
break;
22+
}
23+
}
24+
cout << pos << "\n";
25+
}
26+
return 0;
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// By Basel Bairkdar https://www.facebook.com/baselbairkdar
2+
3+
#include<iostream>
4+
using namespace std;
5+
6+
int main() {
7+
// As values are 0-500, we can make array of 501 mark the last value in it
8+
// Then we answer the queries directly
9+
10+
const int N = 500 + 1;
11+
int n, q, x, ans[N];
12+
13+
for (int i = 0; i < N; i++)
14+
ans[i] = -1; /** at first the answer of any number is -1 **/
15+
16+
cin >> n;
17+
for (int i = 0; i < n; i++) {
18+
cin >> x;
19+
ans[x] = i;/** update the answer for x to be i **/
20+
}
21+
int num;
22+
cin >> q;
23+
while (q--) {
24+
cin >> num;
25+
cout << ans[num] << endl;
26+
}
27+
return 0;
28+
}
Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
// By Basel Bairkdar https://www.facebook.com/baselbairkdar
22

3-
#include<bits/stdc++.h>
4-
3+
#include<iostream>
54
using namespace std;
65

7-
const int N=205;
86

9-
int n,a[N];
7+
int main() {
8+
const int N = 200;
9+
int n, a[N];
10+
11+
cin >> n;
12+
for (int i = 0; i < n; i++) {
13+
cin >> a[i];
14+
}
1015

11-
int main()
12-
{
13-
cin >> n;
14-
for (int i=0;i<n;i++)
15-
{
16-
cin >> a[i];
17-
}
18-
bool increasing=1; /** we will assume that the array is increasing at first **/
19-
for (int i=1;i<n;i++)
20-
{
21-
if (a[i]<a[i-1])
22-
{
23-
increasing=0;
24-
}
25-
}
26-
if (increasing)
27-
{
28-
cout << "YES\n";
29-
}
30-
else
31-
{
32-
cout << "NO\n";
33-
}
34-
return 0;
16+
bool increasing = 1; /** we will assume that the array is increasing at first **/
17+
for (int i = 1; i < n; i++) {
18+
if (a[i] < a[i - 1]) {
19+
increasing = 0;
20+
break;
21+
}
22+
}
23+
if (increasing) {
24+
cout << "YES\n";
25+
} else {
26+
cout << "NO\n";
27+
}
28+
return 0;
3529
}
Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,36 @@
11
// By Basel Bairkdar https://www.facebook.com/baselbairkdar
22

3-
#include<bits/stdc++.h>
4-
3+
#include<iostream>
54
using namespace std;
65

7-
const int N=205;
6+
int main() {
7+
8+
const int N = 100;
9+
10+
int n, a[N], mn = 10000, mx = -1;
811

9-
int n,a[N],mn=1e9,mx=-1,index_mn,index_mx;
12+
cin >> n;
13+
for (int i = 0; i < n; i++) {
14+
cin >> a[i];
15+
if (a[i] < mn) {
16+
mn = a[i];
17+
}
18+
if (a[i] > mx) {
19+
mx = a[i];
20+
}
21+
}
1022

11-
int main()
12-
{
13-
/**
14-
store minimum value in mn, maximum value in mx
15-
store the index of mn in index_mn, index of mx in index_mx
23+
for (int i = 0; i < n; i++) {
24+
if (a[i] == mn)
25+
a[i] = mx;
26+
else if (a[i] == mx)
27+
a[i] = mn;
28+
}
1629

17-
why initiating mn with large number and mx with small number ?
18-
try mn=0 and see what happens
19-
**/
20-
cin >> n;
21-
for (int i=0;i<n;i++)
22-
{
23-
cin >> a[i];
24-
if (a[i]<mn)
25-
{
26-
mn=a[i];
27-
index_mn=i;
28-
}
29-
if (a[i]>mx)
30-
{
31-
mx=a[i];
32-
index_mx=i;
33-
}
34-
}
35-
a[index_mn]=mx;
36-
a[index_mx]=mn;
37-
for (int i=0;i<n;i++)
38-
{
39-
cout << a[i] << ((i==n-1)?"\n":" ");
40-
}
41-
return 0;
30+
for (int i = 0; i < n; i++) {
31+
if (i)
32+
cout << " ";
33+
cout << a[i];
34+
}
35+
return 0;
4236
}

18-Programming-4kids/10_homework_04_answer.cpp

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// By Basel Bairkdar https://www.facebook.com/baselbairkdar
2+
#include<iostream>
3+
using namespace std;
4+
5+
int main() {
6+
/**
7+
we will put the first three elements from the array
8+
in a new array called mn which will store the minimum
9+
three values so far.
10+
11+
now we will iterate over the input array and see
12+
if the current element is less than the maximum element
13+
in array mn, if so we will make change.
14+
**/
15+
const int N = 200;
16+
int n, a[N], mn[3], mx;
17+
18+
cin >> n;
19+
for (int i = 0; i < n; i++) {
20+
cin >> a[i];
21+
if (i < 3) {
22+
mx = max(mx, a[i]);
23+
mn[i] = a[i];
24+
}
25+
}
26+
for (int i = 3; i < n; i++) {
27+
if (a[i] < mx) {
28+
for (int j = 0; j < 3; j++) {
29+
if (mn[j] == mx) {
30+
mn[j] = a[i];
31+
break;
32+
}
33+
}
34+
// update mx value
35+
for (int j = 0; j < 3; j++) {
36+
if (mn[j] > mx) {
37+
mx = mn[j];
38+
}
39+
}
40+
}
41+
}
42+
for (int i = 0; i < 3; i++) {
43+
cout << mn[i] << ((i == n - 1) ? "\n" : " ");
44+
}
45+
return 0;
46+
}
Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
// By Basel Bairkdar https://www.facebook.com/baselbairkdar
2+
#include<iostream>
3+
using namespace std;
24

3-
#include<bits/stdc++.h>
5+
int main() {
6+
const int N = 200;
47

5-
using namespace std;
8+
int n, a[N];
69

7-
const int N=205;
10+
// let's use some big value and later minimize
11+
int mn = 10000000;
812

9-
int n,a[N],mn=1e9;
13+
cin >> n;
14+
for (int i = 0; i < n; i++)
15+
cin >> a[i];
1016

11-
int main()
12-
{
13-
cin >> n;
14-
for (int i=0;i<n;i++)
15-
{
16-
cin >> a[i];
17-
}
18-
/**
19-
let's calculate Ai+Aj+j-i for every pair (i,j)
20-
such that i < j
21-
this can be done using nested for loops.
22-
**/
23-
for (int i=0;i<n;i++)
24-
{
25-
for (int j=i+1;j<n;j++)
26-
{
27-
int tmp=a[i]+a[j]+j-i;
28-
if (tmp<mn)
29-
{
30-
mn=tmp;
31-
}
32-
}
33-
}
34-
cout << mn << endl;
35-
return 0;
17+
/**
18+
let's calculate Ai+Aj+j-i for every pair (i,j)
19+
such that i < j
20+
this can be done using nested for loops.
21+
**/
22+
for (int i = 0; i < n; i++) {
23+
for (int j = i + 1; j < n; j++) {
24+
int tmp = a[i] + a[j] + j - i;
25+
if (tmp < mn) {
26+
mn = tmp;
27+
}
28+
}
29+
}
30+
cout << mn << endl;
31+
return 0;
3632
}

0 commit comments

Comments
 (0)