From 2728255d8292038393d96d4efccca7631dd6f8fe Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Mon, 27 May 2024 10:38:27 +0530 Subject: [PATCH] Added codes for 27 May --- GeeksForGeeks/May/27-5-24/GFG.java | 61 +++++++++++++++++++++++++++++ GeeksForGeeks/May/27-5-24/README.md | 2 + LeetCode/May/27-5-24/README.md | 2 + LeetCode/May/27-5-24/Solution.java | 20 ++++++++++ 4 files changed, 85 insertions(+) create mode 100644 GeeksForGeeks/May/27-5-24/GFG.java create mode 100644 GeeksForGeeks/May/27-5-24/README.md create mode 100644 LeetCode/May/27-5-24/README.md create mode 100644 LeetCode/May/27-5-24/Solution.java diff --git a/GeeksForGeeks/May/27-5-24/GFG.java b/GeeksForGeeks/May/27-5-24/GFG.java new file mode 100644 index 0000000..db46c42 --- /dev/null +++ b/GeeksForGeeks/May/27-5-24/GFG.java @@ -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 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; + } +} diff --git a/GeeksForGeeks/May/27-5-24/README.md b/GeeksForGeeks/May/27-5-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/GeeksForGeeks/May/27-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/May/27-5-24/README.md b/LeetCode/May/27-5-24/README.md new file mode 100644 index 0000000..1d3ee04 --- /dev/null +++ b/LeetCode/May/27-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n^2) +Space complexity - O(1) diff --git a/LeetCode/May/27-5-24/Solution.java b/LeetCode/May/27-5-24/Solution.java new file mode 100644 index 0000000..ecbd8f3 --- /dev/null +++ b/LeetCode/May/27-5-24/Solution.java @@ -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; + } +}