From c55ce4b125a236decd1607a891ca4b69b78b945e Mon Sep 17 00:00:00 2001 From: SAHIL SHARMA <76551267+KIET7UKE@users.noreply.github.com> Date: Thu, 20 Oct 2022 18:40:01 +0530 Subject: [PATCH] Added Bit Set Algorithm Solutions --- BIT set/Bit Difference.cpp | 54 +++++++++++++++ ...of a number without using pow and sqrt.cpp | 34 +++++++++ ...tiplication, division and mod operator.cpp | 49 +++++++++++++ BIT set/Find position of set bit.cpp | 43 ++++++++++++ BIT set/Non Repeating Numbers.cpp | 69 +++++++++++++++++++ 5 files changed, 249 insertions(+) create mode 100644 BIT set/Bit Difference.cpp create mode 100644 BIT set/Calculate square of a number without using pow and sqrt.cpp create mode 100644 BIT set/Divide two integers without using multiplication, division and mod operator.cpp create mode 100644 BIT set/Find position of set bit.cpp create mode 100644 BIT set/Non Repeating Numbers.cpp diff --git a/BIT set/Bit Difference.cpp b/BIT set/Bit Difference.cpp new file mode 100644 index 0000000..86bf312 --- /dev/null +++ b/BIT set/Bit Difference.cpp @@ -0,0 +1,54 @@ +// { Driver Code Starts +//Initial Template for C++ + +#include +using namespace std; + + + // } Driver Code Ends +//User function Template for C++ + +class Solution{ + public: + // Function to find number of bits needed to be flipped to convert A to B + int countBitsFlip(int a, int b){ + + // Your logic here + int diff=0; + if(a0&&b>0) + { + if(a%2!=b%2)//if any bit does not matches + diff++; + a/=2; + b/=2; + } + while(a>0)// if(still a bits are left) + { + if(a%2==1)//a bit is set than only set will different + diff++; + a/=2; + + } + return diff; + } +}; + +// { Driver Code Starts. + +// Driver Code +int main() +{ + int t; + cin>>t;// input the testcases + while(t--) //while testcases exist + { + int a,b; + cin>>a>>b; //input a and b + + Solution ob; + cout< +using namespace std; + +int square(int n) +{ + // Base case + if (n == 0) + return 0; + + // Handle negative number + if (n < 0) + n = -n; + + // Get floor(n/2) using right shift + int x = n >> 1; + + // If n is odd + if (n & 1) + return ((square(x) << 2) + (x << 2) + 1); + else // If n is even + return (square(x) << 2); +} + +// Driver Code +int main() +{ + // Function calls + for (int n = 1; n <= 5; n++) + cout << "n = " << n << ", n^2 = " << square(n) + << endl; + return 0; +} + diff --git a/BIT set/Divide two integers without using multiplication, division and mod operator.cpp b/BIT set/Divide two integers without using multiplication, division and mod operator.cpp new file mode 100644 index 0000000..2f789bd --- /dev/null +++ b/BIT set/Divide two integers without using multiplication, division and mod operator.cpp @@ -0,0 +1,49 @@ +// C++ implementation to Divide two +// integers without using multiplication, +// division and mod operator +#include +using namespace std; + +// Function to divide a by b and +// return floor value it +int divide(long long dividend, long long divisor) { + +// Calculate sign of divisor i.e., +// sign will be negative only iff +// either one of them is negative +// otherwise it will be positive +int sign = ((dividend < 0) ^ + (divisor < 0)) ? -1 : 1; + +// remove sign of operands +dividend = abs(dividend); +divisor = abs(divisor); + +// Initialize the quotient +long long quotient = 0, temp = 0; + +// test down from the highest bit and +// accumulate the tentative value for +// valid bit +for (int i = 31; i >= 0; --i) { + + if (temp + (divisor << i) <= dividend) { + temp += divisor << i; + quotient |= 1LL << i; + } +} + +return sign * quotient; +} + +// Driver code +int main() { +int a = 10, b = 3; +cout << divide(a, b) << "\n"; + +a = 43, b = -8; +cout << divide(a, b); + +return 0; +} + diff --git a/BIT set/Find position of set bit.cpp b/BIT set/Find position of set bit.cpp new file mode 100644 index 0000000..26f5611 --- /dev/null +++ b/BIT set/Find position of set bit.cpp @@ -0,0 +1,43 @@ +// { Driver Code Starts +#include +using namespace std; + + // } Driver Code Ends +class Solution { + public: + int findPosition(int N) { + // code here + if(N==1) + return 1; + int res=log2(N); + float ans=log2(N); + if(ans!=res) + { + return -1; + } + int c=1; + while(N>0) + { + if(N%2==1) + return c; + N/=2; + c++; + } + return -1; + } +}; + +// { Driver Code Starts. +int main() { + int t; + cin >> t; + while (t--) { + int N; + + cin>>N; + + Solution ob; + cout << ob.findPosition(N) << endl; + } + return 0; +} // } Driver Code Ends diff --git a/BIT set/Non Repeating Numbers.cpp b/BIT set/Non Repeating Numbers.cpp new file mode 100644 index 0000000..f13b3e9 --- /dev/null +++ b/BIT set/Non Repeating Numbers.cpp @@ -0,0 +1,69 @@ +// { Driver Code Starts +#include +using namespace std; + + // } Driver Code Ends +class Solution +{ +public: + vector singleNumber(vector nums) + { + // Code here. + int x1=0,a,b; + vector v; + for(int i=0;i> T; + while(T--) + { + int n; + cin >> n; + vector v(2 * n + 2); + for(int i = 0; i < 2 * n + 2; i++) + cin >> v[i]; + Solution ob; + vector ans = ob.singleNumber(v); + for(auto i: ans) + cout << i << " "; + cout << "\n"; + } + return 0; +} // } Driver Code Ends