Norway


There are many situations where we use integer values as index in array to see presence or absence, we can use bit manipulations to optimize in such problems.

Let us consider below problem as an example.

Given two numbers say a and b, mark the multiples of 2 and 5 between a and b using less than O(|b – a|) space and output each of the multiples.
Note : We have to mark the multiples i.e save (, value) pairs in memory such that each either have value as 1 or 0 representing as multiple of 2 or 5 or not respectively.
Examples :

Input : 2 
Output : 2 4 5 6 8 

Input: 60 95
Output: 60 62 64 65 66 68 70 72 74 75 76 78 
        80 82 84 85 86 88 90 92 94 95

Approach 1 (Simple):
Hash the indices in an array from a to b and mark each of the indices as 1 or 0.
Space complexity : O(max(a, b))

- Array1 - Space optimization using bit manipulations

Approach 2 (Better than simple):
Save memory, by translating a to 0th index and b to (b-a)th index.
Space complexity : O(|b-a|).

- Array updated - Space optimization using bit manipulations

Simply hash |b – a| positions of an array as 0 and 1.

// CPP program to mark numbers as multiple of 2 or 5
#include <bits/stdc++.h>
using namespace std;

// Driver code
int main()
{
    int a = 2, b = 10;
    int size = abs(b - a) + 1;
    int* array = new int[size];

    // Iterate through a to b, If it is a multiple
    // of 2 or 5 Mark index in array as 1
    for (int i = a; i <= b; i++)
        if (i % 2 == 0 || i % 5 == 0)
            array[i - a] = 1;

    cout << "MULTIPLES of 2 and 5:n";
    for (int i = a; i <= b; i++)
        if (array[i - a] == 1)
            cout << i << " ";

    return 0;
}
Output:
MULTIPLES of 2 and 5:
2 4 5 6 8 10

Approach 3 (Using Bit Manipulations):

Here is a space optimized which uses bit manipulation technique that can be applied to problems mapping binary values in arrays.
Size of int variable in 64-bit compiler is 4 bytes. 1 byte is represented by 8 bit positions in memory. So, an integer in memory is represented by 32 bit positions(4 Bytes) these 32 bit positions can be used instead of just one index to hash binary values.

- Array 32bit - Space optimization using bit manipulations

// CPP code to for marking multiples
#include <bits/stdc++.h>
using namespace std;

// index >> 5 corresponds to dividing index by 32
// index & 31 corresponds to modulo operation of 
// index by 32

// Function to check value of bit position whether 
// it is zero or one
bool checkbit(int array[], int index)
{
    return array[index >> 5] & (1 << (index & 31));
}

// Sets value of bit for corresponding index
void setbit(int array[], int index)
{
    array[index >> 5] |= (1 << (index & 31));
}

/* Driver program to test above functions*/
int main()
{
    int a = 2, b = 10;
    int size = abs(b - a);

    // Size that will be used is actual_size/32
    // ceil is used to initialize the array with
    // positive number
    size = ceil(size / 32);
    
    // Array is dynamically initialized as
    // we are calculating size at run 
    int* array = new int[size];

    // Iterate through every index from a to b and
    // call setbit() if it is a multiple of 2 or 5
    for (int i = a; i <= b; i++)
        if (i % 2 == 0 || i % 5 == 0)
            setbit(array, i - a);

    cout << "MULTIPLES of 2 and 5:n";
    for (int i = a; i <= b; i++)
        if (checkbit(array, i - a))
            cout << i << " ";

    return 0;
}
Output:
MULTIPLES of 2 and 5:
2 4 5 6 8 10


- icfgkr5m0yqrc3z9v0j7 - Space optimization using bit manipulations

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.







Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here