Skip to content

Instantly share code, notes, and snippets.

@ajaylakhimale
Last active March 19, 2022 13:44
Show Gist options
  • Save ajaylakhimale/350823c1671d25c50d5e30e6192a2986 to your computer and use it in GitHub Desktop.
Save ajaylakhimale/350823c1671d25c50d5e30e6192a2986 to your computer and use it in GitHub Desktop.
// problem Link: https://www.hackerrank.com/challenges/apple-and-orange/problem?isFullScreen=true
// given : apples(m[]), oranges(n[]), distance(d) the falled on. home length[s,t], position of trees a, b
// st = [7,10], a =4 , b = 12, m =3, n = 3, apples = [2, 3 -4], oranges = [3, -2, -4]
// add distance of apples[] with distance of tree located, |||'ly for oranges.
// apples[4+2, 4+3, 4+-4] = [6, 7, 0]
// so in the resulting list 7 is the common element in apple distance and sam's house that
// mean they are intersectin. thats what we have to find.
import 'dart:math' as math;
void main() {
void countApplesAndOranges({int m = 0, int n = 0, int a = 0, int b = 0}) {
int applesFallOnHouse = 0;
int orangeFallOnHouse = 0;
// to check no is in range basicaly no lies bet the two elements of houseST list.
List<int> houseST = [math.Random().nextInt(100), math.Random().nextInt(100)];
List<int> appleDist = [];
List<int> orangeDist = [];
List<int> fruitsAreFallOnSamsHome = [];
houseST.sort();
print(houseST);
//
while (appleDist.length < m) {
int random1 = math.Random().nextInt(100);
appleDist.add(random1 + a);
}
print("Apples: $appleDist");
//
while (orangeDist.length < n) {
int random2 = math.Random().nextInt(100);
orangeDist.add(random2 + b);
}
print("Oranges: $orangeDist");
//
for (int apple in appleDist) {
if (apple > houseST.first && apple < houseST.last) {
applesFallOnHouse += 1;
}
}
for (int orange in orangeDist) {
orange > houseST.first && orange < houseST.last?orangeFallOnHouse += 1:orangeFallOnHouse+=0;
}
fruitsAreFallOnSamsHome.add(applesFallOnHouse);
fruitsAreFallOnSamsHome.add(orangeFallOnHouse);
print(
"this shows apples and oranges are fallen on Sam's House : $fruitsAreFallOnSamsHome");
}
countApplesAndOranges(m: 5, n: 5, a: 2, b: 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment