Skip to content

Instantly share code, notes, and snippets.

@kapilraj2436
Created July 6, 2016 09:45
Show Gist options
  • Save kapilraj2436/bb9fc7d0d3d3bacd098594e2cde0534c to your computer and use it in GitHub Desktop.
Save kapilraj2436/bb9fc7d0d3d3bacd098594e2cde0534c to your computer and use it in GitHub Desktop.
Cadbury Problem in java Algorithm
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);
}
}
@kapilraj2436
Copy link
Author

kapilraj2436 commented Jul 6, 2016

problem :- [Check what was the problem (https://www.careercup.com/question?id=5190297283723264)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment