Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
801 B
0
Indexable
Never
#include <stdio.h>

int fndcoin(int a[], int n, int target) {
    for(int i=0;i<n;i++) {
        
        //if the target is found return index
        if(a[i] == target && a[i+1] != target) {
            return i;
           
        }
        //if the array has target value more than once
        else if(a[i] == target && a[i+1] == target) {
            return -1;
        }

        //if target is not found return index where it should be inserted
        else if(target > a[i] && target < a[i+1]) {
            return i+1;
            
        }
    }
}

int main() {
    int n;
    scanf("%d", &n);
    int a[n];
    for(int i=0;i<n;i++) {
        scanf("%d", &a[i]);
    }
    int target;
    scanf("%d", &target);
    int result = fndcoin(a, n, target);
    printf("Index : %d\n", result);
}
Leave a Comment