Skip to content

Commit a755171

Browse files
committed
local
1 parent ca5360a commit a755171

13 files changed

+664
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// By Basel Bairkdar https://www.facebook.com/baselbairkdar
2+
#include<bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
const int N=205;
7+
8+
int n,a[N],mn=1e9;
9+
10+
int main()
11+
{
12+
cin >> n;
13+
for (int i=0;i<n;i++)
14+
{
15+
cin >> a[i];
16+
}
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+
{
24+
for (int j=i+1;j<n;j++)
25+
{
26+
int tmp=a[i]+a[j]+j-i;
27+
if (tmp<mn)
28+
{
29+
mn=tmp;
30+
}
31+
}
32+
}
33+
cout << mn << endl;
34+
return 0;
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// By Basel Bairkdar https://www.facebook.com/baselbairkdar
2+
3+
#include<bits/stdc++.h>
4+
5+
using namespace std;
6+
7+
const int N=205;
8+
9+
int n,a[N];
10+
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;
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// By Basel Bairkdar https://www.facebook.com/baselbairkdar
2+
3+
#include<bits/stdc++.h>
4+
5+
using namespace std;
6+
7+
const int N=205;
8+
9+
int n,a[N],mn=1e9,mx=-1,index_mn,index_mx;
10+
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
16+
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;
42+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// By Basel Bairkdar https://www.facebook.com/baselbairkdar
2+
3+
#include<bits/stdc++.h>
4+
5+
using namespace std;
6+
7+
const int N=205;
8+
9+
int n,a[N],mn[3],mx;
10+
11+
int main()
12+
{
13+
/**
14+
we will put the first three elements from the array
15+
in a new array called mn which will store the minimum
16+
three values so far.
17+
18+
now we will iterate over the input array and see
19+
if the current element is less than the maximum element
20+
in array mn, if so we will make change.
21+
**/
22+
cin >> n;
23+
for (int i=0;i<n;i++)
24+
{
25+
cin >> a[i];
26+
if (i<3)
27+
{
28+
mx=max(mx,a[i]);
29+
mn[i]=a[i];
30+
}
31+
}
32+
for (int i=3;i<n;i++)
33+
{
34+
if (a[i]<mx)
35+
{
36+
for (int j=0;j<3;j++)
37+
{
38+
if (mn[j]==mx)
39+
{
40+
mn[j]=a[i];
41+
break;
42+
}
43+
}
44+
// update mx value
45+
for (int j=0;j<3;j++)
46+
{
47+
if (mn[j]>mx)
48+
{
49+
mx=mn[j];
50+
}
51+
}
52+
}
53+
}
54+
for (int i=0;i<3;i++)
55+
{
56+
cout << mn[i] << ((i==n-1)?"\n":" ");
57+
}
58+
return 0;
59+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// By Basel Bairkdar https://www.facebook.com/baselbairkdar
2+
3+
#include<bits/stdc++.h>
4+
5+
using namespace std;
6+
7+
const int N=205;
8+
9+
int n,a[N],mn=1e9;
10+
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;
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// By Amr Keleg https://www.facebook.com/amr.keleg
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
int main() {
7+
// The size of the array should be larger than
8+
// the maximum value of N
9+
int arr[1000];
10+
11+
int N;
12+
cin>>N;
13+
14+
for(int i=0; i<N; i++)
15+
cin>>arr[i];
16+
17+
// We need to compare the value at the left half
18+
// to those in the right half
19+
// The value at index (0) should be compared to the value at index (N-1)
20+
// Then the value at index (1) should be compared to the value at index (N-2)
21+
// So, clearly we need to increment the left index and decrement the right one
22+
// On reaching the index N/2, we are sure that we have compared all the elements
23+
// of both sides so we don't need to do anything and the array is palindrome
24+
for (int i=0; i< N/2; i++){
25+
if (arr[i] != arr[N-1-i])
26+
{
27+
cout<<"NO";
28+
return 0;
29+
}
30+
}
31+
cout<<"YES";
32+
return 0;
33+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// By Amr Fahim https://www.facebook.com/amrfahim2020
2+
// Homework 7 solution
3+
#include <iostream>
4+
using namespace std ;
5+
6+
// We use 2 frequency arrays one for the positive numbers and the other for the negative
7+
8+
int n , neg_num[500 +10] , pos_num[270 + 10 ] , max_num , max_freq ;
9+
10+
int main()
11+
{
12+
cin >> n ;
13+
int arr[n] ;
14+
15+
for (int i = 0 ; i < n ;i++ )
16+
{
17+
cin >> arr[i] ;
18+
// Check the sign of the input value
19+
//to know which array we should add the deal with
20+
if (arr[i] >= 0 )
21+
{
22+
pos_num[arr[i]]++ ;
23+
24+
// Comparing the value we have now with the max value
25+
if (pos_num[arr[i]] > max_freq )
26+
{
27+
max_freq = pos_num[arr[i]] ;
28+
max_num = arr[i] ;
29+
30+
}
31+
32+
}
33+
else if (arr[i] < 0 )
34+
{
35+
neg_num[arr[i]]++ ;
36+
37+
// Comparing the value we have now with the max value
38+
if (neg_num[arr[i]] > max_freq )
39+
{
40+
max_freq = neg_num[arr[i]] ;
41+
max_num = arr[i] ;
42+
}
43+
}
44+
45+
46+
}
47+
cout << max_num <<" has repeated " << max_freq <<" times " ;
48+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// By Amr Fahim https://www.facebook.com/amrfahim2020
2+
// Homework 8 solution
3+
4+
#include <iostream>
5+
using namespace std ;
6+
7+
int n , x , occurrence[10] ;
8+
9+
int main()
10+
{
11+
cin >> n ;
12+
13+
for (int i = 0 ; i < n ;i++ )
14+
{
15+
cin >> x ;
16+
17+
while(x)
18+
{
19+
int digit = x%10 ;
20+
occurrence[digit]++ ;
21+
x /= 10 ;
22+
}
23+
24+
}
25+
for (int i = 0 ; i <= 9 ; i++)
26+
{
27+
cout << i <<" " << occurrence[i] << endl ;
28+
}
29+
30+
}
31+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// By Amr Fahim https://www.facebook.com/amrfahim2020
2+
// Homework 9 solution
3+
#include <iostream>
4+
using namespace std ;
5+
6+
const int N = 2e5+10 ;
7+
int occurrence[N] , sequence[N] , indx ;
8+
9+
int main()
10+
{
11+
cin >> indx ;
12+
sequence[0] = 0 ;
13+
occurrence[0] = 1 ;
14+
15+
for (int i = 1 ; i <= indx ;i++ )
16+
{
17+
int cur = sequence[i-1] - (i-1) -1 ;
18+
19+
if (cur < 0 || occurrence[cur] )
20+
{
21+
cur = sequence[i-1] + (i-1) + 1 ;
22+
}
23+
sequence[i] = cur ;
24+
occurrence[cur] = 1 ;
25+
26+
}
27+
28+
cout << sequence[indx] ;
29+
30+
}
31+
32+

0 commit comments

Comments
 (0)