C++ Sol.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/// <<------------------------------------------------------------------------------>>
void init()
{
cin.tie(0);
cin.sync_with_stdio(0);
cout.tie(0);
// Reading From File
freopen("Shower.in","r",stdin);
}
/// <<----;------------------------------------------------------------------------->>
int main()
{
init();
ll n, T;
cin >> n >> T;
ll t[n];
for(int i = 0;i < n; i++)
cin >>t[i];
ll cnt = T;
ll pos = T;
for(int i = 1 ;i <n ;i++)
{
if(t[i] < pos)
{
cnt -= (pos - t[i]);
}
cnt += T;
pos = t[i] + T;
}
cout << cnt;
}
Java Sol.
import java.io.File;
import java.util.Scanner;
public class AIC {
public static void main(String[]args) throws Exception {
// Reading from file
File f =new File("Shower.in");
Scanner input=new Scanner(f);
int n, T;
n = input.nextInt();
T = input.nextInt();
int t[] = new int [n];
for(int i = 0;i < n ;i++)
{
t[i] = input.nextInt();
}
long cnt = T;
long pos = T;
for(int i = 1 ;i <n ;i++)
{
if(t[i] < pos)
{
cnt -= (pos - t[i]);
}
cnt += T;
pos = t[i] + T;
}
System.out.print(cnt);
}
}
C++ Sol.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
/// <<------------------------------------------------------------------------------>>
void init()
{
cin.tie(0);
cin.sync_with_stdio(0);
cout.tie(0);
//Reading From File
freopen("Cinema.in","r",stdin);
}
/// <<----;------------------------------------------------------------------------->>
int main()
{
init();
int x,y,z;
cin >> x >> y >> z;
int l = y + z;
int n = (x - z) / l;
cout << n;
}
Java Sol.
import java.io.File;
import java.util.Scanner;
public class AIC {
public static void main(String[]args) throws Exception {
// Reading from file
File f =new File("Cinema.in ");
Scanner input=new Scanner(f);
int x, y, z;
x = input.nextInt();
y = input.nextInt();
z = input.nextInt();
int l = y + z;
int n = (x - z) / l;
System.out.print(n);
}
}
C++ Sol.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void init()
{
cin.tie(0);
cin.sync_with_stdio(0);
cout.tie(0);
freopen("square.in","r",stdin);
}
/// <<----;------------------------------------------------------------------------->>
int main()
{
init();
int n;
cin >> n;
for(int i = 31622 ;i >= 1; i--) // i = sqrt(1e9)
{
if(i * i <= n)
{
cout << i*i;
return 0;
}
}
}
Java Sol.
import java.io.File;
import java.util.Scanner;
public class AIC {
public static void main(String[]args) throws Exception {
// Reading from file
File f =new File("square.in");
Scanner input=new Scanner(f);
int n = input.nextInt();
for(int i = 31622 ;i >= 1; i--) // i = sqrt(1e9)
{
if(i * i <= n)
{
System.out.print(i*i);
break;
}
}
}
}
C++ Sol.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void init()
{
cin.tie(0);
cin.sync_with_stdio(0);
cout.tie(0);
freopen("Boxes.in","r",stdin);
}
/// <<----;------------------------------------------------------------------------->>
int main()
{
init();
string x;
cin >> x;
stack<int>st;
int n = x.size();
for(int i=0;i < n; i++)
{
if(st.empty())
st.push(x[i]);
else if(st.top() == x[i])
st.push(x[i]);
else st.pop();
}
cout << n - st.size();
}
Java Sol.
import java.io.File;
import java.util.*;
import java.io.*;
import java.util.Stack;
public class AIC {
public static void main(String[]args) throws Exception {
// Reading from file
File f =new File("Boxes.in");
Scanner input=new Scanner(f);
String x = input.nextLine();
int n = x.length();
Stack st = new Stack();
for(int i=0;i < n; i++)
{
if(st.empty())
st.add(x.charAt(i));
else if((char)st.peek() == x.charAt(i))
st.push(x.charAt(i));
else st.pop();
}
System.out.println(n - st.size());
}
}
C++ Sol.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/// <<------------------------------------------------------------------------------>>
void init()
{
cin.tie(0);
cin.sync_with_stdio(0);
cout.tie(0);
freopen("tiles.in","r",stdin);
}
/// <<----;------------------------------------------------------------------------->>
int main()
{
init();
string x;
cin >> x;
int n = x.size();
string x1 = x;
int cnt1 = 0 , cnt2 = 0;
for(int i = 1 ;i < n ;i++)
{
if(x1[i] == x1[i - 1])
{
cnt1++;
if(x1[i] == '0') x1[i] = '1';
else x1[i] = '0';
}
}
cnt2 = 1;
if(x[0] == '1') x[0] = '0';
else x[0] = '1';
for(int i = 1 ;i < n ;i++)
{
if(x[i] == x[i - 1])
{
cnt2++;
if(x[i] == '0') x[i] = '1';
else x[i] = '0';
}
}
cout << min(cnt1,cnt2);
}
C++ Sol.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/// <<------------------------------------------------------------------------------>>
void init()
{
cin.tie(0);
cin.sync_with_stdio(0);
cout.tie(0);
freopen("misbaha.in","r",stdin);
}
/// <<----;------------------------------------------------------------------------->>
int main()
{
init();
int n;
cin >> n;
int Freq[int(1e5+6)] = {0};
for(int i = 1 ;i < n;i++)
{
int u,v;
cin >> u >> v;
Freq[u]++;
Freq[v]++;
}
int cnt = 0;
for(int i = 1;i <= n;i++)
{
if(Freq[i] == 1)
cnt++;
}
cout << cnt;
}
C++ Sol.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/// <<------------------------------------------------------------------------------>>
void init()
{
cin.tie(0);
cin.sync_with_stdio(0);
cout.tie(0);
freopen("teams.in","r",stdin);
}
int mx;
string rst;
/// <<----;------------------------------------------------------------------------->>
int main()
{
init();
int n;
cin >> n;
set<string>st;
map<string,int>mp;
for(int i = 0;i < 3*n; i++)
{
string ipt;
cin >> ipt;
st.insert(ipt);
mp[ipt]++;
}
for(string x :st)
{
if(mp[x] > mx)
{
mx = mp[x];
rst= x;
}
}
cout << rst;
}
C++ Sol.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define precision(n) cout << fixed << setprecision(n)
/// <<------------------------------------------------------------------------------>>
void init()
{
cin.tie(0);
cin.sync_with_stdio(0);
cout.tie(0);
freopen("cell.in","r",stdin);
}
/// <<----;------------------------------------------------------------------------->>
int main()
{
init();
int t;
cin >> t;
while(t--)
{
int A,B, cnt = 1, T = 0;
cin >> A >> B;
while(cnt != A)
{
cnt *= B;
T++;
}
precision(14) << T << endl;
}
}
C++ Sol.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
/// <<------------------------------------------------------------------------------>>
void init()
{
cin.tie(0);
cin.sync_with_stdio(0);
cout.tie(0);
//Reading From File
freopen("geometry.in","r",stdin);
}
/// <<----;------------------------------------------------------------------------->>
int main()
{
init();
long double L ;
cin >> L ;
long double pi=acos(-1) ;
long double redius = L/2;
long double Area_Of_one_semi_circle = 0.5 * pi * redius * redius ;
long double Area_of_Parabola = Area_Of_one_semi_circle - (redius * redius);
long double Required_Area = .5*L*L - Area_of_Parabola ;
long double perimeter_of_semi_circle = pi * redius ;
long double Required_perimeter = L + L + perimeter_of_semi_circle ;
cout << fixed << setprecision(6) << Required_Area << " " << Required_perimeter ;
return 0;
}
Hint
Three pointers ...