Created
July 6, 2016 09:45
-
-
Save kapilraj2436/bb9fc7d0d3d3bacd098594e2cde0534c to your computer and use it in GitHub Desktop.
Cadbury Problem in java Algorithm
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
import java.io.*; | |
public class CandidateCode | |
{ | |
private static int cadbury(int m,int n,int p,int q) | |
{ | |
if (m>n){ | |
int temp=m; | |
m=n; | |
n=temp; | |
}if (p>q){ | |
int temp=p; | |
p=q; | |
q=temp; | |
} | |
int count=0; | |
for (int i = m;i>0;) | |
{ | |
for (int j= p;j>0;) | |
{ | |
int counter = getCount(i,j); | |
System.out.println("total number of students are :"+counter); | |
count = count + counter; | |
if(j==q){ | |
j=-1; | |
break; | |
} | |
j=q; | |
} | |
if(i==n){ | |
i=-1; | |
break; | |
} | |
i=n; | |
} | |
return count; | |
} | |
private static int getCount(int m, int p) | |
{ | |
int count = 0; | |
for(int i=0,j=0;i>=0 && j>=0;i++) | |
{ | |
if(m==p){ | |
j=-1; | |
count++; | |
break; | |
} | |
if(m<p){ | |
p=p-m; | |
count++; | |
} | |
if(m>p){ | |
m=m-p; | |
count++; | |
} | |
} | |
return count; | |
} | |
public static void main(String[] args) | |
{ | |
//7,8,9,6-----5,6,3,4------- | |
int count=cadbury(7,8,9,6); | |
System.out.println("Counter : "+ count); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
problem :- [Check what was the problem (https://www.careercup.com/question?id=5190297283723264)