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
class GfG | |
{ | |
// The task is to complete this function | |
int getId(int M[][], int n) | |
{ | |
java.util.Stack<Integer> stack= new java.util.Stack<Integer>(); | |
// Your code here | |
for(int i=0; i<n; i++){ | |
stack.push(i); |
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
public boolean groupSum5(int start, int[] nums, int target) { | |
if(start>=nums.length){ | |
return target==0; | |
} | |
if(nums[start]%5 == 0 && start<nums.length-1 && nums[start+1]!=1){ | |
return groupSum5(start+1, nums, target-nums[start]); | |
}else if(nums[start]%5==0){ | |
return(groupSum5(start+2, nums, target-nums[start])); | |
}else if(groupSum5(start+1, nums, target-nums[start])){ | |
return true; |
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
public boolean groupSum(int start, int[] nums, int target) { | |
if(target==0){ | |
return true; | |
} | |
if(start>=nums.length){ | |
return false; | |
} | |
if(groupSum(start+1, nums, target-nums[start])){ | |
return true; | |
}else{ |
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
public int countPairs(String str) { | |
if(str.length()<3) | |
return 0; | |
String sub=str.substring(0,3); | |
int n= checkSubstringForPair(sub); | |
return n+countPairs(str.substring(1,str.length())); | |
} | |
public int checkSubstringForPair(String sub){ | |
if(sub.charAt(0)==sub.charAt(2)) |
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
public boolean nestParen(String str) { | |
int i= str.indexOf('('), j=str.lastIndexOf(')'); | |
if(i==-1 ^ j==-1){ | |
return false; | |
} | |
if(i==-1 && j==-1) | |
return true; | |
return nestParen(str.substring(i+1,j)); | |
} |
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
public int count8(int n) { | |
if(n==0) | |
return 0; | |
int rem100=n%100; | |
int div=n/10; | |
int rem=n%10; | |
if(rem100==88){ | |
return 2+count8(div); | |
} |
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
public int strCount(String str, String sub) { | |
int index=str.indexOf(sub); | |
int l= sub.length(); | |
if(index==-1){ | |
return 0; | |
} | |
return 1+ strCount(str.substring(0,index) | |
+str.substring(index+l, str.length()), sub); | |
} |
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
class PairStar{ | |
public static void main(String ... args){ | |
System.out.println(pairStar(args[0])); | |
} | |
public static String pairStar(String str) { | |
System.out.println(str); | |
if(str.length()<=1){ | |
return str; | |
} | |
int mid= str.length()/2; |
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
public boolean array220(int[] nums, int index) { | |
if(index>=nums.length-1) | |
return false; | |
for(int i=index+1; i<nums.length; i++){ | |
if((nums[index]*10)==nums[i]){ | |
return true; | |
} | |
} | |
return array220(nums,index+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
public int triangle(int rows) { | |
if(rows==0) | |
return 0; | |
if(rows==1){ | |
return 1; | |
} | |
return triangle(rows-1)+rows; | |
} |