Swap – Multiples of X and Y

The program must accept N integers and two integers X and Y as the input. The program must swap the first occurring multiple of X and the last occurring multiple of Y among the N integers. Then the program must print modified values of the N integers as the output.

Note:
At least one multiple of X and Y are always present in the N integers.

Boundary Condition(s):
2 <= N <= 100
1 <= X, Y <= 100
1 <= Each integer value <= 10^8

Input Format:
The first line contains the value of N.
The second line contains the value of N integers separated by space(s).
The third line contains the value of X and Y separated by space(s).

Output Format:
The first line contains the modified value of N integers as per the conditions.

Example Input/Output 1:
Input:
10
13 28 76 34 86 77 18 92 57 10
7 11
Output:
13 77 76 34 86 28 18 92 57 10
Explanation:
The first occurring multiple of 7 among the 10 integers is 28.
The last occurring multiple of 11 among the 10 integers is 77.
So these two values are swapped in their positions.
Hence the output is
13 77 76 34 86 28 18 92 57 10

Example Input/Output 2:
Input:
7
234 435 75 2345 6 435 875
5 5
Output:
234 875 75 2345 6 435 435

n = int(input())
numbers = list(map(int, input().split()))
x, y = map(int, input().split())

# Find the first occurring multiple of x
for i, num in enumerate(numbers):
    if num % x == 0:
        first_index = i
        break

# Find the last occurring multiple of y
for i, num in reversed(list(enumerate(numbers))):
    if num % y == 0:
        last_index = i
        break

# Swap the numbers
numbers[first_index], numbers[last_index] = numbers[last_index], numbers[first_index]

# Print the result
print(*numbers)
#include <stdio.h>

int main() {
    int n, x, y, first_index = -1, last_index = -1;
    scanf("%d", &n);

    int numbers[n];
    for (int i = 0; i < n; i++) {
        scanf("%d", &numbers[i]);
    }

    scanf("%d %d", &x, &y);

    for (int i = 0; i < n; i++) {
        if (numbers[i] % x == 0) {
            first_index = i;
            break;
        }
    }

    for (int i = n - 1; i >= 0; i--) {
        if (numbers[i] % y == 0) {
            last_index = i;
            break;
        }
    }

    int temp = numbers[first_index];
    numbers[first_index] = numbers[last_index];
    numbers[last_index] = temp;

    for (int i = 0; i < n; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}
#include <iostream>
#include <vector>

int main() {
    int n, x, y, first_index = -1, last_index = -1;
    std::cin >> n;

    std::vector<int> numbers(n);
    for (int i = 0; i < n; i++) {
        std::cin >> numbers[i];
    }

    std::cin >> x >> y;

    for (int i = 0; i < n; i++) {
        if (numbers[i] % x == 0) {
            first_index = i;
            break;
        }
    }

    for (int i = n - 1; i >= 0; i--) {
        if (numbers[i] % y == 0) {
            last_index = i;
            break;
        }
    }

    std::swap(numbers[first_index], numbers[last_index]);

    for (int i = 0; i < n; i++) {
        std::cout << numbers[i] << " ";
    }

    return 0;
}
import java.util.Scanner;

public class SwapMultiples {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        int[] numbers = new int[n];
        for (int i = 0; i < n; i++) {
            numbers[i] = scanner.nextInt();
        }

        int x = scanner.nextInt();
        int y = scanner.nextInt();

        int first_index = -1, last_index = -1;
        for (int i = 0; i < n; i++) {
            if (numbers[i] % x == 0) {
                first_index = i;
                break;
            }
        }

        for (int i = n - 1; i >= 0; i--) {
            if (numbers[i] % y == 0) {
                last_index = i;
                break;
            }
        }

        int temp = numbers[first_index];
        numbers[first_index] = numbers[last_index];
        numbers[last_index] = temp;

        for (int i = 0; i < n; i++) {
            System.out.print(numbers[i] + " ");
        }
    }
}
Previous Article

Consonants in Range

Next Article

Append Alphabet - Surrounding Integers

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *