-
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
72aa869
commit a29fa6b
Showing
4 changed files
with
82 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,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; | ||
} | ||
} |
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,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,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; | ||
} | ||
} |