-
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
7fe0f7c
commit 2728255
Showing
4 changed files
with
85 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,61 @@ | ||
//{ 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[] a = IntArray.input(br, n); | ||
|
||
Solution obj = new Solution(); | ||
int res = obj.longestSubseq(n, a); | ||
|
||
System.out.println(res); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
|
||
class Solution { | ||
public static int longestSubseq(int n, int[] a) { | ||
// code here | ||
int ans = 1; | ||
int res[] = new int[1002]; | ||
for(int i : a) | ||
{ | ||
res[i] = 1 + Math.max(res[i-1], res[i+1]); | ||
ans = Math.max(ans, res[i]); | ||
} | ||
return ans; | ||
} | ||
} |
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^2) | ||
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,20 @@ | ||
class Solution | ||
{ | ||
public int specialArray(int[] nums) | ||
{ | ||
int n = nums.length; | ||
|
||
for(int i =0; i<=n; i++) | ||
{ | ||
int count = 0; | ||
|
||
for (int j : nums) | ||
if (j >= i) | ||
count++; | ||
|
||
if (i == count) | ||
return i; | ||
} | ||
return -1; | ||
} | ||
} |