Open In App
Related Articles

Swap three numbers in cycle

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

// C program to perform Cyclic Swapping
// using Call by Reference
#include <stdio.h>
 
void cyclicSwap(int* a, int* b, int* c)
{
    // Before overwriting b, store its
    // value in temp.
    int temp = *b;
 
    // Now do required swapping starting
    // with 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++

// C++ program to perform Cyclic Swapping
// using Call by Reference
#include <bits/stdc++.h>
using namespace std;
 
void cyclicSwap(int* a, int* b, int* c)
{
    // Before overwriting b, store its
    // value in temp.
    int temp = *b;
 
    // Now do required swapping starting
    // with 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) {
        // Before overwriting arr[1], store its value in temp.
        int temp = arr[1];
 
        // Now do required swapping starting with 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]);
    }
}
// This code is contributed by sarojmcy2e

                    

Python3

# Python code addition
 
def cyclic_swap(arr):
    # Before overwriting arr[1], store its value in temp.
    temp = arr[1]
   
    # Now do required swapping starting with 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])
 
# The code  is contributed by Nidhi goel.

                    

C#

// C# program to perform Cyclic Swapping
// using Call by Reference
using System;
class GFG {
     
    static void cyclicSwap(ref int a,
                   ref int b, ref int c)
    {
         
        // Before overwriting b, store
        // its value in temp.
        int temp = b;
     
        // Now do required swapping
        // starting with b.
        b = a;
        a = c;
        c = temp;
    }
     
    // Driver code
    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");
    }
}
 
// This code is contributed by Sam007.

                    

Javascript

// Javascript code addition
 
function cyclicSwap(arr) {
    // Before overwriting arr[1], store its value in temp.
    let temp = arr[1];
 
    // Now do required swapping starting with 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]);
 
 
// This code is contributed by Arushi Jindal.

                    

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
Previous
Next
Share your thoughts in the comments
Similar Reads