#include <stdio.h>
#include <stdlib.h>
int findFirst( int * nums, int length, int target)
{
if ( nums == nullptr || length <= 0 )
return - 1 ;
int index = 0 ;
while ( index < length && nums[ index] ! = target)
{
int delta = target - nums[ index] ;
index + = abs ( delta) ;
}
if ( index < length)
return index;
return - 1 ;
}
/************************* test code *****************************/
bool isFirst( int * nums, int length, int target, int index)
{
for ( int i = 0 ; i < index; ++ i)
{
if ( nums[ i] == target)
return false ;
}
if ( nums[ index] == target)
return true ;
return false ;
}
void test( const char * testName, int * nums, int length, int target, bool isIn)
{
int result = findFirst( nums, length, target) ;
if ( ( isIn && result >= 0 && isFirst( nums, length, target, result) )
|| ( ! isIn && result < 0 ) )
{
printf ( "%s passed. Result is %d. \n " , testName, result) ;
}
else
{
printf ( "%s FAILED.\n " , testName) ;
}
}
void testGroup1( )
{
int nums[ ] = { 4 , 5 , 6 , 5 , 6 , 7 , 8 , 9 , 10 , 9 } ;
test( "test1_1" , nums, sizeof ( nums) / sizeof ( int ) , 0 , false ) ;
test( "test1_2" , nums, sizeof ( nums) / sizeof ( int ) , 1 , false ) ;
test( "test1_3" , nums, sizeof ( nums) / sizeof ( int ) , 2 , false ) ;
test( "test1_4" , nums, sizeof ( nums) / sizeof ( int ) , 3 , false ) ;
test( "test1_5" , nums, sizeof ( nums) / sizeof ( int ) , 4 , true ) ;
test( "test1_6" , nums, sizeof ( nums) / sizeof ( int ) , 5 , true ) ;
test( "test1_7" , nums, sizeof ( nums) / sizeof ( int ) , 6 , true ) ;
test( "test1_8" , nums, sizeof ( nums) / sizeof ( int ) , 7 , true ) ;
test( "test1_9" , nums, sizeof ( nums) / sizeof ( int ) , 8 , true ) ;
test( "test1_10" , nums, sizeof ( nums) / sizeof ( int ) , 9 , true ) ;
test( "test1_11" , nums, sizeof ( nums) / sizeof ( int ) , 10 , true ) ;
test( "test1_12" , nums, sizeof ( nums) / sizeof ( int ) , 11 , false ) ;
}
void testGroup2( )
{
int nums[ ] = { 8 , 7 , 6 , 7 , 6 , 5 , 4 , 3 , 2 , 3 , 4 , 3 } ;
test( "test2_1" , nums, sizeof ( nums) / sizeof ( int ) , 0 , false ) ;
test( "test2_2" , nums, sizeof ( nums) / sizeof ( int ) , 1 , false ) ;
test( "test2_3" , nums, sizeof ( nums) / sizeof ( int ) , 2 , true ) ;
test( "test2_4" , nums, sizeof ( nums) / sizeof ( int ) , 3 , true ) ;
test( "test2_5" , nums, sizeof ( nums) / sizeof ( int ) , 4 , true ) ;
test( "test2_6" , nums, sizeof ( nums) / sizeof ( int ) , 5 , true ) ;
test( "test2_7" , nums, sizeof ( nums) / sizeof ( int ) , 6 , true ) ;
test( "test2_8" , nums, sizeof ( nums) / sizeof ( int ) , 7 , true ) ;
test( "test2_9" , nums, sizeof ( nums) / sizeof ( int ) , 8 , true ) ;
test( "test2_10" , nums, sizeof ( nums) / sizeof ( int ) , 9 , false ) ;
test( "test2_11" , nums, sizeof ( nums) / sizeof ( int ) , 10 , false ) ;
test( "test2_12" , nums, sizeof ( nums) / sizeof ( int ) , 11 , false ) ;
}
void testGroup3( )
{
int nums[ ] = { 6 , 5 , 4 , 3 , 4 , 5 , 6 , 7 , 8 } ;
test( "test3_1" , nums, sizeof ( nums) / sizeof ( int ) , 0 , false ) ;
test( "test3_2" , nums, sizeof ( nums) / sizeof ( int ) , 1 , false ) ;
test( "test3_3" , nums, sizeof ( nums) / sizeof ( int ) , 2 , false ) ;
test( "test3_4" , nums, sizeof ( nums) / sizeof ( int ) , 3 , true ) ;
test( "test3_5" , nums, sizeof ( nums) / sizeof ( int ) , 4 , true ) ;
test( "test3_6" , nums, sizeof ( nums) / sizeof ( int ) , 5 , true ) ;
test( "test3_7" , nums, sizeof ( nums) / sizeof ( int ) , 6 , true ) ;
test( "test3_8" , nums, sizeof ( nums) / sizeof ( int ) , 7 , true ) ;
test( "test3_9" , nums, sizeof ( nums) / sizeof ( int ) , 8 , true ) ;
test( "test3_10" , nums, sizeof ( nums) / sizeof ( int ) , 9 , false ) ;
test( "test3_11" , nums, sizeof ( nums) / sizeof ( int ) , 10 , false ) ;
test( "test3_12" , nums, sizeof ( nums) / sizeof ( int ) , 11 , false ) ;
}
int main( )
{
testGroup1( ) ;
testGroup2( ) ;
testGroup3( ) ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmludCBmaW5kRmlyc3QoaW50KiBudW1zLCBpbnQgbGVuZ3RoLCBpbnQgdGFyZ2V0KQp7CiAgIGlmIChudW1zID09IG51bGxwdHIgfHwgbGVuZ3RoIDw9IDApCiAgICAgIHJldHVybiAtMTsKCiAgIGludCBpbmRleCA9IDA7CiAgIHdoaWxlIChpbmRleCA8IGxlbmd0aCAmJiBudW1zW2luZGV4XSAhPSB0YXJnZXQpCiAgIHsKICAgICAgaW50IGRlbHRhID0gdGFyZ2V0IC0gbnVtc1tpbmRleF07CiAgICAgIGluZGV4ICs9IGFicyhkZWx0YSk7CiAgIH0KCiAgIGlmIChpbmRleCA8IGxlbmd0aCkKICAgICAgcmV0dXJuIGluZGV4OwoKICAgcmV0dXJuIC0xOwp9CgovKioqKioqKioqKioqKioqKioqKioqKioqKiB0ZXN0IGNvZGUgKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCmJvb2wgaXNGaXJzdChpbnQqIG51bXMsIGludCBsZW5ndGgsIGludCB0YXJnZXQsIGludCBpbmRleCkKewogICBmb3IgKGludCBpID0gMDsgaSA8IGluZGV4OyArK2kpCiAgIHsKICAgICAgaWYgKG51bXNbaV0gPT0gdGFyZ2V0KQogICAgICAgICByZXR1cm4gZmFsc2U7CiAgIH0KCiAgIGlmIChudW1zW2luZGV4XSA9PSB0YXJnZXQpCiAgICAgIHJldHVybiB0cnVlOwoKICAgcmV0dXJuIGZhbHNlOwp9Cgp2b2lkIHRlc3QoY29uc3QgY2hhciogdGVzdE5hbWUsIGludCogbnVtcywgaW50IGxlbmd0aCwgaW50IHRhcmdldCwgYm9vbCBpc0luKQp7CiAgIGludCByZXN1bHQgP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