Skip to content

Commit 34947e0

Browse files
committed
More Solutions Added
1 parent 8ed9e6b commit 34947e0

13 files changed

+469
-0
lines changed

Array/Matrix Interchange.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
void interchange(int r,int c)
2+
{
3+
//Your code here
4+
int temp = 0;
5+
for(int i=0;i<r;i++) {
6+
temp = arr[i][0];
7+
arr[i][0] = arr[i][c - 1];
8+
arr[i][c - 1] = temp;
9+
}
10+
}

Array/Maximum in Struct Array.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
class Height
4+
{
5+
int feet;
6+
int inches;
7+
8+
public Height(int ft, int inc)
9+
{
10+
feet = ft;
11+
inches = inc;
12+
}
13+
}
14+
class maximum
15+
{
16+
public static void main (String[] args) {
17+
Scanner sc = new Scanner(System.in);
18+
int t = sc.nextInt();
19+
20+
while(t-- > 0)
21+
{
22+
int n = sc.nextInt();
23+
Height arr[] = new Height[n];
24+
for(int i = 0; i < n; i++)
25+
{
26+
int temp1 = sc.nextInt();
27+
int temp2 = sc.nextInt();
28+
arr[i] = new Height(temp1, temp2);
29+
30+
}
31+
GfG gfg = new GfG();
32+
33+
int res = gfg.findMax(arr, n);
34+
System.out.println(res);
35+
}
36+
}
37+
}
38+
39+
}
40+
41+
/*Please note that it's Function problem i.e.
42+
you need to write your solution in the form of Function(s) only.
43+
Driver Code to call/invoke your function is mentioned above.*/
44+
45+
/*
46+
Class of the element of the array is as
47+
class Height {
48+
int feet;
49+
int inches;
50+
public Height(int ft, int inc)
51+
{
52+
feet = ft;
53+
inches = inc;
54+
}
55+
}
56+
*/
57+
// function must return the maximum Height
58+
// return the height in inches
59+
class GfG
60+
{
61+
public static int findMax(Height arr[], int n)
62+
{
63+
int max = 0;
64+
int sum = 0;
65+
for (int i = 0; i < n; i++) {
66+
sum = arr[i].feet * 12 + arr[i].inches;
67+
if (sum > max) {
68+
max = sum;
69+
}
70+
}
71+
return max;
72+
}
73+
}

Array/Missing Number in Array.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
class GFG
5+
{
6+
public static void main (String[] args)
7+
{
8+
Scanner sc = new Scanner(System.in);
9+
int t = sc.nextInt();
10+
while (t-- != 0) {
11+
int n = sc.nextInt();
12+
int a[] = new int[n - 1];
13+
int sum = 0;
14+
for (int i = 0; i < n - 1; i++) {
15+
a[i] = sc.nextInt();
16+
sum += a[i];
17+
}
18+
int hyp = (n * (n + 1)) / 2;
19+
System.out.println(hyp - sum);
20+
}
21+
}
22+
}

Array/Next Permutation.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
class GFG
5+
{
6+
public static void main (String[] args)
7+
{
8+
Scanner sc = new Scanner(System.in);
9+
int t = sc.nextInt();
10+
while (t-- != 0) {
11+
int n = sc.nextInt();
12+
int a[] = new int[n];
13+
for (int i = 0; i < n; i++) {
14+
a[i] = sc.nextInt();
15+
}
16+
int temp = 0;
17+
int f = 0;
18+
int idx = 0;
19+
for (int i = n - 1; i > 0; i--) {
20+
if (a[i] > a[i - 1]) {
21+
f = 1;
22+
temp = a[n - 1];
23+
a[n - 1] = a[i - 1];
24+
a[i - 1] = temp;
25+
idx = i - 1;
26+
break;
27+
}
28+
}
29+
if (f == 0) {
30+
for (int i = n - 1; i >= 0; i--) {
31+
System.out.print(a[i] + " ");
32+
}
33+
}
34+
else {
35+
for (int i = idx + 1; i < n; i++) {
36+
for (int j = i + 1; j < n; j++) {
37+
if (a[j] < a[i]) {
38+
temp = a[j];
39+
a[j] = a[i];
40+
a[i] = temp;
41+
}
42+
}
43+
}
44+
for (int i = 0; i < n; i++) {
45+
System.out.print(a[i] + " ");
46+
}
47+
}
48+
System.out.println();
49+
f = 0;
50+
temp = 0;
51+
}
52+
}
53+
}

Array/Sum of bit differences.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
class GFG
5+
{
6+
public static void main (String[] args)
7+
{
8+
Scanner sc = new Scanner(System.in);
9+
int t = sc.nextInt();
10+
while (t-- != 0) {
11+
int n = sc.nextInt();
12+
int a[] = new int[n];
13+
for (int i = 0; i < n; i++) {
14+
a[i] = sc.nextInt();
15+
}
16+
int sum = 0;
17+
int num = 0, rem = 0;
18+
for (int i = 0; i < n; i++) {
19+
for (int j = i + 1; j < n; j++) {
20+
num = a[i] ^ a[j];
21+
while (num != 0) {
22+
rem = num % 2;
23+
if (rem == 1) {
24+
sum += 2;
25+
}
26+
num /= 2;
27+
}
28+
}
29+
}
30+
System.out.println(sum);
31+
}
32+
}
33+
}

BST/Ceil for BST.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
3+
public class solution {
4+
public static int Ceil(BinaryTreeNode<Integer> node, int input) {
5+
Stack<BinaryTreeNode> st = new Stack<>();
6+
int res = -1;
7+
BinaryTreeNode<Integer> curr = node;
8+
while (curr != null || !st.isEmpty()) {
9+
while (curr != null) {
10+
st.push(curr);
11+
curr = curr.left;
12+
}
13+
curr = st.pop();
14+
if (curr.data > input) {
15+
res = curr.data;
16+
break;
17+
}
18+
curr = curr.right;
19+
}
20+
return res;
21+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class Node {
5+
int data;
6+
Node left;
7+
Node right;
8+
9+
Node(int d) {
10+
data = d;
11+
left = right = null;
12+
}
13+
14+
}
15+
16+
17+
18+
class GFG {
19+
static Node createTree(Node root, int d) {
20+
if (root == null) {
21+
root = new Node(d);
22+
}
23+
else if (d < root.data) {
24+
root.left = createTree(root.left, d);
25+
}
26+
else {
27+
root.right = createTree(root.right, d);
28+
}
29+
return root;
30+
}
31+
32+
public static void main (String[] args) {
33+
int j = 0;
34+
int f = 0;
35+
Node root = null;
36+
Scanner sc = new Scanner(System.in);
37+
int n = sc.nextInt();
38+
int a[] = new int[n];
39+
for (int i = 0; i < n; i++) {
40+
a[i] = sc.nextInt();
41+
root = createTree(root, a[i]);
42+
}
43+
Queue<Node> q = new LinkedList<>();
44+
Node curr = null;
45+
q.add(root);
46+
while (!q.isEmpty()) {
47+
curr = q.remove();
48+
if (a[j++] != curr.data) {
49+
f = 1;
50+
break;
51+
}
52+
if (curr.left != null) {
53+
q.add(curr.left);
54+
}
55+
if (curr.right != null) {
56+
q.add(curr.right);
57+
}
58+
}
59+
if (f == 0) {
60+
System.out.println("Yes");
61+
}
62+
else {
63+
System.out.println("No");
64+
}
65+
}
66+
}

BST/Check if trees are identical.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
boolean isIdentical(Node root1, Node root2)
2+
{
3+
Stack<Node> st1 = new Stack<>();
4+
ArrayList<Integer> al1 = new ArrayList<>();
5+
Node curr = root1;
6+
while (!st1.empty() || curr != null) {
7+
while (curr != null) {
8+
st1.push(curr);
9+
curr = curr.left;
10+
}
11+
curr = st1.pop();
12+
al1.add(curr.data);
13+
curr = curr.right;
14+
}
15+
16+
Stack<Node> st2 = new Stack<>();
17+
ArrayList<Integer> al2 = new ArrayList<>();
18+
curr = root2;
19+
while (!st2.empty() || curr != null) {
20+
while (curr != null) {
21+
st2.push(curr);
22+
curr = curr.left;
23+
}
24+
curr = st2.pop();
25+
al2.add(curr.data);
26+
curr = curr.right;
27+
}
28+
29+
if (al1.size() != al2.size()) {
30+
return false;
31+
}
32+
33+
for (int i = 0; i < al1.size(); i++) {
34+
if (al1.get(i) != al2.get(i)) {
35+
return false;
36+
}
37+
}
38+
return true;
39+
}

BST/Floor from BST.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
3+
public class solution {
4+
public static int Floor(BinaryTreeNode<Integer> node, int input) {
5+
Stack<BinaryTreeNode> st = new Stack<>();
6+
int res = -1;
7+
BinaryTreeNode<Integer> curr = node;
8+
while (!st.isEmpty() || curr != null) {
9+
while (curr != null) {
10+
st.push(curr);
11+
curr = curr.left;
12+
}
13+
curr = st.pop();
14+
if (curr.data < input) {
15+
res = curr.data;
16+
}
17+
curr = curr.right;
18+
}
19+
return res;
20+
}
21+
}

0 commit comments

Comments
 (0)