From a29fa6b4ec6751169f5843f5c2994729106758dc Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Tue, 26 Mar 2024 21:31:24 +0530 Subject: [PATCH] Added codes for 26 March --- GeeksForGeeks/March/26-3-24/GFG.java | 54 +++++++++++++++++++++++++++ GeeksForGeeks/March/26-3-24/README.md | 2 + LeetCode/March/26-3-24/README.md | 2 + LeetCode/March/26-3-24/Solution.java | 24 ++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 GeeksForGeeks/March/26-3-24/GFG.java create mode 100644 GeeksForGeeks/March/26-3-24/README.md create mode 100644 LeetCode/March/26-3-24/README.md create mode 100644 LeetCode/March/26-3-24/Solution.java diff --git a/GeeksForGeeks/March/26-3-24/GFG.java b/GeeksForGeeks/March/26-3-24/GFG.java new file mode 100644 index 0000000..db35c7a --- /dev/null +++ b/GeeksForGeeks/March/26-3-24/GFG.java @@ -0,0 +1,54 @@ +//{ Driver Code Starts +import java.util.*; + +public class GFG { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t-- > 0) { + String s = sc.next(); + Solution ss = new Solution(); + boolean result = ss.isAdditiveSequence(s); + System.out.println((result == true ? 1 : 0)); + } + sc.close(); + } +} +// } Driver Code Ends + + +// User function Template for Java +class Solution +{ + public boolean isAdditiveSequence(String n) + { + // code here + int m = (int)Math.min(18,n.length()); + + for(int i=0;i=l) + { + if(Long.parseLong(n.substring(j+1,j+l+1))==sum) + { + i = j; + k = i; + j = j+l-1; + + if(j+1==m-1) + return true; + } + } + } + } + return false; + } +} diff --git a/GeeksForGeeks/March/26-3-24/README.md b/GeeksForGeeks/March/26-3-24/README.md new file mode 100644 index 0000000..1d3ee04 --- /dev/null +++ b/GeeksForGeeks/March/26-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n^2) +Space complexity - O(1) diff --git a/LeetCode/March/26-3-24/README.md b/LeetCode/March/26-3-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/March/26-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/March/26-3-24/Solution.java b/LeetCode/March/26-3-24/Solution.java new file mode 100644 index 0000000..0a93430 --- /dev/null +++ b/LeetCode/March/26-3-24/Solution.java @@ -0,0 +1,24 @@ +class Solution +{ + public int firstMissingPositive(int[] nums) + { + int n = nums.length; + + for (int i = 0; i < n; ++i) + while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) + swap(nums, i, nums[i] - 1); + + for (int i = 0; i < n; ++i) + if (nums[i] != i + 1) + return i + 1; + + return n + 1; + } + + private void swap(int[] nums, int i, int j) + { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } +}