Skip to content

Commit

Permalink
Added codes for 5 March
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Mar 5, 2024
1 parent 642fa26 commit 090508a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
74 changes: 74 additions & 0 deletions GeeksForGeeks/March/05-3-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

class Main {

public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim()); //Inputting the testcases
while(t-->0){
int n = Integer.parseInt(br.readLine().trim()); // size of array
int arr[] = new int[n];
String inputLine[] = br.readLine().trim().split(" ");
for(int i=0; i<n; i++){
arr[i] = Integer.parseInt(inputLine[i]); // input elements of array
}

Solution ob = new Solution();

System.out.println(ob.maxIndexDiff(arr, n)); // print the result
}
}
}
// } Driver Code Ends


class Solution
{
static int maxIndexDiff(int arr[], int n)
{
int maxDiff;
int i, j;

int RMax[] = new int[n];
int LMin[] = new int[n];

LMin[0] = arr[0];
for (i = 1; i < n; ++i)
LMin[i] = min(arr[i], LMin[i - 1]);

RMax[n - 1] = arr[n - 1];
for (j = n - 2; j >= 0; --j)
RMax[j] = max(arr[j], RMax[j + 1]);

i = 0;
j = 0;
maxDiff = -1;
while (j < n && i < n)
{
if (LMin[i] <= RMax[j])
{
maxDiff = max(maxDiff, j - i);
j = j + 1;
}
else
i = i + 1;
}

return maxDiff;

}

static int max(int x, int y)
{
return x > y ? x : y;
}

static int min(int x, int y)
{
return x < y ? x : y;
}
}


2 changes: 2 additions & 0 deletions GeeksForGeeks/March/05-3-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/March/05-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
19 changes: 19 additions & 0 deletions LeetCode/March/05-3-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution
{
public int minimumLength(String s)
{
int i = 0;
int j = s.length() - 1;

while (i < j && s.charAt(i) == s.charAt(j))
{
char c = s.charAt(i);
while (i <= j && s.charAt(i) == c)
++i;
while (i <= j && s.charAt(j) == c)
--j;
}

return j - i + 1;
}
}

0 comments on commit 090508a

Please sign in to comment.