Skip to content

Commit 3e9fc9d

Browse files
palindrome problem added
1 parent 4780c29 commit 3e9fc9d

37 files changed

+1283
-0
lines changed

AVLTree.java

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package BinaryTrees;
2+
3+
public class AVL {
4+
int count = 5;
5+
public class Node{
6+
private int val;
7+
private int height;
8+
Node left;
9+
Node right;
10+
public Node(int val){
11+
this.val = val;
12+
}
13+
public int getVal(){
14+
return val;
15+
}
16+
}
17+
public Node root;
18+
public int height(Node node){
19+
if(node==null){
20+
return -1;
21+
}
22+
return node.height;
23+
}
24+
public boolean isEmpty(){
25+
return root==null;
26+
}
27+
public void insert(int val){
28+
root = insert(root,val);
29+
}
30+
public Node insert(Node node,int val){
31+
if(node==null){
32+
node = new Node(val);
33+
return node;
34+
}
35+
if(node.val>val){
36+
node.left = insert(node.left,val);
37+
}
38+
if(node.val<val) {
39+
node.right = insert(node.right, val);
40+
}
41+
node.height = Math.max(height(node.left),height(node.right))+1;
42+
return rotate(node);
43+
}
44+
private Node rotate(Node node){
45+
if(height(node.left)-height(node.right)>1){
46+
//left heavy
47+
if(height(node.left.left)-height(node.left.right)>0){
48+
return rightRotate(node);
49+
}
50+
if(height(node.left.left)-height(node.left.right)<0){
51+
node.left = leftRotate(node.left);
52+
return rightRotate(node);
53+
}
54+
}
55+
if((height(node.left) - height(node.right)) < -1){
56+
//right heavy
57+
if(height(node.right.left)-height(node.right.right)<0){
58+
return leftRotate(node);
59+
}
60+
if(height(node.right.left)-height(node.right.right)>0){
61+
node.right = rightRotate(node.right);
62+
return leftRotate(node);
63+
}
64+
}
65+
return node;
66+
}
67+
public Node rightRotate(Node p){
68+
Node c = p.left;
69+
Node t = c.right;
70+
c.right = p;
71+
p.left = t;
72+
73+
p.height = Math.max(height(p.left),height(p.right))+1;
74+
c.height = Math.max(height(c.left),height(c.right))+1;
75+
76+
return c;
77+
}
78+
public Node leftRotate(Node p){
79+
Node c = p.right;
80+
Node t = c.left;
81+
c.left = p;
82+
p.right = t;
83+
84+
p.height = Math.max(height(p.left),height(p.right))+1;
85+
c.height = Math.max(height(c.left),height(c.right))+1;
86+
return c;
87+
}
88+
public void display(){
89+
display2D(root,0);
90+
}
91+
public void display2D(Node root, int space){
92+
if(root==null){
93+
return;
94+
}
95+
space+=count;
96+
display2D(root.right,space);
97+
System.out.println();
98+
for(int i=count;i<space;i++){
99+
System.out.print(" ");
100+
}
101+
System.out.println(root.val);
102+
display2D(root.left,space);
103+
}
104+
public void populateSorted(int[] nums){
105+
populateSorted(nums,0,nums.length);
106+
}
107+
public void populateSorted(int[] nums,int s,int e){
108+
if(s>=e){
109+
return ;
110+
}
111+
int mid = (s+e)/2;
112+
this.insert(nums[mid]);
113+
populateSorted(nums,s,mid);
114+
populateSorted(nums,mid+1,e);
115+
}
116+
public boolean isBalanced(Node node){
117+
if(node==null){
118+
return true;
119+
}
120+
return Math.abs(height(node.left)-height(node.right))<=1 && isBalanced(node.left) && isBalanced(node.right);
121+
}
122+
}

Armstrong.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
class Armstrong
3+
{
4+
int arm(int n)
5+
{
6+
int c,s=0;
7+
while(n>0)
8+
{
9+
c=n%10;
10+
s=s+(int)(Math.pow(c,3));
11+
n=n/10;
12+
}
13+
return s;
14+
}
15+
void range(int l,int u)
16+
{
17+
int i;
18+
for(i=l;i<=u;i++)
19+
{
20+
if(i==arm(i))
21+
System.out.println(i+" ");
22+
}
23+
}
24+
public static void main()
25+
{
26+
Scanner sc=new Scanner(System.in);
27+
Armstrong ob=new Armstrong();
28+
int lo,up;
29+
System.out.println("Enter the lower and the upper limits");
30+
lo=sc.nextInt();
31+
up=sc.nextInt();
32+
ob.range(lo,up);
33+
}
34+
}
35+
36+
37+
38+

BinarySearch.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class BinarySearch {
2+
3+
// Method to perform binary search iteratively
4+
public static int binarySearch(int[] arr, int target) {
5+
int left = 0;
6+
int right = arr.length - 1;
7+
8+
while (left <= right) {
9+
int mid = left + (right - left) / 2; // To avoid overflow
10+
11+
// Check if target is present at mid
12+
if (arr[mid] == target) {
13+
return mid;
14+
}
15+
// If target is greater, ignore the left half
16+
else if (arr[mid] < target) {
17+
left = mid + 1;
18+
}
19+
// If target is smaller, ignore the right half
20+
else {
21+
right = mid - 1;
22+
}
23+
}
24+
25+
// Target was not found in the array
26+
return -1;
27+
}
28+
29+
public static void main(String[] args) {
30+
int[] arr = {2, 3, 4, 10, 40};
31+
int target = 10;
32+
33+
int result = binarySearch(arr, target);
34+
if (result == -1) {
35+
System.out.println("Element not present in array");
36+
} else {
37+
System.out.println("Element found at index " + result);
38+
}
39+
}
40+
}

Bubble_Sort.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Bubble_Sort{
2+
3+
public static void printArray(int[] arr){
4+
for(int i=0;i<arr.length;i++){
5+
System.out.print(arr[i]+" ");
6+
}
7+
}
8+
9+
public static void main(String[] args) {
10+
int arr[]={7,8,3,2,1};
11+
//bubble sort
12+
for(int i=0;i<arr.length-1;i++){
13+
for(int j=0;j<arr.length-1-i;j++){
14+
if (arr[j]>arr[j+1]){
15+
int temp=arr[j];
16+
arr[j]=arr[j+1];
17+
arr[j+1]=temp;
18+
19+
}
20+
}
21+
}
22+
23+
printArray(arr);
24+
25+
}
26+
}

Emirp.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
class Emirp
3+
{
4+
public static void main()
5+
{
6+
Scanner sc=new Scanner(System.in);
7+
int n,c=0,c2=0,r=0,i,c1;
8+
System.out.println("Enter no.");
9+
n=sc.nextInt();
10+
11+
for(i=1;i<=n;i++)
12+
{
13+
if(n%i==0)
14+
c++;
15+
}
16+
while(n>0)
17+
{
18+
c1=n%10;
19+
r=r*10+c1;
20+
n=n/10;
21+
}
22+
for(i=1;i<=r;i++)
23+
{
24+
if(r%i==0)
25+
c2++;
26+
}
27+
if(c==2 && c2==2)
28+
System.out.println("Yes Emirp");
29+
else
30+
System.out.println("Not Emirp");
31+
}
32+
}
33+

Fabo.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Fabo {
2+
public static int fibonacciRecursion(int n,int[]arr){
3+
4+
/* if(n == 0){
5+
return 0;
6+
}
7+
if(n == 1 || n == 2){
8+
return 1;*/
9+
10+
if(n<=1){
11+
return n;
12+
}
13+
if(arr[n]!=0){
14+
return arr[n];
15+
}
16+
17+
18+
int ans=fibonacciRecursion(n-2,arr) + fibonacciRecursion(n-1,arr);
19+
return arr[n]=ans;
20+
21+
}
22+
23+
public static void main(String args[]) {
24+
int maxNumber = 6;
25+
System.out.print("Fibonacci Series of " + maxNumber + " numbers: ");
26+
int []arr=new int[maxNumber+1];
27+
int ans=fibonacciRecursion(maxNumber,arr);
28+
System.out.println(ans);
29+
30+
}
31+
}

Insertion_Sort.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Arrays;
2+
3+
public class Insertion_Sort {
4+
5+
public static void main(String[] args) {
6+
int[] arr={4,10,6,8,2,9};
7+
insertsort(arr);
8+
System.out.println(Arrays.toString(arr));
9+
10+
}
11+
static void insertsort(int[] arr){
12+
for (int i = 1; i < arr.length; i++) {
13+
int j=i;
14+
while (j>0&&arr[j-1]>arr[j]){
15+
int temp=arr[j];
16+
arr[j]=arr[j-1];
17+
arr[j-1]=temp;
18+
j--;
19+
}
20+
}
21+
}
22+
}
23+
24+

Kaden_Algo.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Kaden_Algo {
2+
3+
public static void main(String[] args) {
4+
int[] arr= { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
5+
System.out.println(Kadane(arr));
6+
}
7+
public static int Kadane(int[] arr) {
8+
int ans = Integer.MIN_VALUE;
9+
int prevsum = 0;
10+
for (int i = 0; i < arr.length; i++) {
11+
prevsum += arr[i];
12+
ans = Math.max(ans, prevsum);
13+
14+
if (prevsum < 0) {
15+
prevsum = 0;
16+
}
17+
}
18+
return ans;
19+
20+
}
21+
}

LL_BinaryTree.java

Whitespace-only changes.

Leetcode_1813.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Leetcode_1813 {
2+
public boolean areSentencesSimilar(String sentence1, String sentence2) {
3+
String[] s1Words = sentence1.split(" ");
4+
String[] s2Words = sentence2.split(" ");
5+
int n1 = s1Words.length;
6+
int n2 = s2Words.length;
7+
int left = 0, r1 = n1 - 1, r2 = n2 - 1;
8+
9+
while (s1Words[left].equals(s2Words[left])) {
10+
left++;
11+
if ((left == n1) || (left == n2)) return true;
12+
}
13+
14+
while (s1Words[r1].equals(s2Words[r2])) {
15+
if ((r1 == left) || (r2 == left)) return true;
16+
r1--;
17+
r2--;
18+
}
19+
return false;
20+
}
21+
public static void main(String[] args) {
22+
Leetcode_1813 obj = new Leetcode_1813();
23+
System.out.println(obj.areSentencesSimilar("My name is Haley", "My Haley"));
24+
}
25+
}

Leetcode_1963.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Leetcode_1963 {
2+
public int minSwaps(String s) {
3+
int size = 0;
4+
for (int i = 0; i < s.length(); i++) {
5+
char ch = s.charAt(i);
6+
if (ch == '[')
7+
size++;
8+
else if (size > 0)
9+
size--;
10+
}
11+
return (size + 1) / 2;
12+
}
13+
public static void main(String[] args) {
14+
Leetcode_1963 obj = new Leetcode_1963();
15+
System.out.println(obj.minSwaps("][]["));
16+
}
17+
18+
}

0 commit comments

Comments
 (0)