Created
June 11, 2017 18:56
-
-
Save jianminchen/12a27ecf9a0968db72c268653774b45d to your computer and use it in GitHub Desktop.
Hackerrank count solution - score 4 out of 40, https://www.hackerrank.com/contests/infinitum18/challenges/count-solutions
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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
class Solution | |
{ | |
static int countSolutions(int a, int b, int c, int d) | |
{ | |
int count = 0; | |
// Complete this function | |
for (int x = 1; x <= c; x++) | |
{ | |
var leftBase = x * x; | |
var rightBase = x * a; | |
var difference = leftBase - rightBase; | |
// it is not sorted and binary search not working | |
for(int y = 1; y <= d; y++) | |
{ | |
var leftPart = y * y; | |
var rightPart = y * b; | |
var differenceII = leftPart - rightPart; | |
var sum = difference + differenceII; | |
if (sum == 0) | |
{ | |
count++; | |
} | |
} | |
} | |
return count; | |
} | |
static void Main(String[] args) | |
{ | |
int q = Convert.ToInt32(Console.ReadLine()); | |
for (int a0 = 0; a0 < q; a0++) | |
{ | |
string[] tokens_a = Console.ReadLine().Split(' '); | |
int a = Convert.ToInt32(tokens_a[0]); | |
int b = Convert.ToInt32(tokens_a[1]); | |
int c = Convert.ToInt32(tokens_a[2]); | |
int d = Convert.ToInt32(tokens_a[3]); | |
int result = countSolutions(a, b, c, d); | |
Console.WriteLine(result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment