Skip to content

Commit 8a6ace7

Browse files
committed
java practice at hackerrank started
1 parent ae54ee1 commit 8a6ace7

10 files changed

+537
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
Institute: National Institute of Technology, Uttarakhand
5+
*/
6+
#include <bits/stdc++.h>
7+
#include <ext/pb_ds/assoc_container.hpp>
8+
#include <ext/pb_ds/tree_policy.hpp>
9+
using namespace std;
10+
using namespace __gnu_pbds;
11+
typedef long long ll ;
12+
typedef vector<ll> vl;
13+
typedef vector<vector<ll>> vvl;
14+
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
15+
/* Abbrevations */
16+
#define ff first
17+
#define ss second
18+
#define mp make_pair
19+
#define line cout<<endl;
20+
#define pb push_back
21+
// Some print
22+
#define no cout<<"NO"<<endl;
23+
#define yes cout<<"YES"<<endl;
24+
// sort
25+
#define all(V) (V).begin(),(V).end()
26+
#define srt(V) sort(all(V))
27+
#define srtGreat(V) sort(all(V),greater<ll>())
28+
// some extra
29+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line;
30+
#define precision(x) cout<<fixed<<setprecision(x);
31+
#define sz(V) ll(V.size())
32+
// datatype definination
33+
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
34+
35+
36+
class Codeforces{
37+
private:
38+
// read only variable
39+
const ll INF=1e18;
40+
const ll mod1=1e9+7;
41+
const ll mod2=998244353;
42+
43+
44+
public:
45+
Codeforces(){
46+
47+
}
48+
49+
ll power(ll x,ll y){
50+
ll result=1;
51+
while(y>0){
52+
if(y&1){
53+
result*=x;
54+
}
55+
y>>=1;
56+
x*=x;
57+
}
58+
return result;
59+
}
60+
61+
ll power(ll x,ll y,ll mod){
62+
ll result=1;
63+
x%=mod;
64+
while(y>0){
65+
if(y&1){
66+
result*=x;
67+
result%=mod;
68+
}
69+
y>>=1;
70+
x*=x;
71+
x%=mod;
72+
}
73+
return result;
74+
}
75+
76+
ll str_to_num(string s){
77+
stringstream pk(s);
78+
ll num;
79+
pk>>num;
80+
return num;
81+
}
82+
83+
string num_to_str(ll num){
84+
return to_string(num);
85+
}
86+
// Techniques :
87+
// divide into cases, brute force, pattern finding
88+
// sort, greedy, binary search, two pointer
89+
// transform into graph
90+
91+
// Experience :
92+
// Cp is nothing but only observation and mathematics.
93+
ll solve(){
94+
int n;
95+
cin>>n;
96+
if(n<=2){
97+
cout<<n<<endl;
98+
}
99+
else{
100+
string s1="";
101+
for(int i=0;i<1000;i++){
102+
if(i&1){
103+
s1+='1';
104+
}
105+
else{
106+
s1+='2';
107+
}
108+
}
109+
int sum=0;
110+
for(int i=0;i<s1.size();i++){
111+
sum+=(s1[i]-'0');
112+
if(sum==n){
113+
string ans=s1.substr(0,i+1);
114+
cout<<ans<<endl;
115+
return 0;
116+
}
117+
else if(sum>n){
118+
break;
119+
}
120+
}
121+
sum=0;
122+
for(int i=1;i<sz(s1);i++){
123+
sum+=(s1[i]-'0');
124+
if(sum==n){
125+
string ans=s1.substr(1,i);
126+
cout<<ans<<endl;
127+
return 0;
128+
}
129+
else if(sum>n){
130+
break;
131+
}
132+
}
133+
line;
134+
}
135+
return 0;
136+
}
137+
};
138+
139+
140+
/* --------------------MAIN PROGRAM----------------------------*/
141+
142+
int main()
143+
{
144+
speed;
145+
/* #ifndef ONLINE_JUDGE
146+
freopen("input.txt","r",stdin);
147+
freopen("output.txt","w",stdout);
148+
#endif */
149+
ll TestCase=1;
150+
cin>>TestCase;
151+
while(TestCase--){
152+
Codeforces cf;
153+
cf.solve();
154+
}
155+
}
156+
/* -----------------END OF PROGRAM --------------------*/
157+
/*
158+
* stuff you should look before submission
159+
* constraint and time limit
160+
* int overflow
161+
* special test case (n=0||n=1||n=2)
162+
* don't get stuck on one approach if you get wrong answer
163+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.*;
2+
import java.text.*;
3+
import java.text.DecimalFormat;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
public class Solution {
7+
// use java 7 or java 8
8+
// this code will give error in java 15
9+
10+
public static void main(String[] args) {
11+
Scanner scanner = new Scanner(System.in);
12+
double payment = scanner.nextDouble();
13+
scanner.close();
14+
15+
Locale indiaLocale = new Locale("en", "IN");
16+
NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
17+
NumberFormat india = NumberFormat.getCurrencyInstance(indiaLocale);
18+
NumberFormat china = NumberFormat.getCurrencyInstance(Locale.CHINA);
19+
NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);
20+
21+
System.out.println("US: "+ us.format(payment));
22+
System.out.println("India: "+india.format(payment));
23+
System.out.println("China: " + china.format(payment));
24+
System.out.println("France: " + france.format(payment));
25+
}
26+
}

HackerRank/Java/Java_If-Else.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.io.*;
2+
import java.math.*;
3+
import java.security.*;
4+
import java.text.*;
5+
import java.util.*;
6+
import java.util.concurrent.*;
7+
import java.util.regex.*;
8+
9+
public class Solution {
10+
11+
12+
13+
private static final Scanner scanner = new Scanner(System.in);
14+
15+
public static void main(String[] args) {
16+
int N = scanner.nextInt();
17+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
18+
19+
if(N%2==1 || (N>=6 && N <= 20)){
20+
System.out.println("Weird");
21+
}
22+
else{
23+
System.out.println("Not Weird");
24+
}
25+
scanner.close();
26+
}
27+
}

HackerRank/Java/Java_Loops.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.io.*;
2+
import java.math.*;
3+
import java.security.*;
4+
import java.text.*;
5+
import java.util.*;
6+
import java.util.concurrent.*;
7+
import java.util.regex.*;
8+
9+
10+
11+
public class Solution {
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
14+
15+
int N = Integer.parseInt(bufferedReader.readLine().trim());
16+
17+
bufferedReader.close();
18+
for(int i=1;i<=10;i++){
19+
System.out.println(N+" x "+i+" = "+N*i);
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.Scanner;
2+
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
Scanner sc=new Scanner(System.in);
7+
System.out.println("================================");
8+
for(int i=0;i<3;i++){
9+
String s1=sc.next();
10+
int x=sc.nextInt();
11+
//Complete this line
12+
System.out.printf("%-15s%03d",s1,x);
13+
System.out.printf("\n");
14+
}
15+
System.out.println("================================");
16+
17+
}
18+
}
19+
20+
21+
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
Scanner scan = new Scanner(System.in);
7+
int a = scan.nextInt();
8+
int b = scan.nextInt();
9+
int c = scan.nextInt();
10+
// Complete this line
11+
// Complete this line
12+
13+
System.out.println(a);
14+
System.out.println(b);
15+
System.out.println(c);
16+
// Complete this line
17+
// Complete this line
18+
}
19+
}
20+

HackerRank/Java/Welcome_to_Java!.java

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class Solution {
2+
3+
public static void main(String[] args) {
4+
/* Enter your code here. Print output to STDOUT. Your class should be named Solution. */
5+
System.out.println("Hello, World.");
6+
System.out.println("Hello, Java.");
7+
}
8+
}
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
Institute: National Institute of Technology, Uttarakhand
5+
*/
6+
typedef long long ll ;
7+
typedef vector<int> vl;
8+
typedef vector<vector<int>> vvl;
9+
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
10+
/* Abbrevations */
11+
#define ff first
12+
#define ss second
13+
#define mp make_pair
14+
#define pb push_back
15+
// Some print
16+
#define no cout<<"NO"<<endl;
17+
#define yes cout<<"YES"<<endl;
18+
// sort
19+
#define all(V) (V).begin(),(V).end()
20+
#define srt(V) sort(all(V))
21+
#define srtGreat(V) sort(all(V),greater<ll>())
22+
// some extra
23+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} cout<<endl;
24+
#define precision(x) cout<<fixed<<setprecision(x);
25+
#define sz(V) ll(V.size())
26+
27+
28+
/* ascii value
29+
A=65,Z=90,a=97,z=122
30+
*/
31+
32+
/* Some syntax
33+
//Syntax to create a min heap for priority queue
34+
//priority_queue <int, vector<int>, greater<int>>pq;
35+
*/
36+
/* --------------------MAIN PROGRAM----------------------------*/
37+
// to run ctrl+b
38+
const ll INF=1e18;
39+
const ll mod1=1e9+7;
40+
const ll mod2=998244353;
41+
// Techniques :
42+
// divide into cases, brute force, pattern finding
43+
// sort, greedy, binary search, two pointer
44+
// transform into graph
45+
46+
// Experience :
47+
// Cp is nothing but only observation and mathematics.
48+
49+
50+
//Add main code here
51+
52+
class Solution {
53+
public:
54+
string minRemoveToMakeValid(string s) {
55+
string ans="";
56+
stack<pair<char,int>> st;
57+
for(int i=0;i<s.length();i++){
58+
if(s[i]>='a'&&s[i]<='z'){
59+
continue;
60+
}
61+
else{
62+
if(s[i]=='('){
63+
st.push({s[i],i});
64+
}
65+
else{
66+
if(st.empty()){
67+
s[i]=',';
68+
}
69+
else if(st.top().ff=='('){
70+
st.pop();
71+
}
72+
else{
73+
s[i]=',';
74+
}
75+
}
76+
}
77+
}
78+
while(!st.empty()){
79+
s[st.top().ss]=',';
80+
st.pop();
81+
}
82+
for(auto x:s){
83+
if(x!=','){
84+
ans+=x;
85+
}
86+
}
87+
return ans;
88+
}
89+
};
90+
91+
92+
/* -----------------END OF PROGRAM --------------------*/
93+
/*
94+
* stuff you should look before submission
95+
* constraint and time limit
96+
* int overflow
97+
* special test case (n=0||n=1||n=2)
98+
* don't get stuck on one approach if you get wrong answer
99+
*/

0 commit comments

Comments
 (0)