Google Interview Question
Software EngineersCountry: United States
Since the nos (a,b,c,d) are always positive.
Is there a case when a is not equal to c or d?
What I mean is that, is it safe to assume that the cube summation will only be equal to itself i.e. a^3+b^3 = c^3+d^3 only when ((a = c && b=d) or (a =d&&b=c))
Do we know if there is a solution where a is not equal to c or d when the equation is satisfied for positive integers?
Using a hashmap:
//MehrdadAP
void solve(int N)
{
unordered_map<long long int,vector< pair<int,int> > > umap;
for (int i=0;i<=N;i++)
for (int j=i;j<=N;j++){
long long int tmp = i*1LL*i*aLL*i+j*1LL*j*1LL*j;
umap[tmp].push_back({i,j});
}
for (auto it:umap){
if (it.second.size()==1) continue;
for (auto x:it.second)
cout << "(" << x.first << "," << x.second << ")" << " " ;
cout << endl;
}
}
Use a hash table to keep the sums of all possible pairs (i,j).
def a3b3c3d3(n):
l=[pow(i,3) for i in range(n)]
d={}
for i in range(n):
for j in range(i, n):
s=l[i]+l[j]
d.setdefault(l[i]+l[j], []).append((i,j))
for l in d.values():
if len(l)>=2:
first=True
for i,j in l:
if (first):
first=False
else:
sys.stdout.write("=")
sys.stdout.write("{0}^3+{1}^3".format(i,j))
sys.stdout.write("\n")
public static void main(String args[])
{
Map<Integer,Set<String>> list=new HashMap<Integer,Set<String>>();
int n=(int) Math.pow(10,2);
int timecomplexity=0;
for(int a=0;a<n;a++){
for(int b=0;b<n;b++){
Set<String> l;
timecomplexity++;
int result=(int) (Math.pow(a, 3)+Math.pow(b, 3));
if(list.containsKey(result)){
l=list.get(result);
l.add(a+","+b);
list.put(result, l);
}
else{
l=new TreeSet<String>();
l.add(a+","+b);
list.put(result, l);
}
}
}
for(Map.Entry<Integer,Set<String>> entry:list.entrySet()){
Set<String> l=entry.getValue();
System.out.print(entry.getKey()+": ");
for(String s:l){
System.out.print(s+" ; ");
}
System.out.println();
}
public static void main(String args[])
{
Map<Integer,Set<String>> list=new HashMap<Integer,Set<String>>();
int n=(int) Math.pow(10,2);
for(int a=0;a<n;a++){
for(int b=0;b<n;b++){
Set<String> l;
int result=(int) (Math.pow(a, 3)+Math.pow(b, 3));
if(list.containsKey(result)){
l=list.get(result);
l.add(a+","+b);
list.put(result, l);
}
else{
l=new TreeSet<String>();
l.add(a+","+b);
list.put(result, l);
}
}
}for(Map.Entry<Integer,Set<String>> entry:list.entrySet()){
Set<String> l=entry.getValue();
System.out.print(entry.getKey()+": ");
for(String s:l){
System.out.print(s+" ; ");
}
System.out.println();
}
}
Time Complexity = O(n^2)
The idea is to maintain a hash map of integer and pair-of-numbers as shown below.
public class Main {
public static class Pair{
int a,b;
Pair(int a, int b){
this.a = a;
this.b = b;
}
public String toString(){
return "("+this.a+", "+this.b+")";
}
}
public static void main(String[] args) {
int n = 100000;
printTaxiCabNumbers(n);
}
private static void printTaxiCabNumbers(int n) {
HashMap<Integer, Pair> map = new HashMap<Integer, Pair>();
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
int m = (int) (Math.pow(i, 3)+Math.pow(j, 3));
if(map.containsKey(m)){
System.out.println(new Pair(i,j).toString()+" and "+map.get(m).toString());
}else{
map.put((int) (m), new Pair(i,j));
}
}
}
}
}
Time Complexity = O(n)
The idea is to maintain a HashMap of sum and Integer-pairs.
public class Main {
public static class Pair{
int a,b;
Pair(int a, int b){
this.a = a;
this.b = b;
}
public String toString(){
return "("+this.a+", "+this.b+")";
}
}
public static void main(String[] args) {
int n = 100000;
printTaxiCabNumbers(n);
}
private static void printTaxiCabNumbers(int n) {
HashMap<Integer, Pair> map = new HashMap<Integer, Pair>();
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
int m = (int) (Math.pow(i, 3)+Math.pow(j, 3));
if(map.containsKey(m)){
System.out.println(new Pair(i,j).toString()+" and "+map.get(m).toString());
}else{
map.put((int) (m), new Pair(i,j));
}
}
}
}
}
must it be in collection? or I can write solution while searching solutions.
public class AllSolutions {
public static void main(String[] args){
long maxint = (int)Math.pow(10,5);
long powOfI=0;
long powOfJ =0;
for(int i =0;i<=maxint;i++){
powOfI=(long)Math.pow(i,3);
for (int j =i;j<=maxint;j++){
powOfJ=(long)Math.pow(j,3);
System.out.println("a && c = "+i+", b && d ="+j+", => a^3+b^3=c^3+d^3="+(powOfI+ powOfJ));
System.out.println("a && d = "+i+", b && c ="+j+", => a^3+b^3=c^3+d^3="+(powOfI+ powOfJ));
System.out.println("b && c = "+i+", a && d ="+j+", => a^3+b^3=c^3+d^3="+(powOfI+ powOfJ));
System.out.println("b && d = "+i+", a && c ="+j+", => a^3+b^3=c^3+d^3="+(powOfI+ powOfJ));
}
}
}
}
I am just writing my thoughts on this:
use hash map and get all the numbers.
- aka August 24, 2015