diff --git a/Week1/Swarnima_Shishodia/Que1.cpp b/Week1/Swarnima_Shishodia/Que1.cpp new file mode 100644 index 0000000..908206d --- /dev/null +++ b/Week1/Swarnima_Shishodia/Que1.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +int main() +{ + long long int n; + + cout<<"Enter a number "; + //Input a number + cin>>n; + //Output the square of a number + cout< +using namespace std; + +int gcd(int num1,int num2) +{ + if(num2==0) + return num1; + else + return gcd(num2,num1%num2); +} +int main() +{ + int num1,num2,result; + cin>>num1; + cin>>num2; + result=gcd(num1,num2); + cout< +using namespace std; + +void sieve_of_erasthones(int a[]) +{ +int p=10000,i,q=2,j; +for(i=0;i<=p;i++)//Mark all the array position as 1 + a[i]=1; +a[0]=0; +a[1]=0; +while(q*q<=p) //Mark the sieve positions as 0 which gets divided by q +{ + for(j=q*q;j<=p;j=j+q) + a[j]=0; + q=q+1; +} +//The numbers which are prime, 1 will be present +} +int main() { + int t,i,n,j; + cin>>t; + int a[10000]; + sieve_of_erasthones(a); //Call the sieve function + for(i=0;i>n; + for(j=0;j<=n;j++) + { + if(a[j]==1) + cout< +using namespace std; +void simpleSieve(int limit, vector &prime) +{ + bool mark[limit+1]; + memset(mark, true, sizeof(mark)); + for (int p=2; p*p prime; + simpleSieve(limit, prime); + int low = limit; + int high = 2*limit; + while (low < n) + { + if (high >= n) + high = n; + bool mark[limit+1]; + memset(mark, true, sizeof(mark)); + for (int i = 0; i < prime.size(); i++) + { + int loLim = floor(low/prime[i]) * prime[i]; + if (loLim < low) + loLim += prime[i]; + for (int j=loLim; j>n; + segmentedSieve(n); + return 0; +} diff --git a/Week1/Swarnima_Shishodia/Que13.cpp b/Week1/Swarnima_Shishodia/Que13.cpp new file mode 100644 index 0000000..ba44cc4 --- /dev/null +++ b/Week1/Swarnima_Shishodia/Que13.cpp @@ -0,0 +1,42 @@ +//To calculate the power of a number +#include +using namespace std; +int power_(int base,int power) +{ + int result=1; + while(power!=0) + { + if(power%2==0) + { + base=base*base; //If power is even then take square of the base and divide the power by 2 + power=power/2; + } + else + { + result=result*base; //If power is odd then multiply base by result + power=power-1; //and decrease the power by 1 + } + + } + return result; +} +int main() +{ + int base,power,m,y,i,p; + cin>>base; + cin>>power; + cin>>m; + p=power; + if(power%2==0) + power=power/2; //If power is even divide it by 2 + else + power=power-1; // If power is odd subtract it by 1 + + y=power_(base,power); + if(p%2==0) + i=(((y%m)*(y%m))%m);//Using the properties + else + i=(((base%m)*(y%m))%m); + cout< +using namespace std; +int main() +{ + int t,i,j,n,flag; + cin>>t; + for(i=0;i>n; + if(n==1) + cout<<"No"< +using namespace std; +void sieve_of_erasthones(int a[]) //Use sieve to calculate prime number +{ + int p=2,i; + for(i=0;i<401;i++) //Mark all array index as 1 + a[i]=1; +a[0]=0; +a[1]=0; + while(p*p<=400) + { + for(i=p*p;i<401;i=i+p) + a[i]=0; //The numbers which are not prime will be marked as 0 + p=p+1; + } +} +int main() +{ + int t,i,j,n,k; + cin>>t; + int a[401]; + + sieve_of_erasthones(a); //Call the sieve function + + for(i=0;i>n; + for(j=2;j<=n;j++) + { + for(k=2;k<=n;k++) + { + if(k*j<=n && a[k]==1 && a[j]==1) //To print the pairs + cout< +using namespace std; +int main() +{ + int t,i,j,a,b; + cin>>t; + uint64_t n; + if(n==0) + cout<<"NO"; + else + { + for(i=0;i>n; + if((n&(n-1))==0) //If a number and with number-1 is 0, it means that it can be represented as power of 2 + cout<<"YES"< +using namespace std; +int main() +{ + long long int n; + int i; + + cout<<"Enter a number "; + cin>>n; + + //Print the table + for(i=1;i<=10;i++) + cout< +using namespace std; +int main() +{ + int n,i,j; + cout<<"Enter a number "; + cin>>n; + + //Run first loop till n and inside that print the number of star for each line which is always equal to the i + + for(i=1;i<=n;i++) + { + for(j=1;j<=i;j++) + cout<<"*"; + cout< +using namespace std; +int main() +{ + int space, rows,i,k; + cout <<"Enter number of rows: "; + cin >> rows; + for(i=1;i<= rows;i++) + { + k=0; + //To print the space in left side + for(space=1;space<=rows-i;space++) + { + cout <<" "; + } + + //To print the stars + while(k != i) + { + cout << "* "; + ++k; + } + cout << endl; + } + return 0; +} diff --git a/Week1/Swarnima_Shishodia/Que4.cpp b/Week1/Swarnima_Shishodia/Que4.cpp new file mode 100644 index 0000000..bdf23c0 --- /dev/null +++ b/Week1/Swarnima_Shishodia/Que4.cpp @@ -0,0 +1,20 @@ +//To print Downward Star Triangle + +#include +using namespace std; +int main() +{ + int n,i,j; + cout<<"Enter a number "; + cin>>n; + //Run a loop from n to 1.Inside it run another loop which will print exactly i stars + for(i=n;i>=1;i--) + { + for(j=1;j<=i;j++) + cout<<"*"; + cout< +using namespace std; +int main() +{ + int space, rows,i,k; + cout <<"Enter number of rows: "; + cin >> rows; + //To print upward traingle + for(i=1;i<= rows;i++) + { + k=0; + //Print the spaces on left side + for(space=1;space<=rows-i;space++) + { + cout <<" "; + } + + //Print the stars + while(k != i) + { + cout << "* "; + ++k; + } + cout << endl; + } + + //To print Downward Triangle + for(i=1;i<=rows-1;i++) + { + k=0; + // To print spaces on left side + for(space=1;space<=i;space++) + { + cout <<" "; + } + + //To print stars in each row + while(k != rows-i) + { + cout << "* "; + ++k; + } + cout << endl; + } + + return 0; +} diff --git a/Week1/Swarnima_Shishodia/Que6.cpp b/Week1/Swarnima_Shishodia/Que6.cpp new file mode 100644 index 0000000..66b3a20 --- /dev/null +++ b/Week1/Swarnima_Shishodia/Que6.cpp @@ -0,0 +1,29 @@ +//To print the fibonacci Triangle +#include +using namespace std; +int main() +{ + //Take two variables a and b, initialize a as 0 and b as 1 + int i,j,a=0,b=1,c,n; + cin>>n; + for(i=1;i<=n;i++) + { + for(j=1;j<=i;j++) + { + + if(i==1) //To print first row + cout< +using namespace std; +int main() +{ + int i,j,n,space; + cin>>n; + for(i=1;i<=n;i++) + { + //to print leftside numbers + for(j=1;j<=i;j++) + cout<=1;j--) + cout< +using namespace std; +int main() +{ + int n,i,j,star=1; + cin>>n; + + for(i=0;i + +using namespace std; +int multiply(int x, int fact_size, int fact[]) //To miltiply the numbers to calculate factorial +{ + int carry=0; + for(int i=0;i=0;i--) + cout<>t; + for(i=0;i>n; + factorial(n); + } + return 0; +} diff --git a/Week1/testing.txt b/Week1/testing.txt new file mode 100644 index 0000000..a8f4558 --- /dev/null +++ b/Week1/testing.txt @@ -0,0 +1 @@ +This file is for testing perpose. diff --git a/Week2/Swarnima_Shishodia/Que1.cpp b/Week2/Swarnima_Shishodia/Que1.cpp new file mode 100644 index 0000000..e3b801b --- /dev/null +++ b/Week2/Swarnima_Shishodia/Que1.cpp @@ -0,0 +1,38 @@ +#include +#include +using namespace std; +int main() +{ + /*variable s store's the string input by the user + p stores the modified string + w stores the final string that we need to print*/ + +string s,p="",w=""; +cin>>s; +//concatenated '.' at left side of string so that last word can be concatenated when . appears +s="."+s; +//q stores the length of the string +//i is a loop variable +int q=s.size()-1,i; +//traversing the string from right side +for(i=s.size()-1;i>=0;i--) +{ + //When . appears the this part will get executed + if(s[i]=='.') + { + int j=i; + //all the characters to the right of the . get stored till next . appears + while(j<=q) + { + p=p+s[j]; + j=j+1; + } + q=i-1;// so that the size of remaining string is stored + } +} +// As the first character is a dot which I have concatenated so run a loop from 1 position +for(int i=1;i +using namespace std; +void allpermutation(string str) +{ + sort(str.begin(),str.end());//Sort the string lexicographically + while(next_permutation(str.begin(),str.end()))// nextt_permutation function prints all the permutations of a string + cout << str << endl; +} +int main() +{ + string str; + cin>>str;//Input the string + allpermutation(str);//Call the function + return 0; +} diff --git a/Week2/Swarnima_Shishodia/Que2I.cpp b/Week2/Swarnima_Shishodia/Que2I.cpp new file mode 100644 index 0000000..dbb9c4b --- /dev/null +++ b/Week2/Swarnima_Shishodia/Que2I.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; +int main() +{ +int t; + cin>>t; + while(t--) + { + string st; + cin>>st; + int s=4,b=1,c=0; + for(int i=0;i +using namespace std; +int main() +{ + string c,d; + cin>>c; + cin>>d; + //Make two arrays of length 26 mark all the position in both the arrays equal to 1 + int a[26],b[26]; + for(int i=0;i<26;i++) + { + a[i]=0; + b[i]=0; + } + int i,j=0; + //increase the array position by 1 when character appears, After loop gets over It will give each character numbers + //Suppose 'a' comes then its ASCII value is 97, 97-97=0 at 0th position 1 gets stored . + //In similar way 1st position represents 'b' as 98-97=1 + for(i=0;i +#include +#include +using namespace std; +int main() +{ + string s; + getline(cin,s);//to input a space separated string + int a[257]; + for(int i=0;i<257;i++) + a[i]=0; + +/* Make a array for 257 size, whenever a character comes, its ASCII value is calculated and 1 is stored at the same position in the array. +Print the character, and store 1 at that position when the same character will come reappear +then it will not enter into the if position as it already 1*/ + for(int i=0;i +using namespace std; +int main() + { + int t; + cin>>t; + while(t--) + { + int n; + cin>>n; + vector v; + //Store the digits of a number in vector + while(n!=0) + { + v.push_back(n%10); + n/=10; + } + //Sort the vector + sort(v.begin(), v.end()); + int len=v.size();//Store the length of a vector + bool found=false; + while(next_permutation(v.begin(), v.end()))// Using next_permutation find all the permutation of a number + { + int sum=0; + for(int j=0;j +using namespace std; +int main() { + int t,n,m,i,j,num1,num2; + cin>>t; + for(i=0;i>n; + int a[n]; + num1=0; + num2=0; + for(j=0;j>a[j]; + + cin>>m; + int b[m]; + for(j=0;j>b[j]; + + for(j=0;j +#include +//To find the contiguous subarray with maximum sum +//Decalre two variables max_so_far and max_ending_here +//max_so_far stores the maximum sum till now +//max_ending_here considers the place where max_so_far get decreased +using namespace std; +int main() +{ + int t,n; + cin>>t; + for(int i=0;i>n; + long long a[n]; + for(int j=0;j>a[j]; + int max_so_far=INT_MIN,max_ending_here=0; + for (int i=0;i +using namespace std; +//1. Reverse from 0 to d-1 +//2. Reverse from d to n-1 +//3. Reverse from 0 to n-1 +//4. Print the output +int main() { + int t,i,j,n,d; + cin>>t; + for(i=0;i>n; + cin>>d; + int a[n]; + for(j=0;j>a[j]; + j=0; + int k=d-1; + // Reverse from 0 to d-1 + while(j<=k) + { + int temp=a[j]; + a[j]=a[k]; + a[k]=temp; + j=j+1; + k=k-1; + } + j=d; + k=n-1; + //Reverse from d to n-1 + while(j<=k) + { + int temp=a[j]; + a[j]=a[k]; + a[k]=temp; + j=j+1; + k=k-1; + } + j=0; + k=n-1; + //Reverse the whole array + while(j<=k) + { + int temp=a[j]; + a[j]=a[k]; + a[k]=temp; + j=j+1; + k=k-1; + } + for(j=0;j +using namespace std; +//Logic - Traverse the string from right to left and print the final output +int main() { + int t,i,j; + cin>>t; + for(i=0;i>s; + for(j=s.length()-1;j>=0;j--) + cout< +using namespace std; +int main() +{ + int t,s,i,j,p,q; + long long int sum=0,n; + cin>>t; + for(i=0;i>n; + cin>>s; + long long int a[n]; + for(j=0;j>a[j]; + p=0; + q=0; + sum=0; + while(qs) + { + sum=sum-a[p]; + p=p+1; + } + if(sum==s) + break; + } + + if(sum==s) + cout< +using namespace std; +//In Liner search compare each and every element of array with the element you are looking for +//If element is found then set the flag variable value equal to 1 +// Print flag variable +int main() { + int i,j,n,x,f,t; + cin>>t; + for(i=0;i>n; + cin>>x; + int a[n]; + for(j=0;j>a[j]; + for(j=0;j +using namespace std; +//To sort the array having 0,1,2 +//Count all 0 ,1 and 2 +//First make the count of zero number's as 0 +//Make count of 1 as 1 in array and after that do same for second +int main() +{ + int t,n,zero,one,two; + cin>>t; + for(int i=0;i>n; + zero=0; + one=0; + two=0; + int a[n]; + for(int j=0;j>a[j]; + for(int j=0;j +using namespace std; +int main() +{ + int t,i,j,n,x; + cin>>t; //Input the number of test cases + for(i=0;i>n;//Input the number of elements + cin>>x;//Input the number which you want to search + int arr[n]; + for(j=0;j>arr[j]; + int low=0,high=n-1,f=-1; + //Calculate the middle element in the range low to high. + //Check whether it is equal to mid element,If yes set f=1 + // If 'x' is smaller than arr[mid], the make high=mid-1; because other elements are greater than it + //If 'x' is higher than arr[mid] ,them make low=mid+1 ,because all left elements are less than x + while(low<=high) + { + int mid=(low+high)/2; + if(arr[mid]==x) + { + f=1; + break; + } + else if(arr[mid] +using namespace std; +//In mirror inverse we just need to check if we make array element equal to index and then look for array element . +//If all the value matches i then , it is Mirror Inverse otherwise not +bool isMirrorInverse(int arr[], int n) +{ + for (int i = 0; i < n; i++) + { + if (arr[arr[i]] != i) + return false; + } + return true; +} +int main() +{ + int n; + cin>>n; + int arr[n]; + for(int i=0;i>arr[i]; + if (isMirrorInverse(arr,n)) + cout<<"Yes"; + else + cout<<"No"; + return 0; +} diff --git a/Week3/Que8Array.cpp b/Week3/Que8Array.cpp new file mode 100644 index 0000000..055e052 --- /dev/null +++ b/Week3/Que8Array.cpp @@ -0,0 +1,29 @@ +#include +//#include +//#includem; +using namespace std; +//To find the majority element store the count of each element +//Then check if cout is greater than n/2 print yes +//Otherwise print No +int main() +{ + int n; + cin>>n; + unordered_map m; + int arr[n]; + for(int i=0;i>arr[i]; + for(int i=0;in/2) + f=1; + } +if(f==1) + cout<<"YES"; +else + cout<<"NO"; +return 0; +} diff --git a/Week3/Question2MediumArray.cpp b/Week3/Question2MediumArray.cpp new file mode 100644 index 0000000..dc1e167 --- /dev/null +++ b/Week3/Question2MediumArray.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +#define SIZE 105 +void spirallyTraverse(int m, int n, int ar[SIZE][SIZE]); +int main() +{ + int T=0; + scanf("%d",&T); + while(T--) + { + int m,n; + scanf("%d",&m); + scanf("%d",&n); + int ar[SIZE][SIZE]={{0}}; + int i=0; + int j=0; + int row=0; + int col=0; + for(i=0;i=left;i--) + cout<=top;i--) + cout< +using namespace std; +#include +//To print an element which occur only once +//Store the count of each number in unordered map as key value pair +//Check if value is 1 then print key +int main() +{ + int t,n,k; + cin>>t; + for(int i=0;i>n; + cin>>k; + unordered_mapm; + int a[n]; + for(int j=0;j>a[j]; + for(int j=0;j +using namespace std; +//Using binary search we can do it in log(n) . +//Try to get the position of first 0 and then subtract length of array from first 0 position +int main() +{ + int t,n; + cin>>t; + for(int i=0;i>n; + int a[n]; + for(int j=0;j>a[j]; + + int low=0,high=n-1,f=0; + while(low<=high) + { + int mid=(low+high)/2; + if(mid==0 && a[mid]==0 || a[mid]==0 && a[mid-1]==1)//If mid==0 and at 0th position element is 0 then all the array have 0 + //If a[mid]==0 then if it is the first 0, a[mid-1] will be 1 + { + f=n-mid; + break; + } + else if(a[mid]==1)//If array mid==1 then surely left side of mid have all elements 1 , we need to make low=mid+1 + low=mid+1; + else + high=mid-1; //If it is not first 1 then it will be on left side that's why high=mid-1 + } + cout<