xyz Interview Question
- 0of 0 votes
AnswersProblem:
- sarunreddy82 April 29, 2018 in United States
Insert + or – sign anywhere between the digits 123456789 in such a way that the expression evaluates to 100. The condition is that the order of the digits must not be changed.
e.g.: 1 + 2 + 3 – 4 + 5 + 6 + 78 + 9 = 100
I have below C solution - Can you please help me to convert to Java. I need this solution in Java.
#include<stdio.h>
#include<conio.h>
int findnumber(int i,int j)
{
int k;
int n;
for(k = 0; k < j; k++)
{
n = i % 3;
i = i / 3;
}
return n;
}
void main()
{
int i, j, k, current, result, operation;
clrscr();
for(i = 0; i < 19683; i++)
{
if(i%3 == 0)
continue;
current = 0;
result = 0;
for(j = 1; j < 10; j++)
{
k = findnumber(i,j);
if(k==0)
{
current = current * 10 + j;
}
else
{
result = result + (operation == 1 ? current : -current);
current = j;
operation = k;
}
}
result = result + (operation == 1 ? current : -current);
if(result == 100)
{
for(j = 1; j < 10; j++)
{
k = findnumber(i,j);
if(k==0)
printf("%d",j);
else
printf("%c%d",k==1?'+':'-',j);
}
printf("\n");
}
}
getch();
}| Report Duplicate | Flag | PURGE
xyz Java
Interview Type: Phone Interview