Skip to content

Instantly share code, notes, and snippets.

@sbsatter
sbsatter / TheCelebrityProblem.java
Created August 20, 2016 07:20
Geeks4Geeks celebrity problem
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);
@sbsatter
sbsatter / GroupSum5
Created August 19, 2016 16:50
Backtracking: GroupSum5- Coding bat Recursive 2 http://codingbat.com/prob/p138907
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;
@sbsatter
sbsatter / GroupSum
Created August 19, 2016 16:16
Classic backtracking problem: Recursion-2 > groupSum prev | next | chance Given an array of ints, is it possible to choose a group of some of the ints, such that the group sums to the given target? This is a classic backtracking recursion problem. Once you understand the recursive backtracking strategy in this problem, you can use the same patte…
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{
@sbsatter
sbsatter / CountPairs.java
Created August 19, 2016 15:45
http://codingbat.com/prob/p154048 Count overlapping pairs in the form AxA where A has a pair!
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))
@sbsatter
sbsatter / NestedParenthesis.java
Created August 19, 2016 15:27
Find nested parenthesis recursively
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));
}
@sbsatter
sbsatter / Count8.java
Created August 19, 2016 14:59
Given a non-negative int n, compute recursively (no loops) the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4. Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12). count8(8) → 1 co…
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);
}
@sbsatter
sbsatter / StrCount.java
Created August 19, 2016 14:48
Given a string and a non-empty substring sub, compute recursively the number of times that sub appears in the string, without the sub strings overlapping. strCount("catcowcat", "cat") → 2 strCount("catcowcat", "cow") → 1 strCount("catcowcat", "dog") → 0
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);
}
@sbsatter
sbsatter / PairStar.java
Created August 19, 2016 14:00
CodingBat: Recursion-1 > pairStar prev | next | chance Given a string, compute recursively a new string where identical chars that are adjacent in the original string are separated from each other by a "*". pairStar("hello") → "hel*lo" pairStar("xxyy") → "x*xy*y" pairStar("aaaa") → "a*a*a*a"
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;
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);
@sbsatter
sbsatter / triangle.java
Created August 19, 2016 12:48
CODINGBAT RECURSION 1 PROBLEM- JAVA- TRIANGLE We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows. triangle(0) → 0 triangle(1) → 1 triangle(2)…
public int triangle(int rows) {
if(rows==0)
return 0;
if(rows==1){
return 1;
}
return triangle(rows-1)+rows;
}