Skip to content

Commit a00328c

Browse files
committed
Programs Added
1 parent b26cdc3 commit a00328c

12 files changed

+512
-0
lines changed

#7 Reverse Integer.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//https://leetcode.com/problems/reverse-integer/
2+
class Solution {
3+
public int reverse(int x) {
4+
int rev = 0;
5+
while(x!=0)
6+
{
7+
int r = x%10;
8+
x=x/10;
9+
if(rev> Integer.MAX_VALUE/10 ||rev == Integer.MAX_VALUE/10 && r > 7)
10+
return 0;
11+
if(rev< Integer.MIN_VALUE/10 ||rev == Integer.MIN_VALUE/10 && r > 8)
12+
return 0;
13+
rev=rev*10+r;
14+
}
15+
return rev;
16+
}
17+
}

1184 Distance Between Bus Stops.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// https://leetcode.com/problems/distance-between-bus-stops/
2+
3+
class Solution {
4+
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
5+
int clockwise = 0, anticlockwise = 0;
6+
int i = start;
7+
8+
while(i != destination) {
9+
clockwise += distance[i];
10+
i = (i + 1)%distance.length;
11+
}
12+
13+
while(i != start) {
14+
anticlockwise += distance[i];
15+
i = (i + 1)%distance.length;
16+
}
17+
18+
return Math.min(anticlockwise, clockwise);
19+
}
20+
}

1539 Kth Missing Positive Number.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://leetcode.com/problems/kth-missing-positive-number/submissions/
2+
3+
class Solution {
4+
public int findKthPositive(int[] arr, int k) {
5+
if(k > arr[arr.length - 1] - arr.length) return k + arr.length;
6+
int x = 1;
7+
int i = 0;
8+
while(i < arr.length) {
9+
if(arr[i] != x) {
10+
k--;
11+
if(k == 0) return x;
12+
}
13+
else i++;
14+
x++;
15+
}
16+
return k + x - 1;
17+
}
18+
}

BinarySearch.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Scanner;
2+
3+
public class BinarySearch {
4+
public static void main(String[] args) {
5+
Scanner scan = new Scanner(System.in);
6+
System.out.println("Enter size of an array");
7+
int n = scan.nextInt();
8+
int[] a = new int[n];
9+
System.out.println("Enter array elements");
10+
for(int i=0;i<n;i++)
11+
a[i] = scan.nextInt();
12+
System.out.println("Enter Element to be Searched");
13+
int k = scan.nextInt();
14+
int low=0;
15+
int high=n-1;
16+
while(low<=high){
17+
int mid = (low+high)/2;
18+
if(a[mid]==k){
19+
System.out.println(k + " found at index " + mid);
20+
return;
21+
}
22+
if(k>a[mid])
23+
low=mid+1;
24+
else
25+
high=mid-1;
26+
}
27+
System.out.println("Element not found");
28+
}
29+
}

Doubly Linked List.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.*;
2+
class Node
3+
{
4+
int data;
5+
Node next;
6+
Node prev;
7+
}
8+
class DoublyLinkedList
9+
{
10+
Node head;
11+
public void insert (int data)
12+
{
13+
Node node = new Node ();
14+
node.data = data;
15+
if (head == null)
16+
{
17+
head = node;
18+
19+
}
20+
else
21+
{
22+
Node n = head;
23+
while (n.next != null)
24+
{
25+
n = n.next;
26+
}
27+
n.next = node;
28+
node = n.next;
29+
30+
}
31+
}
32+
public void show ()
33+
{
34+
Node n1 = head;
35+
while (n1.next != null)
36+
{
37+
System.out.println (n1.data);
38+
n1= n1.next;
39+
40+
}
41+
System.out.println (n1.data);
42+
43+
}
44+
}
45+
46+
public class Main
47+
{
48+
public static void main (String[]args)
49+
{
50+
DoublyLinkedList l = new DoublyLinkedList ();
51+
l.insert (2);
52+
l.insert (3);
53+
l.insert (4);
54+
l.show();
55+
56+
}
57+
}

PasswordValidation.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.*;
2+
class MyException extends Exception{
3+
4+
MyException(String msg){
5+
super(msg);
6+
}
7+
}
8+
class PasswordValidation{
9+
public static void main(String [] args){
10+
Scanner sc=new Scanner(System.in);
11+
System.out.println("Password must contain one digit, one uppercase character, one lowercase character and length should be greater than equal to 8");
12+
System.out.println("Enter password : ");
13+
String y=sc.nextLine();
14+
int ctd=0,ctu=0,ctl=0;
15+
try{
16+
if(y.length()<8)
17+
throw new MyException("Too short password!");
18+
for(int i=0;i<y.length();i++){
19+
if(y.charAt(i)>='0'&&y.charAt(i)<='9'){
20+
ctd++;
21+
}
22+
if(y.charAt(i)>='A'&&y.charAt(i)<='Z')
23+
ctu++;
24+
if(y.charAt(i)>='a'&&y.charAt(i)<='z')
25+
ctl++;
26+
}
27+
if(ctd==0&&ctl==0&&ctu==0)
28+
throw new MyException("No Digit , no uppercase and no lower case!");
29+
else if(ctd==0&&ctl==0)
30+
throw new MyException("No Digit and no lowercase!");
31+
else if(ctl==0&&ctu==0)
32+
throw new MyException("No lowercase and no uppercase!");
33+
else if(ctd==0&&ctu==0)
34+
throw new MyException("No digit and no uppercase!");
35+
else if(ctd==0)
36+
throw new MyException("No Digit!");
37+
else if(ctu==0)
38+
throw new MyException("No Uppercase character!");
39+
else if(ctl==0)
40+
throw new MyException("No Uppercase character!");
41+
System.out.println("correct password");
42+
}
43+
catch(MyException e){
44+
System.out.println(e.getMessage());
45+
}
46+
}}

Rotate_Array.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//https://leetcode.com/problems/rotate-array/
2+
class Solution {
3+
public void rotate(int[] nums, int k) {
4+
for(int i=0;i<k;i++){
5+
int temp = nums[nums.length-1];
6+
int j = nums.length-2;
7+
while(j>=0){
8+
nums[j+1] = nums[j];
9+
j--;
10+
}
11+
nums[0] = temp;
12+
}
13+
}
14+
}

SortLinkedList.java

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/******************************************************************************
2+
3+
Welcome to GDB Online.
4+
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
5+
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
6+
Code, Compile, Run and Debug online from anywhere in world.
7+
8+
*******************************************************************************/
9+
class LinkedList
10+
{
11+
Node head;
12+
13+
public LinkedList()
14+
{
15+
this.head=null;
16+
}
17+
18+
void insertAtEnd(int data)
19+
{
20+
Node newNode=new Node(data);
21+
if(head==null)
22+
{
23+
head=newNode;
24+
}
25+
else
26+
{
27+
Node current=head;
28+
while(current.next!=null)
29+
{
30+
current=current.next;
31+
}
32+
current.next=newNode;
33+
}
34+
}
35+
void insertAtStart(int data)
36+
{
37+
Node newNode=new Node(data);
38+
if(head==null)
39+
{
40+
head=newNode;
41+
}
42+
else
43+
{
44+
newNode.next=head;
45+
head=newNode;
46+
}
47+
}
48+
49+
void traverse()
50+
{
51+
Node n=head;
52+
while(n!=null)
53+
{
54+
System.out.print(n.data+" -> ");
55+
n=n.next;
56+
}
57+
}
58+
59+
void sortList()
60+
{
61+
int swap=0;
62+
Node n=head;
63+
Node s1=head;
64+
Node s2=head;
65+
while(s1!=null)
66+
{
67+
s2=head;
68+
Node tmp=head;
69+
Node tmp2=null;
70+
while(s2.next!=null)
71+
{
72+
if(s2.data>s2.next.data)
73+
{
74+
swap++;
75+
if(s2==head)
76+
{
77+
head=s2.next;
78+
}
79+
tmp2=s2.next.next;
80+
s2.next.next=s2;
81+
tmp.next=s2.next;
82+
s2.next=tmp2;
83+
84+
}
85+
tmp=s2;
86+
s2=s2.next;
87+
if(s2==null)
88+
{
89+
break;
90+
}
91+
}
92+
s1=s1.next;
93+
}
94+
System.out.println("Swap " + swap);
95+
}
96+
}
97+
class Node
98+
{
99+
int data;
100+
Node next;
101+
102+
public Node(int data)
103+
{
104+
this.data=data;
105+
this.next=null;
106+
}
107+
108+
}
109+
public class Main
110+
{
111+
public static void main(String[] args) {
112+
LinkedList l1=new LinkedList();
113+
l1.insertAtEnd(5);
114+
l1.insertAtEnd(2);
115+
l1.insertAtEnd(3);
116+
l1.insertAtEnd(1);
117+
l1.insertAtEnd(12);
118+
l1.insertAtEnd(-1);
119+
l1.insertAtEnd(14);
120+
l1.insertAtEnd(15);
121+
l1.insertAtEnd(16);
122+
l1.insertAtEnd(-5);
123+
l1.insertAtEnd(6);
124+
l1.insertAtEnd(7);
125+
l1.insertAtEnd(8);
126+
l1.insertAtEnd(9);
127+
l1.insertAtEnd(10);
128+
l1.insertAtEnd(-11);
129+
l1.insertAtEnd(17);
130+
l1.insertAtEnd(-18);
131+
l1.insertAtEnd(0);
132+
l1.insertAtEnd(-222);
133+
l1.traverse();
134+
System.out.println();
135+
l1.sortList();
136+
l1.traverse();
137+
System.out.println();
138+
139+
}
140+
}
141+

0 commit comments

Comments
 (0)