Just [Array]
Anower Hossain
7 min read
ICPC Assiut University Community Newcomers Training, Do Your Best
void solve() {
int n; cin>>n;
ll arr[n];
ll sum = 0;
for(int i=0; i<n; i++) {
cin >> arr[i];
sum += arr[i];
}
cout<< abs(sum) << '\n';
}
void solve() {
int n; cin >> n;
ll arr[n];
for(int i=0; i<n; i++) {
cin>> arr[i];
}
int x; cin >> x;
for(int i=0; i<n; i++) {
if(arr[i]==x) {
cout<< i << '\n';
return;
}
}
cout<< -1 << '\n';
}
void solve() {
int n; cin >> n;
ll arr[n];
for(int i=0; i<n; i++) {
cin>> arr[i];
}
for(int i=0; i<n; i++) {
if (arr[i] > 0) {
arr[i] = 1;
} else if (arr[i] < 0){
arr[i] = 2;
} else {
arr[i] = 0;
}
}
for(int i=0; i<n; i++) {
cout<< arr[i] << ' ';
}
cout<< '\n';
}
void solve() {
int n; cin >> n;
ll arr[n];
for(int i=0; i<n; i++) {
cin>> arr[i];
}
for(int i=0; i<n; i++) {
if(arr[i] <= 10) {
cout<< "A[" << i << "] = " << arr[i] << '\n';
}
}
}
oid solve() {
int n; cin >> n;
ll arr[n];
for(int i=0; i<n; i++) {
cin>> arr[i];
}
int mn = INT_MAX, idx = -1;
for(int i=0; i<n; i++) {
if (arr[i] < mn) {
mn = arr[i];
idx = i;
}
}
cout<< mn << " " << (idx+1) << '\n';
}
void solve() {
int n; cin >> n;
ll arr[n];
for(int i=0; i<n; i++) {
cin>> arr[i];
}
for(int i=n-1; i>=0; i--) {
cout<< arr[i] << ' ';
}
cout<< '\n';
}
void solve() {
int n; cin >> n;
ll arr[n];
for(int i=0; i<n; i++) {
cin>> arr[i];
}
bool is_palindrome = true;
for(int i=0; i<n/2; i++) {
if(arr[i] != arr[n-i-1]) {
is_palindrome = false;
break;
}
}
cout<< (is_palindrome ? "YES\n" : "NO\n");
}
void solve() {
int n; cin >> n;
ll arr[n];
for(int i=0; i<n; i++) {
cin>> arr[i];
}
sort(arr, arr+n);
for(int i=0; i<n; i++) {
cout<< arr[i] << ' ';
}
cout<< '\n';
}
void solve() {
int n; cin>>n;
int arr[n];
for(int i=0; i<n; i++) cin>>arr[i];
int mn_val = INT_MAX;
for(int i=0; i<n; i++) {
for (int j=i+1; j<n; j++) {
int cal = arr[i] + arr[j] + j - i;
mn_val = min(mn_val, cal);
}
}
cout<< mn_val << '\n';
}
void solve()
{
int n; cin>>n;
vector<int> a(n);
for(int i=0; i<n; i++) cin>>a[i];
int mn = *min_element(a.begin(), a.end());
int cnt = count(a.begin(), a.end(), mn);
if(cnt%2 == 1) cout<<"Lucky\n";
else cout<<"Unlucky\n";
}
void solve()
{
int n; cin>>n;
string s; cin>>s;
int digit_sum = 0;
for(char digit : s) {
digit_sum += digit - '0';
}
cout<< digit_sum <<'\n';
}
void sub_arr(int n, vector<int> arr) {
vector<int> mx_sub_arr;
for(int i=0; i<n; i++) {
int curr_mx_ele = arr[i];
for(int j=i; j<n; j++) {
curr_mx_ele = max(curr_mx_ele, arr[j]);
// cout<< curr_mx_ele<< ' ';
mx_sub_arr.push_back(curr_mx_ele);
}
}
for(int mx_val : mx_sub_arr) {
cout<< mx_val << " ";
}
cout<<'\n';
}
void solve() {
int n; cin>>n;
vector<int> arr(n);
for(int i=0; i<n; i++) cin>>arr[i];
int mx_idx = max_element(arr.begin(), arr.end()) - arr.begin();
int mn_idx = min_element(arr.begin(), arr.end()) - arr.begin();
swap(arr[mn_idx], arr[mx_idx]);
for(int num: arr) {
cout << num << " ";
}
cout<< '\n';
}
main{
int a,b; // 3 3
cin>>a>>b;
string code;
cin>>code;
if(a+b+1 != code.size()){
cout<<"No"<<endl;
return 0;
}
if(code[a] != '-'){
cout<<"No"<<endl;
return 0;
}
int counter = 0;
for(int i=0;i<code.size();i++){
if(code[i] >= 48 && code[i] <=57 && i != a){
counter++; // 6
}
}
if(counter == a+b){
cout<<"Yes"<<endl;
} else {
cout<<"No"<<endl;
}
}
ll fibo(int n) {
if(n == 1) return 0;
if(n == 2) return 1;
ll p1 = 0, p2 = 1, curr;
for(int i=3; i<=n; i++) {
curr = p1 + p2;
p1 = p2;
p2 = curr;
}
return p2;
}
void solve() {
int n; cin>>n;
int arr[n], mn = INT_MAX;
for(int i=0; i<n; i++) {
cin>>arr[i];
int c=0;
while(arr[i]%2 == 0 and arr[i]>0) {
c++;
arr[i]/=2;
}
mn = min(mn, c);
}
cout<< mn;
}
Q. Count Subarrays (Optimize Solution -> O(N)
void sub_array(int n, vector<int> arr) {
int c=0, len = 1;
for(int i=1; i<n; i++) {
if(arr[i] >= arr[i-1]) {
len++;
} else {
c += (len * (len+1) / 2);
len = 1;
}
}
c += (len * (len+1) / 2);
cout<< c <<'\n';
}
Q. Count Subarrays (Simple Solution -> O(N^2))
void sub_array_2(int n, vector<int> arr) { // O(N^2)
int c = 0;
for(int i=0; i<n; i++) {
c++;
for(int j=i+1; j<n; j++) {
if(arr[j] >= arr[j-1]) {
c++;
} else break;
}
}
cout << c << '\n';
}
void solve(int tc = 0) {
int n; cin>>n;
vector<int> a(n), b(n);
for(int i=0; i<n; i++) cin>>a[i];
for(int i=0; i<n; i++) cin>>b[i];
sort(a.begin(), a.end());
sort(b.begin(), b.end());
cout<< ((a == b) ? "yes":"no" );
}
void solve(int tc = 0) {
int n,m; cin>>n>>m;
int mat[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
cin>> mat[i][j];
}
}
int x; cin>>x;
bool found = false;
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
if(mat[i][j] == x) {
found = true;
break;
}
}
if(found) break;
}
cout<< ((found) ? "will not take number\n":"will take number\n");
}
void solve(int tc = 0) {
int n; cin>>n;
int mat[n][n];
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cin>> mat[i][j];
}
}
int P_sum = 0;
int S_sum = 0;
for(int i=0; i<n; i++) {
P_sum += mat[i][i];
S_sum += mat[i][n-1-i];
}
cout<< abs(P_sum - S_sum) << '\n';
}
void solve(int tc = 0) {
int n, m; cin>>n>>m;
vector<int> a(n), b(m);
for(int i=0; i<n; i++) cin>> a[i];
for(int i=0; i<m; i++) cin>> b[i];
int i=0, j=0;
while(i < n and j < m) {
if(a[i] == b[j]) {
j++;
};
i++;
}
cout << ((j==m) ? "YES\n":"NO\n");
}
void solve(int tc = 0) {
int n,m; cin>>n>>m;
vector<int> a(n), freq(m+1);
for(int i=0; i<n; i++) {
cin>>a[i];
freq[a[i]]++;
}
for(int i=1; i<=m; i++) {
cout<< freq[i] << '\n';
}
}
void solve(int tc = 0) {
int n,m; cin>>n>>m;
vector<vector<int>> mat(n, vector<int>(m+1));
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
cin>> mat[i][j];
}
}
for(int i=0; i<n; i++) {
for(int j=m-1; j>=0; j--) {
cout<< mat[i][j] <<" ";
}
cout<< '\n';
}
}
void solve(int tc = 0) {
int n,m; cin>>n>>m;
// vector<vector<char>> mat(n, vector<char>(m+1));
char mat[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
cin>> mat[i][j];
}
}
int x,y; cin>>x>>y;
// 0 - Based
x--,y--;
// Possible 8 direction X,Y
// vector<pair<int, int>> directions
int dxy[8][2] = {
{-1, 0}, {1, 0}, {0, 1}, {0, -1},
{-1, -1}, {-1, 1}, {1, -1}, {1, 1}
};
for(int i=0; i<8; i++) {
int nx = x + dxy[i][0];
int ny = y + dxy[i][1];
if(nx >= 0 and nx < n and ny >= 0 and ny < m and mat[nx][ny] != 'x') {
cout << "no\n";
return;
}
}
cout << "yes\n";
}
void solve(int tc = 0) {
ll n,q; cin>>n>>q;
ll a[n];
for(int i=0; i<n; i++) cin>>a[i];
ll pre[n];
pre[0] = a[0];
for(int i=1; i<n; i++) {
pre[i] = a[i] + pre[i-1];
}
while(q--) {
ll l,r; cin>>l>>r;
l--;
r--;
ll sum_res;
if(l==0) {
sum_res = pre[r];
} else {
sum_res = pre[r] - pre[l-1];
}
cout<< sum_res << '\n';
}
}
bool binary_search(int a[], int n, int x) {
int l = 0, r = n - 1;
while(l <= r) {
int mid = l + (r - l) / 2;
if(a[mid] == x) {
return true;
} else if(a[mid] < x) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return false;
}
void solve(int tc = 0) {
int n,q; cin>>n>>q;
int a[n];
for(int i=0; i<n; i++) cin>>a[i];
sort(a, a + n);
while(q--) {
int x; cin>>x;
if(binary_search(a, n, x)) {
cout << "found\n";
} else {
cout << "not found\n";
}
}
}
0
Subscribe to my newsletter
Read articles from Anower Hossain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Anower Hossain
Anower Hossain
This is Anower Hossain. Passionate about programming, and problem-solving.