All submissions

Code

#include <stdio.h>

void swap(int *p1, int *p2) {
    // Your logic here
    int temp;
    temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

int main() {
    int a, b;
    scanf("%d %d", &a, &b);

    swap(&a, &b);

    printf("a = %d ", a);
    printf("b = %d", b);

    return 0;
}

Solving Approach

 

✅ Step-by-Step Algorithm

  1. Start
  2. Read two pointers: ptr1 and ptr2
  3. Create a temporary variable temp
  4. Assign temp = *ptr1 (store value at ptr1)
  5. Assign *ptr1 = *ptr2 (copy value from ptr2 to ptr1)
  6. Assign *ptr2 = temp (restore original ptr1 value into ptr2)
  7. End

✅ Step-by-Step Algorithm (Using XOR)

  1. Start
  2. Read two pointers: ptr1 and ptr2
  3. Check if ptr1 != ptr2 (to avoid zeroing the value if both point to same location)
  4. Perform XOR swap:
    1. *ptr1 = *ptr1 ^ *ptr2
    2. *ptr2 = *ptr1 ^ *ptr2
    3. *ptr1 = *ptr1 ^ *ptr2
  5. End

 

 

Loading...

Input

10 20

Expected Output

a = 20 b = 10