bool findElement(int a[], int length, int expectedNum) { int i = 0; while(i < length) { if(a[i] == expectedNum) return true; else { int diff = abs(expectedNum - a[i]); i = diff + i; } } return false; }
Tag Archives: datastructure. microsoft interview question
Checking if an array of integers is a set.
0A set must pass on these three conditions:
– All values are positive
– Sorted
– Non-duplicates
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
bool isSet(int arr[],int n){ | |
int i=0; | |
while(i<n){ | |
if(arr[i]<0) | |
return false; | |
if(i==0){ | |
i++; | |
continue; | |
} | |
if(arr[i]<=arr[i-1]) | |
return false; | |
i++; | |
} | |
return true; | |
} |