Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 17, 2016 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/e4906acce9c763626453adb182ee3b03 to your computer and use it in GitHub Desktop.
Save jianminchen/e4906acce9c763626453adb182ee3b03 to your computer and use it in GitHub Desktop.
DiffK - facebook code lab - study the solution provided by the lab
class Solution {
public:
bool diffPossible(vector<int> &num, int diff) {
if (num.size() < 2 || diff < 0) return false;
int j = 0, len = num.size();
for (int i = 0; i < len - 1; i++) {
j = max(j, i + 1);
while (j < len && num[j] - num[i] < diff) j += 1;
if (j < len && num[j] - num[i] == diff) return true;
}
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment