Posts

Add two numbers without using arithmetic operators ?

Write a function Add() that returns sum of two integers. The function should not use any of the arithmetic operators (+, ++, –, -, .. etc). Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits. Above is simple Half Adder logic that can be used to add 2 single bits. We can extend this logic for integers. If x and y don’t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&) is used. Bitwise AND of x and y gives all carry bits. We calculate (x & y) << 1 and add it to x ^ y to get the required result.

How to swap two numbers without using a temporary variable?

method 1:    #include <stdio.h> int main() {    int x = 10, y = 5;      // Code to swap 'x' and 'y'    x = x + y;  // x now becomes 15    y = x - y;  // y becomes 10    x = x - y;  // x becomes 5      printf ( "After Swapping: x = %d, y = %d" , x, y);      return 0; }       Method 2 (Using Bitwise XOR)     #include <stdio.h> int main() {    int x = 10, y = 5;      // Code to swap 'x' (1010) and 'y' (0101)    x = x ^ y;  // x now becomes 15 (1111)    y = x ^ y;  // y becomes 10 (1010)    x = x ^ y;  // x becomes 5 (0101)      printf ( "After Swapping: x = %d, y = %d" , x, y);      return 0; }  

You are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing in the list. Write an efficient code to find the missing integer.

You are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing in the list. Write an efficient code to find the missing integer. #Note : the numbers are not in sorted order Algorithm : 1. Get the sum of numbers total = n*(n+1)/2 2 Subtract all the numbers from sum and you will get the missing number.     Note : Time complexity is O(n);   Algorithm : Using EX-OR Here we can use same concept of finding which element is odd number times in the given list. 1) XOR all the array elements, let the result of XOR be X1. 2) XOR all numbers from 1 to n, let XOR be X2. 3) XOR of X1 and X2 gives the missing number.

Given a set of numbers where all elements occur even number of times except one number, find the odd occuring number ?

   // Function to return the only odd occurring element int findOdd( int arr[], int n) {     int res = 0, i;     for (i = 0; i < n; i++)       res ^= arr[i];     return res; }   int main( void ) {     int arr[] = {12, 12, 14, 90, 14, 14, 14};     int n = sizeof (arr)/ sizeof (arr[0]);     printf ( "The odd occurring element is %d " , findOdd(arr, n));     return 0; } //   

Write a program to reverse a string?

Algorithm 1 using stack : 1) Create an empty stack. 2) One by one push all characters of string to stack. 3) One by one pop all characters from stack and put them back to string.         Algorithm 2 using array:   1. here we have to swap the elements