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 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 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 seperated by space(s).
The third line contains the value of X and Y seperated 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 swaped 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

Solution :-

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

int main()
{
    int n,x,y;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    int xarr[n],yarr[n],a=0,b=0;;
    scanf("%d %d",&x,&y);
    
    for(int i=0;i<n;i++)
    {
        if(arr[i]%x==0)
        xarr[a++]=arr[i];
        
        if(arr[i]%y==0)
        yarr[b++]=arr[i];
    }
    a=0;
    b=0;
    for(int i=0;i<n;i++)
    {
        if(arr[i]%x==0)
        arr[i]=yarr[a++];
        
        if(arr[i]%y==0)
        arr[i]=xarr[b++];
        
    }
    
    for(int i=0;i<n;i++)
    {
        printf("%d ",arr[i]);
    }


}

Comments

Popular posts from this blog

Two Matrix Spiral Print

Alphabets Positions Reversed

Odd Factors - (Error Identification) skillrack program id - 7306