Write a C function that implements the Liner Search algorithm
instead of Binary Search.
However, this linear search algorithm returns the indices of the
longest sorted subset of numbers in
an array of integers of size n. The longest sorted subset of
numbers must satisfy three conditions.
First, the subset consists of unique numbers (no duplicate);
second, all the numbers in this subset is
divisible by m, the minimum size of the subset is two elements. In
the main method print all values
of all the discovered longest sorted subsets.
Notes there could be more than one longest sorted subset (e.g., two
subsets that satisfy the
previously stated conditions). The subsets could exist in ascending
or descending order. We
always scan the array from left to right ( 0 to n-1 indices)
Examples, find the longest sorted subset that is divisible by
3
5 3 2 8 12 6 9 9 21 30
The longest sorted subset that is divisible by 3 is {9, 21, 30}. It
exists between indices [7 - 9]. Note
{6, 9, 9, 21, 30} is not the longest because the value 9 exists
twice.
Examples, find the longest sorted subset that is divisible by
2
2 6 8 0 3 12 14 20 3 5
The longest sorted subset that is divisible by 2 are {2, 6, 8} and
{12, 14, 20}. The exist between
indices [0-2] and [5-7]. Note {6, 9, 9, 21, 30} is not the longest
because the value 9 exists twice.
Examples, find the longest sorted subset that is divisible by
3
19 0 13 11 2 9 17 1 3 5
The longest sorted subset that is divisible by 2 is empty or null
.
Examples, find the longest sorted subset that is divisible by
5
5 15 50 3 19 80 50 10 2 7
The longest sorted subset that is divisible by 5 are {5, 15, 50}
and {80, 50, 10}. The exist between
indices [0-2] and [5-7].
Elements are not sorted.
//do comment if any problem arises
//do thumbs up if it helps
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//this function returns next element of array which id divisible by d starting from given start index
int next_divisible(int arr[], int n, int start, int d)
{
for (int i = start; i < n; i++)
if (arr[i] != 0 && arr[i] % d == 0)
return i;
return -1;
}
//this function returns true if element s is found starting from index l to r
int search(int array[], int l, int r, int s)
{
for (int i = l; i <= r; i++)
{
if (array[i] == s)
return 1;
}
return 0;
}
//number of indices found
int number = 0;
//this function returns 2d array where first collumn is start of index and second collumn denotes ending index of array where
//given subsequence is found
int **linearSearch(int array[], int n, int d)
{
//2d array
int **ret = (int **)(malloc(sizeof(int **)));
for (int i = 0; i < 10; i++)
{
ret[i] = (int *)(malloc(sizeof(int[2])));
ret[i][0] = ret[i][1] = 0;
}
int from = next_divisible(array, n, 0, d);
ret[0][0] = from;
ret[0][1] = from;
for (int i = from; i < n; i++)
{
//if next element is not 0 and divisible by d and is not repeted
if (array[i] != 0 && array[i] % d == 0 && search(array, ret[number][0], ret[number][1], array[i]) == 0)
{
ret[number][1] = i;
}
else
{
if (ret[number][1] - ret[number][0] > 1)
number++;
ret[number][0] = next_divisible(array, n, i, d);
ret[number][1] = next_divisible(array, n, i, d);
if (ret[number][0] == -1)
return ret;
i = next_divisible(array, n, i, d);
}
}
number++;
return ret;
}
int calculateMax(int **arr, int n)
{
int max = 0;
for (int i = 0; i < number; i++)
if (arr[i][1] - arr[i][0])
max = 0;
}
int main()
{
int array[] = {2, 6, 8, 0, 3, 12, 14, 20, 3, 5};
int size = 10;
int divisible_by = 2;
int **final = linearSearch(array, size, divisible_by);
int max = calculateMax(final, number);
for (int i = 0; i < number; i++)
if (final[i][1] - final[i][0] == max)
printf("index of longest subsequence [%d - %d]\n", final[i][0], final[i][1]);
}
Output:

Write a C function that implements the Liner Search algorithm instead of Binary Search. However, this...
Just Q3 and Q4
Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...
1. What is the minimum number of locations a binary search algorithm will have to examine when looking for a particular value in a sorted array of 200 elements? (2 points) 1 6 7 8 200 2. In general, which of the following sorting algorithms is the MOST efficient? (2 points) Bubble sort Insertion sort Heap sort Merge sort Selection sort 3. Which of the following are quadratic-sorting algorithms? (2 points) I. insertion sort II. selection sort III. merge sort...
JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized as follows: // index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 int[] numbers = {-30, -9, -6, -4, -2, -1, 0, 2, 4, 10, 12, 17, 22, 30}; // search for the value -5 int index = binarySearch(numbers, -5); Write the indexes of the elements that would be examined by the binary search (the mid values...
1. Randomized Binary Search Which are true of the randomized Binary Search algorithm? Multiple answers:You can select more than one option A) It uses a Variable-Size Decrease-and-Conquer design technique B) Its average case time complexity is Θ(log n) C) Its worst case time complexity is Θ(n) D) It can be implemented iteratively or recursively E) None of the above 2. Randomized Binary Search: Example Assume you have an array, indexed from 0 to 9, with the numbers 1 4 9...
In C++: Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. Using the code below, add your binsearch function to this C++ program. (Do...
Write and test a function in C++ that uses the binary search algorithm to search an array of sorted strings – use a do..while loop to allow user to perform multiple searches w/o terminating the program – see sample output below. Use this name array: string names[SIZE] = { "Collins, Bill", "Smith, Bart", "Allen, Jim", "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri", "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill", "James, Jean", "Weaver, Jim", "Pore, Bob",...
Java The following questions ask about tracing a binary search. To trace the binary search, list the value of first, last, and mid for each pass through the loop or method. Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target)...
Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...
What indexes will be examined as the middle element by a binary search for the target value 8 when the search is run on the following input array? Check if the input array is in sorted order. What can you say about the binary search algorithm’s result? int[] numbers = {6, 5, 8, 19, 7, 35, 22, 11, 9}; int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
What indexes will be examined as the middle element by a binary search for the target value 8 when the search is run on the following input array? Check if the input array is in sorted order. What can you say about the binary search algorithm’s result? int[] numbers = {6, 5, 8, 19, 7, 35, 22, 11, 9}; int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; (Using Java code please)