C

Swap-Multiples Of x And y

80
0

The program must accept N integers and two integers X and Y as the input. The program must swap the first occurring mutiple 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 Conditions:

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 20. The last occurring multiple of 11 among the 10 integers is 77. So these two values are swaped in their positions. Hence the output is 13 77 76 34 86 28 18 92 57 10

#include<stdio.h> 
#include<stdlib.h>

int main()

{
	int n;
	scanf("%d",&n);
	int a[n]; 
	for (int i=0;i<n;i++) 
		scanf("%d",&a[i]);
	int x,y; 
	scanf("%d %d",&x,&y); 
	int r,f; 
	for (int i=0;i<n;i++)
	{
		if(a[i]%x==0)
		{
			r=i; 
			break;
		}
	}
	for (int i=n-1;i>=0;i--)
	{
		if(a[i]%y==0)
		{
			f=i; 
			break;
		}
	}

	int t=a[r]; 
	a[r]=a[f];
	a[f]=t; 
	for (int i=0;i<n;i++) 
		printf("%d ",a[i]);
}
Hephzibai
WRITTEN BY

Hephzibai

Hephzibai is a driven third-year student at St. Joseph's Institute of Technology, specializing in Computer Science and Engineering. With a keen interest in data science and also in fullstack.
One of my greatest strengths lies in my programming skills, which I've honed through countless hours of practice and participation in coding challenges. Platforms like Skillrack, HackerRank, and others have been my playgrounds, where I've tackled a wide range of problems with creativity and determination.

Leave a Reply

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