Question

Write a C function that implements the Liner Search algorithm instead of Binary Search. However, this...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//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:

Add a comment
Know the answer?
Add Answer to:
Write a C function that implements the Liner Search algorithm instead of Binary Search. However, this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    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...

  • JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized...

    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...

    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...

  • Write and test a function in C++ that uses the binary search algorithm to search an...

    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...

    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)...

  • What indexes will be examined as the middle element by a binary search for the target...

    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...

    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)

  • Language = c++ Write a program to find the number of comparisons using the binary search...

    Language = c++ Write a program to find the number of comparisons using the binary search and sequential search algorithms as follows: o Suppose list is an array of 1000 elements. o Use a random number generator to fill the list. o Use the function insertOrd to initially insert all the elements in the list. o You may use the following function to fill the list: void fill(orderedArrayListType& list) {       int seed = 47; int multiplier = 2743;                                ...

  • Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort...

    Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 Initial List 4 6 18 4 6 Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position:...

  • Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm...

    Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm should be able to sort an array like this: Input: an array that has n elements, and the values of its elements are assigned randomly, for example: Index 0 1 2 3 4 5 value 7 3 6 2 1 5 Output: an array - its first n/2 elements are sorted in ascending order and its second n/2 elements sorted in descending order. That...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT