Given a number N and an array A of N numbers, print the absolute summation of these numbers.
Absolute value means to remove any negative sign in front of a number.
Input:
4
7 2 1 3
Output:
13
Input:
3
-1 2 -3
Output:
2
#include <iostream> #include <cmath> using namespace std; int main() { int N; cin >> N; long long sum = 0; for (int i = 0; i < N; i++) { long long num; cin >> num; sum += num; } cout << abs(sum) << endl; return 0; }
Given a number N and an array A of N numbers, determine if the number X exists in array A or not and print its position (0-index).
Note: X may be found once or more than once and may not be found.
Input:
3
3 0 1
0
Output:
1
Input:
5
1 3 0 4 5
10
Output:
-1
#include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } int X; cin >> X; for (int i = 0; i < N; i++) { if (A[i] == X) { cout << i << endl; return 0; } } cout << -1 << endl; return 0; }
Given a number N and an array A of N numbers, print the array after doing the following operations:
Input:
5
1 -2 0 3 4
Output:
1 2 0 1 1
#include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } for (int i = 0; i < N; i++) { if (A[i] > 0) { A[i] = 1; } else if (A[i] < 0) { A[i] = 2; } } for (int i = 0; i < N; i++) { cout << A[i] << " "; } return 0; }
Given a number N and an array A of N numbers, print all array positions that store a number less than or equal to 10 and the number stored in that position.
It's guaranteed that there is at least one number in array less than or equal to 10.
Input:
5
1 2 100 0 30
Output:
A[0] = 1
A[1] = 2
A[3] = 0
#include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } for (int i = 0; i < N; i++) { if (A[i] <= 10) { cout << "A[" << i << "] = " << A[i] << endl; } } return 0; }
You are given a number N and an array A of N integers. Your task is to:
Input:
7 3
5 200 15 300 8 100 55
Output:
5 15 8
#include <iostream> #include <vector> using namespace std; int main() { int N, K; cin >> N >> K; vector<int> A; for (int i = 0; i < N; i++) { int num; cin >> num; if (num <= 100) { A.push_back(num); } } for (int i = 0; i < K && i < A.size(); i++) { cout << A[i] << " "; } return 0; }
You are given a number N and an array A of N integers. Your task is to:
Input:
6
3 4 7 10 9 2
Output:
8 20 4
#include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> A; for (int i = 0; i < N; i++) { int num; cin >> num; if (num % 2 == 0) { A.push_back(num * 2); } } for (int num : A) { cout << num << " "; } return 0; }