JXNVCE ALGO-LOG YouJin Jung

A-BOOLEAN-ARRAY-PUZZLE

#include <bits/stdc++.h>
using namespace std;

void changeToZero(int a[2]) {
    a[ a[1] ] = a[ !a[1] ];
}

int main() {
    int a[] = {1, 0};
    changeToZero(a);
     
    cout<<"arr[0] = "<<a[0]<<endl;
    cout<<" arr[1] = "<<a[1];
    return 0;
}

Gfg Smallest Of Three


layout: post title: GFG-SMALLEST-OF-THREE —

using bit operations!

#include <bits/stdc++.h>
using namespace std;

int smallest(int x, int y, int z) {
    int c = 0;
    while (x && y && z) {
        x--;
        y--;
        z--;
        c++;
    }
    return c;
}

int main() {
    int x = 12, y = 15, z = 5;
    cout << "Minimum of 3 numbers is " << smallest(x, y, z);
    return 0;
}

GFG-SWAP-BITS

#include <bits/stdc++.h>
using namespace std;
 
int swapBits(unsigned int x, unsigned int p1, unsigned int p2, unsigned int n) {
    unsigned int set1 = (x >> p1) & ((1U << n) - 1);
    unsigned int set2 = (x >> p2) & ((1U << n) - 1);
 
    unsigned int Xor = (set1 ^ set2);
    Xor = (Xor << p1) | (Xor << p2);

    unsigned int result = x ^ Xor;
 
    return result;
}

int main() {
    int res = swapBits(28, 0, 3, 2);
    cout << "Result = " << res;
    return 0;
}

GFG-FLOOR-IN-SORTED-ARRAY

#include <iostream>
using namespace std;

int floorSearch(int arr[], int n, int x) {
    if (x >= arr[n - 1])
        return n - 1;
 
    if (x < arr[0])
        return -1;

    for (int i = 1; i < n; i++)
        if (arr[i] > x)
            return (i - 1);
 
    return -1;
}
 
int main() {
    int arr[] = { 1, 2, 4, 6, 10, 12, 14 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 7;
    int index = floorSearch(arr, n - 1, x);
    if (index == -1)
        cout<<"Floor of "<<x <<" doesn't exist in array ";
    else
        cout<<"Floor of "<< x <<" is " << arr[index];
    return 0;
}

GFG-SWAP-BITS-IN-A-GIVEN-NUMBER

#include <iostream>
using namespace std;
 
int swapBits(unsigned int num, unsigned int p1, unsigned int p2, unsigned int n) {
    int shift1, shift2, value1, value2;
    while (n--) {
        shift1 = 1 << p1;
        shift2 = 1 << p2;
 
        value1 = ((num & shift1));
        value2 = ((num & shift2));
 
        if ((!value1 && value2) || (!value2 && value1)) {
            if (value1) {
                num = num & (~shift1);
                num = num | shift2;
            }
            else {
                num = num & (~shift2);
                num = num | shift1;
            }
        }
        p1++;
        p2++;
    }
    return num;
}
 
int main() {
    int res = swapBits(28, 0, 3, 2);
    cout << "Result = " << res;
    return 0;
}