DSA Learning Series : Basics
This post contains 9 simple problems which can be found here at Codechef.
Python :
while(True):
n=int(input())
if(n!=42):
print(n)
else:
break
C++ :
#include <bits/stdc++.h>
#define ll long long int
#define endl "\n"
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
while(true)
{
ll num;
cin >> num;
if(num == 42)
break;
cout << num << endl;
}
return 0;
}
OR
#include <iostream>
using namespace std;
int main() {
// your code goes here
int input;
while(cin>>input)
{
if(input==42)
break;
else
cout<<input<<'\n';
}
return 0;
}
Python :
try:
k=int(input())
for i in range(k):
l=input()
print(int(l[::-1]))
except EOFError:
pass
OR
t=int(input())
while t>0:
l=input()
print(int(l[::-1]))
t-=1
C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t,a,sum=0,r;
cin>>t;
for(int i=0;i<t;i++)
{
sum=0;
cin>>a;
while(a!=0)
{
r=a%10;
sum=sum*10+r;
a=a/10;
}
cout<<sum<<endl;
}
return 0;
}
OR
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define per(i,a,b) for (int i=b-1;i>=a;i--)
#define pb push_back
#define endl '\n'
#define all(x) x.begin(), x.end()
#define pf push_font
typedef long long ll;
typedef vector<int> vi;
const ll mod=1000000007;
int _;
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> _;
while(_--){
string x;
cin >> x;
reverse(all(x));
//cout << x << endl;
int a;
rep(i,0,x.size()){
if(x[i] != '0'){
a = i;break;
}
}
rep(i,a,x.size()){
cout <<x[i];
}
cout << endl;
}
return 0;
}
Python :
t = int(input())
while t>0:
l=input()
lis=[]
for j in l:
lis.append(j)
n=len(lis)
y=n//2
lis1=[]
lis2=[]
for i in range(y):
k=i
lis1.append(lis[k])
lis2.append(lis[n-k-1])
lis1.sort()
lis2.sort()
if lis1==lis2:
print("YES")
else:
print("NO")
t-=1
OR
t=int(input())
for i in range(t):
a=input()
if (len(a)%2==0):
x=a[:len(a)//2]
y=a[len(a)//2:]
else:
x=a[:len(a)//2]
y=a[len(a)//2+1:]
for i in x:
if x.count(i)==y.count(i):
ans=("YES")
else:
ans=("NO")
break
print(ans)
OR
from collections import Counter
def checkTwoHalves(input):
length = len(input)
if (length % 2 != 0):
first = input[0:length // 2]
second = input[(length // 2) + 1:]
else:
first = input[0:length // 2]
second = input[length // 2:]
if Counter(first) == Counter(second):
print ('YES')
else:
print ('NO')
t = int(input())
for i in range(t):
s = input()
checkTwoHalves(s)
OR
for i in range(int(input())):
s = input()
l = len(s) # len(s) odd aabaa l = 5 // 2 + 1, s[:2] s[3:]
f = s[:l//2]
l = s[l//2:] if l%2 == 0 else s[(l//2)+1:]
fCount = dict()
lCount = dict()
for i in f:
fCount[i] = fCount.get(i, 0) + 1
for i in l:
lCount[i] = lCount.get(i, 0) + 1
if set(l) == set(f):
for i in fCount.keys():
if fCount[i] != lCount[i]:
print('NO')
break
else:
print('YES')
else:
print('NO')
C++ :
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define per(i,a,b) for (int i=b-1;i>=a;i--)
#define pb push_back
#define endl '\n'
#define all(x) x.begin(), x.end()
#define pf push_font
typedef long long ll;
typedef vector<int> vi;
const ll mod=1000000007;
int _;
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> _;
while(_--){
string x;
cin >> x;
int n = x.size();
if(n%2==0){
string n1,n2;
n1 = x.substr(0,n/2);
n2 = x.substr(n/2,n/2);
sort(all(n1));
sort(all(n2));
//cout << n1 << ' ' << n2 << endl;
if(n1 == n2) cout << "YES" << endl;
else cout << "NO" << endl;
}
else{
string n1,n2;
n1=x.substr(0,n/2);
n2 = x.substr(n/2+1,n/2);
sort(all(n1));
sort(all(n2));
//cout << n1 << ' ' << n2 << endl;
if(n1 == n2) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
return 0;
}
OR
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
vector<char> arr;
vector<char> arr1;
int flag=0;
int index;
if(s.size()%2)
{
index=(s.size()/2)+1;
}
else
{
index=s.size()/2;
}
for(int i=0;i<s.size()/2;i++)
{
arr.push_back(s[i]);
}
sort(arr.begin(),arr.end());
for(int i=index;i<s.size();i++)
{
arr1.push_back(s[i]);
}
sort(arr1.begin(),arr1.end());
for(int i=0;i<s.size()/2;i++)
{
if(arr[i]==arr1[i])
{
continue;
}
else
{
cout<<"NO"<<endl;
flag=1;
break;
}
}
if(flag==0)
cout<<"YES"<<endl;
}
return 0;
}OR
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <climits>
#include <math.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t > 0)
{
string s;
cin >> s;
int n = s.length();
string p;
if (n % 2 == 0)
{
p = s.substr(n / 2, n / 2);
s.erase(n / 2, n / 2);
}
else
{
p = s.substr((n / 2) + 1, n / 2);
s.erase(n / 2, (n / 2) + 1);
}
sort(s.begin(), s.end());
sort(p.begin(), p.end());
int count = 0;
if (s== p)
{
cout << "YES" << endl;
}
else
{
cout<<"NO"<<endl;
}
t--;
}
return 0;
}Smart Phone
Python :
arr=[]
n=int(input())
for i in range(n):
arr.append(int(input()))
arr.sort()
max1=0
for i in range(n):
rev=arr[i]*(n-i)
if(rev>max1):
max1=rev
print(max1)OR
n = int(input())
budget = []
for i in range(0,n):
x = int(input())
budget.append(x)
budget.sort()
bud = tuple(budget)
max_budget = []
for j in range(0,len(bud)):
max_budget.append((len(budget))*bud[j])
budget.remove(bud[j])
max_budget.sort()
print(max_budget[len(max_budget)-1])C++ :
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define round(n) fixed << setprecision(n)
#define pb push_back
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
vector<int> p(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
}
sort(p.begin(), p.end());
int maxP = 0;
for (int i = 0; i < n; i++) {
maxP = max(maxP, p[i] * (n - i));
}
cout << maxP;
return 0;
}
OR
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
int main()
{
int n;
cin >> n;
vector<int> A(n);
for (int i = 0; i < n; i++)
cin >> A[i];
sort(A.begin(), A.end());
ll result = 0;
for (int i = 0; i < n; i++)
result = max(result, (n - i) * 1ll * A[i]);
cout << result << endl;
}
Python:
for _ in range(int(input())):
n=int(input())
arr=[int(x) for x in input().split()]
count=0
max_speed=10**18
for i in arr:
if(i<=max_speed):
count+=1
max_speed=i
print(count)OR
t=int(input())
for i in range(t):
c=1
n=int(input())
l=[int(i) for i in input().split()]
l1=l[0]
if n==1:
print("1")
elif n>1:
for i in range(1,len(l)):
if l[i]<=l1:
l1=l[i]
c=c+1
print(c) C++:
#include <iostream> using namespace std; int main(void) { // your code goes here int t; cin>>t; while(t--) { int n,c=1; cin>>n; int a[n]; for(int i=0;i<n;i++) cin>>a[i]; int max=a[0]; for(int i=1;i<n;i++) { if(max>a[i]) { max=a[i]; c++; } } cout<<c<<endl; } return 0; }
