Given an array A[n] such that A[i+1] = A[i]+1 OR A[i]-1, and a number k, can you determine in most efficient way whether k is present in A[n] or not?

0
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;
}

Checking if an array of integers is a set.

0

A set must pass on these three conditions: 
– All values are positive 
– Sorted 
– Non-duplicates 

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;
}
view raw isSet.cpp hosted with ❤ by GitHub