Algorithm :
Step 1 of 4 :
Input the array elements of positive integers.
int arr[size];
for (i=0;i<size;i++)
{
cin>>arr[i];
}
Step 2 of 4 :
Input the two numbers for which the numbers between them are to be selected.
int a,b;
cin>>a>>b;
Step 3 of 4 :
Iterate through the loop and check if the element is greater than min(a,b) and less than max(a,b).
If the element satisfies this range then put that element in a new array.
int newarray[size];
k=0;
for (i=0;i<size;i++)
{
if (arr[i]>=min(a,b) && arr[i]<=max(a,b))
{
newarray[k]=arr[i];
k++;
}
}
Step 4 of 4 :
print the new array .
for(i=0;i<k;i++)
{
cout<<newarray[i];
}
=> The above algorithm iterates through the array size ( n ) times. Therefore, the number of comparisons would be n and the time complexity of the above algorithm in terms of Big-O will be O(n). An example of the above algorithm is given below. If you have any queries please comment in the comments section and kindly upvote.
#include<iostream>
using namespace std;
int main()
{
// input the size of the array
cout<<"Enter the size of the array\n";
int n;
cin>>n;
// create an array of size n
int arr[n];
// input array elements
cout<<"Enter the elements\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
// input the two numbers a and b
int a,b;
cout<<"Enter the two numbers\n";
cin>>a>>b;
// compare and put the elements in the new array
int newarray[n];
int k=0;
for(int i=0;i<n;i++)
{
if(arr[i]>=min(a,b) && arr[i]<=max(a,b))
{
newarray[k]=arr[i];
k++;
}
}
// print the new array
for(int i=0;i<k;i++)
{
cout<<newarray[i]<<" ";
}
return 0;
}
3. Chapter 7. Write an algorithm that accepts an array of positive integers and two more...
Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A of length n and a positive integer T as an input and finds the largest N < T such that N can be written as a sum of some elements of A and returns such a representation of N. The complexity of the algorithms has to be O(nT). For example, for A 3,7, 10 and T 19, the output is 17 7+10, because we...
2. Write a Marie program that accepts two positive (inputs) two integers and outputs the sum of both of the integers and all the numbers in between Write a Marie program that determines the largest of a series of positive integers provided by a user. The user will enter a -1 when she is finished. Up to 10 numbers will be provided by the user. Write a Marie program that accepts two positive integers, multiples them by repeated addition, and...
1. Write a function named “areFirstTwoTheSameAsTheLastTwo” that accepts an array of integers, its size and return true if the first two numbers in the array are the same as the last two numbers in the array. It returns false otherwise. If the array has less than 4 elements, it always returns false. For example, this array of {10, 20, 30, 40, 10, 20} or {10, 20, 10, 20} will return true but this array of {10, 20, 20, 10} or...
Parvati is given an array of integers. She is asked to return the sum of all the two-digit numbers in the array. Please help her to perform this task. Write a function: class Solution { public int solution (int[] A); } that, given an array A consisting of N integers, returns the sum of all two-digit numbers. For example, given A = [1, 1000, 80, -91], the function should return -11 (as the two-digit numbers are 80 and -91). Given...
Given an array A[1..n] of positive integers and given a number x, find if any two numbers in this array sum upto x. That is, are there i,j such that A[i]+A[j] = x? Give an O(nlogn) algorithm for this. Suppose now that A was already sorted, can you obtain O(n) algorithm?
Write a C function f such that … function f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...
8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za then anstwer-1 return answer 18 and the (a) Assume that this algorithm receives as input the numbersz-32 and corresponding sequence of integers 2 | 3 1 1 4151617| 8| 9 | 10 İ 11 İ 12 | 13 | 14|15 | 16 | 17 |...
(3) Methods That Return a Value (10 points) Write a method dominant that accepts three integers as parameters and returns true if any one of the three integers is larger than the sum of the other two integers. The integers might be passed in any order, so the largest value could be any of the three. If no value is larger than the sum of the other two, your method should return false. For example, the call of dominant (4,...
1. The following program calls the function countLarger that accepts three arguments: an integer array, an integer size that indicates how many elements are in the array, and an integer n. The function countLarger should return the number of integers in the array that are greater than the value of the argument n. Update the program to include the definition of the function countLarger. #include <iostream> using namespace std; int countLarger(int[], int, int); // Function prototype int main() const int...
2. Given an array of integers, find two numbers such that they add up to a specific target number. Input: numbers (2, 7,1 15, target-9 (10 points) Output: index-, ndex2-2 3. Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0. (10 points) 2. Given an array of integers, find two numbers such that they add up to a specific target number. Input: numbers (2, 7,1 15,...