Skip to content

Commit baf8ed3

Browse files
Added hacker rank java basic code
1 parent fa1bb67 commit baf8ed3

20 files changed

+401
-116
lines changed

.DS_Store

-14 KB
Binary file not shown.

.gitignore

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
*
33
!*.*
44
!*/
5-
.vscode
65
.DS_Store
6+
._.DS_Store
7+
**/.DS_Store
8+
**/._.DS_Store
9+
.vscode
10+
*.class
711
testCPP/*
812
Coding-ninjas-competitive
913
Competitive-Coding
@@ -13,3 +17,11 @@ comptetive-programming_interview-resource
1317
coding template by other
1418
low level/Online_Examination_CPP
1519
low level/Online_Examination_CPP
20+
hackerRank-java/out/
21+
hackerRank-java/.cph/
22+
hackerRank-java/.idea
23+
leetcode/array/.cph/
24+
hello.dSYM/Contents/Info.plist
25+
hackerRank-java/hackerRank-jav a.iml
26+
leetcode/array/save.bin
27+
.cph

Untitled-1.bin

39.1 KB
Binary file not shown.

Untitled-1.cpp

Lines changed: 18 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,24 @@
1-
#include <bits/stdc++.h>
1+
#include <iostream>
22
using namespace std;
3-
const int MAX_N = 3e5 + 6;
43

5-
struct Node {
6-
Node* child[2];
7-
Node() {
8-
this -> child[0] = this -> child[1] = NULL;
9-
}
10-
};
11-
void insert(Node* root, int x) {
12-
Node* cur = root;
13-
for(int bit = 21; bit >= 0; bit--) {
14-
int p = (x & (1 << bit)) ? 1 : 0;
15-
if(!cur -> child[p]) {
16-
cur -> child[p] = new Node();
4+
void fizzBuzz(int n) {
5+
for (int i = 1; i <= n; ++i) {
6+
if (i % 3 == 0 && i % 5 == 0) {
7+
cout << "FizzBuzz" << endl;
8+
} else if (i % 3 == 0) {
9+
cout << "Fizz" << endl;
10+
} else if (i % 5 == 0) {
11+
cout << "Buzz" << endl;
12+
} else {
13+
cout << i << endl;
14+
}
1715
}
18-
cur = cur -> child[p];
19-
}
20-
}
21-
int xor_max_query(Node* root, int x) {
22-
int ans = 0;
23-
Node* cur = root;
24-
for(int bit = 21; bit >= 0; bit--) {
25-
int p = (x & (1 << bit)) ? 1 : 0;
26-
if(cur -> child[1^p]) {
27-
ans += (1 << bit);
28-
cur = cur -> child[1^p];
29-
} else if(cur -> child[p]) {
30-
cur = cur -> child[p];
31-
} else {
32-
break;
33-
}
34-
}
35-
return ans;
3616
}
3717

38-
// vector<Node*> tree;
39-
Node* tree[4 * MAX_N];
40-
void merge(Node* &root, Node* root1, Node* root2) {
41-
if(!root1 && !root2)
42-
return;
43-
root = new Node();
44-
if(!root1) {
45-
root = root2;
46-
return;
47-
}
48-
if(!root2) {
49-
root = root1;
50-
return;
51-
}
52-
merge(root -> child[0], root1 -> child[0], root2 -> child[0]);
53-
merge(root -> child[1], root1 -> child[1], root2 -> child[1]);
54-
}
55-
void build(int id, int l, int r, vector<int>& a) {
56-
if(l == r) {
57-
tree[id] = new Node();
58-
insert(tree[id], a[l]);
59-
return;
60-
}
61-
int mid = (l + r) / 2;
62-
build(2 * id, l, mid, a);
63-
build(2 * id + 1, mid + 1, r, a);
64-
merge(tree[id], tree[2 * id], tree[2 * id + 1]);
65-
}
66-
int query(int id, int l, int r, int lq, int rq, int x) {
67-
if(lq > r || l > rq)
68-
return 0;
69-
if(lq <= l && r <= rq)
70-
return xor_max_query(tree[id], x);
71-
int mid = (l + r) / 2;
72-
int on_left = query(2 * id, l, mid, lq, rq, x);
73-
int on_right = query(2 * id + 1, mid + 1, r, lq, rq, x);
74-
return max(on_left, on_right);
75-
}
7618
int main() {
77-
ios::sync_with_stdio(false);
78-
cin.tie(0);
79-
int n;
80-
cin >> n;
81-
vector<int> a(n);
82-
for(int i = 0; i < n; i++)
83-
cin >> a[i];
84-
build(1, 0, n - 1, a);
85-
int Q;
86-
cin >> Q;
87-
for(int q = 0; q < Q; q++) {
88-
int l, r, x;
89-
cin >> l >> r >> x;
90-
l -= 1;
91-
r -= 1;
92-
cout << query(1, 0, n - 1, l, r, x) << '\n';
93-
}
94-
return 0;
95-
}
19+
int n;
20+
// cout << "Enter the upper limit: ";
21+
cin >> n;
22+
fizzBuzz(n);
23+
return 0;
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.*;
2+
public class JAVA_4_multiLine {
3+
public static void main(String args[]){
4+
Scanner sc = new Scanner(System.in);
5+
int index=1;
6+
while(sc.hasNext()){
7+
String s = sc.nextLine();
8+
9+
s=Integer.toString(index)+" "+s;
10+
index+=1;
11+
System.out.println(s);
12+
}
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Java_1_BasicJava {
2+
3+
public static void main(String[] args) {
4+
System.out.println("Hello, World.");
5+
System.out.println("Hello, Java.");
6+
}
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import java.util.*;
2+
class Java_2_InputJava {
3+
public static void main(String args[]){
4+
Scanner sc = new Scanner(System.in);
5+
int a = sc.nextInt();
6+
int b = sc.nextInt();
7+
int c=sc.nextInt();
8+
System.out.println(a);
9+
System.out.println(b);
10+
System.out.println(c);
11+
}
12+
}

hackerRank-java/Java_3_ifElse.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.*;
2+
3+
public class Java_3_ifElse{
4+
public static void main(String args[]){
5+
Scanner sc = new Scanner(System.in);
6+
7+
int n = sc.nextInt();
8+
9+
if(n%2==1){
10+
System.out.println("Weird");
11+
}else{
12+
if(n>=2 && n<=5){
13+
System.out.println("Not Weird");
14+
}else if (n>=6 && n<=20){
15+
System.out.println("Weird");
16+
}else {
17+
System.out.println("Not Weird");
18+
}
19+
}
20+
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
public class Java_5_try_catch {
3+
public static void main(String args[]){
4+
try{
5+
Scanner sc = new Scanner(System.in);
6+
int a = sc.nextInt();
7+
int b = sc.nextInt();
8+
9+
if(a>=0 && b>=0){
10+
int area = (a*b);
11+
System.out.println(area);
12+
}
13+
else{
14+
throw new Exception("Breadth and height must be positive");
15+
}
16+
}catch(Exception e){
17+
System.out.println(e);
18+
}
19+
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.Calendar;
2+
3+
public class Java_6_Calendar {
4+
enum Days{
5+
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
6+
}
7+
public static void main(String args[]){
8+
Calendar c = Calendar.getInstance();
9+
c.set(2024,11,5);
10+
// System.out.println(c.getTime());
11+
int x = c.get(Calendar.DAY_OF_WEEK);
12+
String Day[]={"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
13+
System.out.println(Day[x-1]);
14+
}
15+
}

0 commit comments

Comments
 (0)