Skip to content

Week3 Level Easy Problems #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: Week1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Week2/Swarnima_Shishodia/Que1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include<iostream>
#include<string.h>
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<p.size();i++)
w=w+p[i];//concatenate the characters in w and print them
cout<<w<<endl;
return 0;
}
Binary file added Week2/Swarnima_Shishodia/Que1.o
Binary file not shown.
15 changes: 15 additions & 0 deletions Week2/Swarnima_Shishodia/Que2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <bits/stdc++.h>
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;
}
36 changes: 36 additions & 0 deletions Week2/Swarnima_Shishodia/Que2I.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
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<st.size();i++)
{
if(st[i]=='W')
{
s=s+b;
b=1;
}
else
{
s=s-b;
b=b*2;
if(s<b&&i!=st.size()-1)
{
c=1;
break;
}
}
}
if(c==1)
cout<<-1<<endl;
else
cout<<s<<endl;
}
return 0;
}
37 changes: 37 additions & 0 deletions Week2/Swarnima_Shishodia/Que3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<iostream>
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<c.size();i++)
a[int(c[i])-97]=a[int(c[i])-97]+1;

for(i=0;i<d.size();i++)
b[int(d[i])-97]=b[int(d[i])-97]+1;

//Check whether array a and b are equal if true then print yes a otherwise print no
for(i=0;i<26;i++)
{
if(a[i]==b[i])
j=j+1;
}
if(j==26)
cout<<"Yes\n";
else
cout<<"No\n";

return 0;
}
27 changes: 27 additions & 0 deletions Week2/Swarnima_Shishodia/Que4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include<iostream>
#include<algorithm>
#include<string.h>
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<s.size();i++)
{
if(a[int(s[i])]==0)
{
cout<<s[i];
a[int(s[i])]=1;
}
if(s[i]==' ')
cout<<' ';
}
return 0;
}
42 changes: 42 additions & 0 deletions Week2/Swarnima_Shishodia/Que4I.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<int> 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<len;j++)
{
sum=sum*10+v[j];
}//Form the number
if(sum%8 == 0)//If it is divisible by 8 then print found
{
found = true;
break;
}
}
//Print Yes if any permutation is divisible by 8 else print no
if(found)
cout<<"Yes\n";
else
cout<<"No\n";
}
return 0;
}
30 changes: 30 additions & 0 deletions Week3/Que1Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
using namespace std;
int main() {
int t,n,m,i,j,num1,num2;
cin>>t;
for(i=0;i<t;i++)
{
cin>>n;
int a[n];
num1=0;
num2=0;
for(j=0;j<n;j++)//to input first array
cin>>a[j];

cin>>m;
int b[m];
for(j=0;j<m;j++)//to input second array
cin>>b[j];

for(j=0;j<n;j++)
num1=a[j]+num1*10;//to form number from 1st array take digit and multiply the number with 10 everytime


for(j=0;j<m;j++)
num2=b[j]+num2*10;//to form number from 2nd array take each digit and multiply the number by 10 and add it to number

cout<<(num1+num2)<<endl;
}
return 0;
}
30 changes: 30 additions & 0 deletions Week3/Que1MediumArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include<climits>
//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<t;i++)
{
cin>>n;
long long a[n];
for(int j=0;j<n;j++)
cin>>a[j];
int max_so_far=INT_MIN,max_ending_here=0;
for (int i=0;i<n;i++)
{
max_ending_here = max_ending_here+a[i];
if (max_so_far < max_ending_here)// If the current sum is greater then make max_so_far equal to curent sum value
max_so_far = max_ending_here;

if (max_ending_here < 0)
max_ending_here = 0;
}
cout<<max_so_far<<endl;
}
}
56 changes: 56 additions & 0 deletions Week3/Que2Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
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<t;i++)
{
cin>>n;
cin>>d;
int a[n];
for(j=0;j<n;j++)
cin>>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<n;j++)
cout<<a[j]<<" ";
cout<<endl;

}
return 0;
}
16 changes: 16 additions & 0 deletions Week3/Que3Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
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<t;i++)
{
string s;
cin>>s;
for(j=s.length()-1;j>=0;j--)
cout<<s[j];
cout<<endl;
}
return 0;
}
44 changes: 44 additions & 0 deletions Week3/Que4Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Use sliding window technique
// Keep on adding the elements if sum of elements is smaller than um we are looking for
// Subtract the leftmost element if sum of elements is greater than sum we are looking for
// If equal print it, Maintain the variable for left variable
#include<iostream>
using namespace std;
int main()
{
int t,s,i,j,p,q;
long long int sum=0,n;
cin>>t;
for(i=0;i<t;i++)
{
cin>>n;
cin>>s;
long long int a[n];
for(j=0;j<n;j++)
cin>>a[j];
p=0;
q=0;
sum=0;
while(q<n)
{
while(sum<s)
{
sum=sum+a[q];
q=q+1;
}
while(sum>s)
{
sum=sum-a[p];
p=p+1;
}
if(sum==s)
break;
}

if(sum==s)
cout<<p+1<<" "<<q<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}
Loading