Skip to content

Instantly share code, notes, and snippets.

@vlaleli
Created December 5, 2025 12:16
Show Gist options
  • Select an option

  • Save vlaleli/aa9bb4e874201ec7fd4f283fd24dc294 to your computer and use it in GitHub Desktop.

Select an option

Save vlaleli/aa9bb4e874201ec7fd4f283fd24dc294 to your computer and use it in GitHub Desktop.
#include "functions.h"
#include <algorithm>
#include <cmath>
int sumDigits(const vector<int>& arr) {
int sum = 0;
for (int x : arr) {
int t = abs(x);
while (t > 0) {
sum += t % 10;
t /= 10;
}
}
return sum;
}
double averagePositive(const vector<int>& arr) {
double s = 0;
int k = 0;
for (int x : arr) {
if (x > 0) {
s += x;
k++;
}
}
if (k == 0) return 0;
return s / k;
}
pair<int,int> maxNegative(const vector<int>& arr) {
int best = -1000000;
int pos = -1;
for (int i = 0; i < arr.size(); i++) {
if (arr[i] < 0 && arr[i] > best) {
best = arr[i];
pos = i;
}
}
return {best, pos};
}
int mostFrequent(const vector<int>& arr) {
int bestValue = arr[0];
int bestCount = 0;
for (int i = 0; i < arr.size(); i++) {
int c = 0;
for (int j = 0; j < arr.size(); j++)
if (arr[i] == arr[j]) c++;
if (c > bestCount) {
bestCount = c;
bestValue = arr[i];
}
}
return bestValue;
}
int maxOddIndexDiv3(const vector<int>& arr) {
int best = -1000000;
bool found = false;
for (int i = 1; i < arr.size(); i += 2) {
if (arr[i] % 3 == 0) {
if (!found || arr[i] > best) {
best = arr[i];
found = true;
}
}
}
if (!found) return 0;
return best;
}
void swapMaxEvenMinOdd(vector<int>& arr) {
int maxEven = -1000000, posEven = -1;
int minOdd = 1000000, posOdd = -1;
for (int i = 0; i < arr.size(); i++) {
if (arr[i] % 2 == 0 && arr[i] > maxEven) {
maxEven = arr[i];
posEven = i;
}
if (arr[i] % 2 != 0 && arr[i] < minOdd) {
minOdd = arr[i];
posOdd = i;
}
}
if (posEven == -1 || posOdd == -1) {
for (int i = 0; i < arr.size(); i++)
arr[i] = 0;
} else {
int t = arr[posEven];
arr[posEven] = arr[posOdd];
arr[posOdd] = t;
}
}
vector<int> intersectionArr(const vector<int>& a, const vector<int>& b) {
vector<int> r;
for (int x : a)
for (int y : b)
if (x == y) r.push_back(x);
sort(r.begin(), r.end());
r.erase(unique(r.begin(), r.end()), r.end());
return r;
}
vector<int> diffArr(const vector<int>& a, const vector<int>& b) {
vector<int> r;
for (int x : a) {
bool ok = true;
for (int y : b)
if (x == y) ok = false;
if (ok) r.push_back(x);
}
for (int y : b) {
bool ok = true;
for (int x : a)
if (x == y) ok = false;
if (ok) r.push_back(y);
}
return r;
}
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <vector>
using namespace std;
int sumDigits(const vector<int>& arr);
double averagePositive(const vector<int>& arr);
pair<int,int> maxNegative(const vector<int>& arr);
int mostFrequent(const vector<int>& arr);
int maxOddIndexDiv3(const vector<int>& arr);
void swapMaxEvenMinOdd(vector<int>& arr);
vector<int> intersectionArr(const vector<int>& a, const vector<int>& b);
vector<int> diffArr(const vector<int>& a, const vector<int>& b);
#endif
#include <iostream>
#include <vector>
#include "functions.h"
using namespace std;
int main() {
vector<int> arr{12, 104, 81, -5, -10, 3, 9, 9};
cout << sumDigits(arr) << endl;
cout << averagePositive(arr) << endl;
auto neg = maxNegative(arr);
cout << neg.first << " " << neg.second << endl;
cout << mostFrequent(arr) << endl;
cout << maxOddIndexDiv3(arr) << endl;
swapMaxEvenMinOdd(arr);
for (int x : arr) cout << x << " ";
cout << endl;
vector<int> a{1,2,3,4,5};
vector<int> b{4,5,6,7};
vector<int> both = intersectionArr(a, b);
for (int x : both) cout << x << " ";
cout << endl;
vector<int> one = diffArr(a, b);
for (int x : one) cout << x << " ";
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment