Skip to content

Commit

Permalink
Added codes for 16 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 16, 2024
1 parent 88962d0 commit a10e67e
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
99 changes: 99 additions & 0 deletions GeeksForGeeks/April/16-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;


class IntArray
{
public static int[] input(BufferedReader br, int n) throws IOException
{
String[] s = br.readLine().trim().split(" ");
int[] a = new int[n];
for(int i = 0; i < n; i++)
a[i] = Integer.parseInt(s[i]);

return a;
}

public static void print(int[] a)
{
for(int e : a)
System.out.print(e + " ");
System.out.println();
}

public static void print(ArrayList<Integer> a)
{
for(int e : a)
System.out.print(e + " ");
System.out.println();
}
}

class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine());
while(t-- > 0){

int n;
n = Integer.parseInt(br.readLine());


int k;
k = Integer.parseInt(br.readLine());


int[] arr = IntArray.input(br, n);

Solution obj = new Solution();
int res = obj.minimizeDifference(n, k, arr);

System.out.println(res);

}
}
}

// } Driver Code Ends



class Solution
{
public static int minimizeDifference(int n, int k, int[] arr)
{
// code here
int[] postMax = new int[n];
int[] postMin = new int[n];

postMin[n - 1] = arr[n - 1];
postMax[n - 1] = arr[n - 1];

for (int i = n - 2; i >= 0; --i)
{
postMax[i] = Math.max(arr[i], postMax[i + 1]);
postMin[i] = Math.min(arr[i], postMin[i + 1]);
}

int minDiff = postMax[k] - postMin[k];

int pMin = arr[0];
int pMax = arr[0];

for (int i = 1; i < n - k; ++i)
{
int currMin = Math.max(pMax, postMax[i + k]) - Math.min(pMin, postMin[i + k]);

minDiff = Math.min(minDiff, currMin);
pMax = Math.max(arr[i], pMax);
pMin = Math.min(arr[i], pMin);
}

minDiff = Math.min(minDiff, pMax - pMin);

return minDiff;
}
}

2 changes: 2 additions & 0 deletions GeeksForGeeks/April/16-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/April/16-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
58 changes: 58 additions & 0 deletions LeetCode/April/16-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution
{
public TreeNode addOneRow(TreeNode root, int val, int depth)
{
if (depth == 1)
{
TreeNode newRoot = new TreeNode(val);
newRoot.left = root;
return newRoot;
}

int d = 0;
Queue<TreeNode> q = new ArrayDeque<>(Arrays.asList(root));

while (!q.isEmpty())
{
++d;
for (int sz = q.size(); sz > 0; --sz)
{
TreeNode node = q.poll();
if (node.left != null)
q.offer(node.left);

if (node.right != null)
q.offer(node.right);

if (d == depth - 1)
{
TreeNode cachedLeft = node.left;
TreeNode cachedRight = node.right;
node.left = new TreeNode(val);
node.right = new TreeNode(val);
node.left.left = cachedLeft;
node.right.right = cachedRight;
}
}
if (d == depth - 1)
break;
}

return root;
}
}

0 comments on commit a10e67e

Please sign in to comment.