Array Problems Solutions

A. Summation Time

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/Output Examples

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;
}

B. Searching

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/Output Examples

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;
}

C. Replacement

Given a number N and an array A of N numbers, print the array after doing the following operations:

Input/Output Examples

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;
}

D. Positions in Array

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/Output Examples

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;
}

E. Array Editing

You are given a number N and an array A of N integers. Your task is to:

  1. Remove all elements that are greater than 100 from the array.
  2. Then, extract a sub-array that contains the first K remaining elements after removal.
  3. Print the final sub-array.
Input/Output Examples

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;
}

F. Filter and Modify

You are given a number N and an array A of N integers. Your task is to:

  1. Keep only the even numbers in the array.
  2. Then, multiply each remaining number by 2.
  3. Finally, print the modified array.
Input/Output Examples

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;
}