-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
642fa26
commit 090508a
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |