Swap three numbers in cycle
Given three numbers, swap them in cyclic form. First number should get the value of third, second should get the value of first and third should get value of second. Examples:
Input : a = 2, b = 4, c = 7
Output : a = 7, b = 2, c = 4
Input : a = 10, b = 20, c = 30
Output : a = 30, b = 10, c = 20
Prerequisite: Pointers, Call by Reference, The idea is to extend simple two variable swapping.
// Before overwriting b, store its
// value in temp.
temp = b;
// Now do required swapping starting
// with b.
b = a;
a = c;
c = temp;
C
#include <stdio.h>
void cyclicSwap( int * a, int * b, int * c)
{
int temp = *b;
*b = *a;
*a = *c;
*c = temp;
}
int main()
{
int a = 2, b = 4, c = 7;
printf ( "Value before swapping:\n" );
printf ( "a = %d \nb = %d \nc = %d\n" , a, b, c);
cyclicSwap(&a, &b, &c);
printf ( "Value after swapping:\n" );
printf ( "a = %d \nb = %d \nc = %d" , a, b, c);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
void cyclicSwap( int * a, int * b, int * c)
{
int temp = *b;
*b = *a;
*a = *c;
*c = temp;
}
int main()
{
int a = 2, b = 4, c = 7;
cout << "Value before swapping:" << endl;
cout << "a = " << a << " \nb = " << b << " \nc = " << c << endl;
cyclicSwap(&a, &b, &c);
cout << "Value after swapping:" << endl;
cout << "a = " << a << " \nb = " << b << " \nc = " << c << endl;
return 0;
}
|
Java
public class Main {
public static void cyclicSwap( int [] arr) {
int temp = arr[ 1 ];
arr[ 1 ] = arr[ 0 ];
arr[ 0 ] = arr[ 2 ];
arr[ 2 ] = temp;
}
public static void main(String[] args) {
int a = 2 , b = 4 , c = 7 ;
int [] arr = {a, b, c};
System.out.println( "Value before swapping:" );
System.out.println( "a = " + arr[ 0 ] + "\nb = " + arr[ 1 ] + "\nc = " + arr[ 2 ]);
cyclicSwap(arr);
System.out.println( "Value after swapping:" );
System.out.println( "a = " + arr[ 0 ] + "\nb = " + arr[ 1 ] + "\nc = " + arr[ 2 ]);
}
}
|
Python3
def cyclic_swap(arr):
temp = arr[ 1 ]
arr[ 1 ] = arr[ 0 ]
arr[ 0 ] = arr[ 2 ]
arr[ 2 ] = temp
a, b, c = 2 , 4 , 7
arr = [a, b, c]
print ( "Values before swapping:" )
print ( "a =" , arr[ 0 ], "\nb =" , arr[ 1 ], "\nc =" , arr[ 2 ])
cyclic_swap(arr)
print ( "Values after swapping:" )
print ( "a =" , arr[ 0 ], "\nb =" , arr[ 1 ], "\nc =" , arr[ 2 ])
|
C#
using System;
class GFG {
static void cyclicSwap( ref int a,
ref int b, ref int c)
{
int temp = b;
b = a;
a = c;
c = temp;
}
public static void Main()
{
int a = 2, b = 4, c = 7;
Console.Write( "Value before swapping:\n" );
Console.Write( "a = " + a + "\n" + "b = " +
b + "\n" + "c = " + c + "\n" );
cyclicSwap( ref a, ref b, ref c);
Console.Write( "Value after swapping:\n" );
Console.Write( "a = " + a + "\n" + "b = " +
b + "\n" + "c = " + c + "\n" );
}
}
|
Javascript
function cyclicSwap(arr) {
let temp = arr[1];
arr[1] = arr[0];
arr[0] = arr[2];
arr[2] = temp;
}
let a = 2, b = 4, c = 7;
let arr = [a, b, c];
console.log( "Value before swapping:" );
console.log( "a = " + arr[0] + "\nb = " + arr[1] + "\nc = " + arr[2]);
cyclicSwap(arr);
console.log( "Value after swapping:" );
console.log( "a = " + arr[0] + "\nb = " + arr[1] + "\nc = " + arr[2]);
|
Output:
Value before swapping:
a = 2
b = 4
c = 7
Value after swapping:
a = 7
b = 2
c = 4
Time complexity : O(1) because constant operations are done
Auxiliary space: O(1)
Last Updated :
20 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...