Consecutive Array Search
January 10, 2020
Given an array of distinct integers and a target integer, determine if two adjacent integers in the array sum to the target. Solve the problem twice, once assuming the array is unsorted and once assuming the array is sorted.
Your task is to solve the two array search problems described above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
In Python. It uses a lazy array with the sums of the consecutive elements, so that for the sorted array case we can search for the target.
public class Main {
public static void main(String args[])
{
int target = 100;
int[] numeros = {5, 26, 39, 47, 53, 38, 12, 23, 41, 39, 40, 16, 47, 13, 10, 18, 4, 22, 50};
}
}
Here’s a solution in C.
The program takes a target value, followed by a u or s (to indicate sorted or unsorted), followed by an array of ints.
The printed output shows the index of the first element of the pair of adjacent elements that sum to the specified target. If there is no such pair, the program prints -1.
Example usage: