Skip to content

Commit

Permalink
Added codes for 26 March
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Mar 26, 2024
1 parent 72aa869 commit a29fa6b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
54 changes: 54 additions & 0 deletions GeeksForGeeks/March/26-3-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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<m;i++)
{
int k = 0;
for(int j=i+1;j<m;j++)
{
long a = Long.parseLong(n.substring(k,i+1));
long b = Long.parseLong(n.substring(i+1,j+1));
long sum = a+b;
int l = (sum+"").length();

if(n.substring(j+1).length()>=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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/26-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^2)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/March/26-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)
24 changes: 24 additions & 0 deletions LeetCode/March/26-3-24/Solution.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit a29fa6b

Please sign in to comment.