Skip to content

Commit 72675e2

Browse files
committed
#Modification 55
1 parent 3881954 commit 72675e2

17 files changed

Lines changed: 275 additions & 92 deletions

linkedList/Problem_2.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package linkedList;
2+
3+
// Problem Title => Reverse a Linked List in group of Given Size. [Very Imp]
4+
public class Problem_2 {
5+
6+
public static void main(String[] args) {
7+
8+
}
9+
}

linkedList/Problem_3.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.HashSet;
44

5+
// Problem Title => Write a program to Detect loop in a linked list.
56
public class Problem_3 {
67

78
static Node head;

linkedList/Problem_4.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package linkedList;
2+
3+
// Problem Title => Write a program to Delete loop in a linked list.
4+
public class Problem_4 {
5+
6+
public static void main(String[] args) {
7+
8+
}
9+
}

list/Problem_1_1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Node reverse (Node node) {
3636

3737
void printList(Node node) {
3838
while(node != null) {
39-
System.out.println(node.data + " ");
39+
System.out.println(node.data + " ");
4040
node = node.next;
4141
}
4242
}

ssp/SSP_Problem_03.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package ssp;
2+
3+
import java.util.Scanner;
4+
5+
// Problem Title => Search in a rotated sorted array
6+
public class SSP_Problem_03 {
7+
8+
public static String countAndSay(int n){
9+
if(n == 1) return "1";
10+
if(n == 2) return "11";
11+
12+
StringBuilder str = new StringBuilder("11");
13+
for(int i = 3; i <= n; i++){
14+
str.append('$');
15+
int len = str.length();
16+
int cnt = 1;
17+
StringBuilder temp = new StringBuilder();
18+
char[] a = str.toString().toCharArray();
19+
for (int j = 1; j < len; j++)
20+
{
21+
// If current character
22+
// does't match
23+
if (a[j] != a[j - 1])
24+
{
25+
// Append count of
26+
// str[j-1] to temp
27+
temp.append(cnt);
28+
29+
// Append str[j-1]
30+
temp.append(a[j - 1]);
31+
32+
// Reset count
33+
cnt = 1;
34+
}
35+
36+
// If matches, then increment
37+
// count of matching characters
38+
else cnt++;
39+
}
40+
41+
// Update str
42+
str = new StringBuilder(temp.toString());
43+
}
44+
45+
return str.toString();
46+
47+
}
48+
public static void main(String[] args) {
49+
Scanner sc = new Scanner(System.in);
50+
int n = sc.nextInt();
51+
int[] a = new int[n];
52+
for(int i = 0; i < n; i++){
53+
a[i] = sc.nextInt();
54+
}
55+
int N = 3;
56+
System.out.println(countAndSay(N));
57+
}
58+
}

ssp/SSP_Problem_04_1.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ssp;
2+
3+
import java.util.Scanner;
4+
5+
// Problem Title => square root of an integer
6+
public class SSP_Problem_04_1 {
7+
8+
static int floorSqrt(int x){
9+
// base case
10+
if(x == 0 || x == 1)
11+
return x;
12+
13+
// maintaining counter's
14+
int i = 1, result = 1;
15+
while(result <= x){
16+
i++;
17+
result = i*i;
18+
}
19+
return i-1;
20+
}
21+
22+
public static void main(String[] args) {
23+
Scanner sc = new Scanner(System.in);
24+
int x = sc.nextInt();
25+
System.out.print(floorSqrt(x));
26+
}
27+
}
28+
29+
// Time Complexity => O(^n) [It means root n].
30+
// Space Complexity => O(1) [Constant].

ssp/SSP_Problem_04_2.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ssp;
2+
3+
import java.util.Scanner;
4+
5+
// Problem Title => square root of an integer
6+
public class SSP_Problem_04_2 {
7+
8+
static int floorSqrt(int x){
9+
// base case
10+
if(x == 0 || x == 1)
11+
return x;
12+
13+
// Do Binary Search for floor(sqrt(x))
14+
long start = 1, end = x, ans = 0;
15+
while(start <= end){
16+
long mid = (start + end) / 2;
17+
// checking if that is the square root of middle number
18+
if(mid*mid == x)
19+
return (int)mid;
20+
21+
// checking if that is smaller than the square root of middle number
22+
if(mid*mid < x){
23+
start = mid+1;
24+
ans = mid;
25+
}
26+
27+
else
28+
end = mid-1;
29+
}
30+
return (int)ans;
31+
}
32+
33+
public static void main(String[] args) {
34+
Scanner sc = new Scanner(System.in);
35+
int x = sc.nextInt();
36+
System.out.print(floorSqrt(x));
37+
}
38+
}
39+
40+
// Time Complexity => O(log n) [It means Log n Time will be taken].
41+
// Space Complexity => O(1) [Constant Time will be taken].

ssp/SSP_Problem_05.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ssp;
2+
3+
import java.util.Scanner;
4+
5+
// Problem Title => Maximum and minimum of an array using minimum number of comparisons
6+
public class SSP_Problem_05 {
7+
8+
static int floorSqrt(int x){
9+
// base case
10+
if(x == 0 || x == 1)
11+
return x;
12+
13+
// Do Binary Search for floor(sqrt(x))
14+
long start = 1, end = x, ans = 0;
15+
while(start <= end){
16+
long mid = (start + end) / 2;
17+
// checking if that is the square root of middle number
18+
if(mid*mid == x)
19+
return (int)mid;
20+
21+
// checking if that is smaller than the square root of middle number
22+
if(mid*mid < x){
23+
start = mid+1;
24+
ans = mid;
25+
}
26+
27+
else
28+
end = mid-1;
29+
}
30+
return (int)ans;
31+
}
32+
33+
public static void main(String[] args) {
34+
Scanner sc = new Scanner(System.in);
35+
int x = sc.nextInt();
36+
System.out.print(floorSqrt(x));
37+
}
38+
}
39+
40+
// Time Complexity => O(log n) [It means Log n Time will be taken].
41+
// Space Complexity => O(1) [Constant Time will be taken].

stack/P1.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package stack;
2+
// Problem Title => Implement Stack from Scratch
3+
4+
class Stack{
5+
static final int MAX = 1000;
6+
int top;
7+
int[] a = new int[MAX];
8+
9+
boolean isEmpty(){
10+
return (top < 0);
11+
}
12+
13+
Stack(){
14+
top = -1;
15+
}
16+
17+
boolean push(int x){
18+
if(top >= MAX-1){
19+
System.out.println("Stack Overflow");
20+
return false;
21+
}
22+
else {
23+
a[++top] = x;
24+
System.out.println(x + " pushed into stack");
25+
return true;
26+
}
27+
}
28+
29+
int pop(){
30+
if(top < 0){
31+
System.out.println("Stack Underflow");
32+
return 0;
33+
}
34+
else {
35+
return a[top--];
36+
}
37+
}
38+
39+
int peek(){
40+
if(top < 0){
41+
System.out.println("Stack Underflow");
42+
return 0;
43+
}
44+
else {
45+
return a[top];
46+
}
47+
}
48+
}
49+
50+
public class P1 {
51+
public static void main(String[] args) {
52+
Stack s = new Stack();
53+
s.push(10);
54+
s.push(20);
55+
s.push(30);
56+
System.out.println(s.pop() + " Popped from stack");
57+
s.peek();
58+
s.isEmpty();
59+
}
60+
}

stack_and_queue/P1_i.java

Whitespace-only changes.

0 commit comments

Comments
 (0)