Microsoft Interview Question: I was asked two questions. On... | Glassdoor.co.in

Interview Question

Software Development Engineer II Interview Bengaluru

I was asked two questions. One was given an array of

  integers find triplets such that a^2 + b^2 = c^2.
Answer

Interview Answer

2 Answers

1

One way to do this could be

____________________________________________________
#include
#include "math.h"

using namespace std;

int arr[] = {1,0,4,5,3,2,1,8,0,7};

void test(int a, int b, int c)
{
    if( a*a + b*b == c*c)
    {
        cout << "Found a triplet: (" << a << "," << b << "," << c << ")" << endl;
    }
}

int main()
{
    int size = sizeof(arr) / sizeof(int);

    if(size < 3)
        return -1;

    for(int a=0; a

Paul on 03-Nov-2012
0

Here is a O(n) solution

int[] nums = new int[] { 3, 5, 6, 2, 4, 16, 61,8,10,7};

int[] lengths = new int[nums.Max() + 1];

for (int i = 0; i > pythaTriplets =
  new List> ();
for (int i = 2; i (
              2*i,i*i-1,i*i+1));
        }
    }
    catch { continue;}
}
pythaTriplets.Dump();
//2x, x*x-1, x*x+1

Sudipta on 24-Jun-2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.