Today was my first day into my daily Data Structures and Algorithms (DSA) series and below is my progress for today.
I went through the fifth video in Babar's DSA Playlist. You can check out the video here: Babar's DSA Playlist - Video 5. It covered several fundamental concepts.
Firstly, I revisited the basics of bitwise operators. Although I've used them before, it felt beneficial to refresh my understanding. Then, I went deeper into increment and decrement operations. Below is the code that I used just for the clarification:
#include<iostream>
using namespace std;
int main(){
int a = 4;
int b = 6;
//Bitwise Operators
cout<<"a&b "<< (a&b) << endl;
cout<<"a|b "<< (a|b) << endl;
cout<<"~a "<< (~a) << endl;
cout<<"a^b "<< (a^b) << endl;
//Left shift
cout<<"19<<1 "<<(19<<1)<< endl;
cout<<"19<<2 "<<(19<<2)<< endl;
//Right shift
cout<<"19>>1 "<<(19>>1)<< endl;
cout<<"19>>2 "<<(19>>2)<< endl;
int i = 12;
//Pre increment ++i (i will change to 13)
cout<<(++i)<<endl;
//Post increment i++ (i is 13 and will change to 14 but print 13)
cout<<(i++)<<endl;
//Pre decrement --i (i is 14 and will change to 13)
cout<<(--i)<<endl;
//Pre decrement i-- (i is 13 will change to 12 but print 13)
cout<<(i--)<<endl;
}
After that, I focused on the concept of 'for' loops. I knew how to use them, but it was great to solve a few basic problems like finding Fibonacci numbers, calculating sums, checking for prime numbers, etc. Below are the code snippets for the questions.
//Fibonacci
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int a = 0;
int b = 1;
cout<<a<<" "<<b<<" ";
for(int i=1;i<=n;i++){
int nextNum= a+b;
cout<<nextNum<<" ";
a = b;
b = nextNum;
}
}
//One to n sum
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter a number "<<endl;
cin>>n;
int sum = 0;
for(int i=1;i<=n;i++){
sum+=i;
}
cout<<"The sum of 1 to "<<n<<" is "<<sum<<endl;
}
//Prime or Not Prime
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
bool isPrime = 1;
for(int i = 2;i<n;i++){
if(n%i == 0){
isPrime=0;
break;
}
}
if(isPrime==0){
cout<<"It is a Non prime Number";
}else{
cout<<"It is a Prime Number";
}
}
Later, I learned about 'break' and 'continue' statements along with variable scoping. and then ended the day by attempting two problems on LeetCode:
Subtract the Product and Sum of Digits of an Integer I successfully solved this one independently without needing to refer to any external resources.
class Solution { public: int subtractProductAndSum(int n) { int a = n; int sum=0,product=1; while(a!=0){ int digit=a%10; product*=digit; sum+=digit; a=a/10; } return product-sum; } };
Number of 1 Bits I almost completed this by myself but needed a little help towards the end.
class Solution { public: int hammingWeight(uint32_t n) { int count = 0; while(n!=0){ if(n&1){ count++; } n=n>>1; } return count; } };
If you're interested, you can check out my LeetCode profile here to see the problems I've been working on.
That wraps up my progress for today! Tomorrow, I wish to complete another video from the series and solve more problems.
Cheers,
Jay Chavan.