Consider an array of length n containing positive and negative integers in random. Write a C++ code that rearranges the integers so that the negative integers appear before the positive integers. Your solution should use:
a. O(n^2)
b. O(n)
a) O(n2)
#include <iostream>
#include <stdio.h>
using namespace std;
void printfunc(int array[], int n) // function to print the array
{
for (int i = 0; i < n; i++)
printf("%d ", array[i]);
printf("\n");
}
void arrange(int array[], int n) // Function to Rearrange positive and negative
{
int current_element, j;
for(int i = 1; i < n; i++)
{
current_element= array[i];
if (current_element > 0) // current element is positive just continue
continue;
j = i - 1;
while (j >= 0 && array[j] > 0) //to rearrange the elements of array
{
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = current_element;
}
}
int main()
{
int array[] = { -8, -1, 3, 11, -4, -14, 6, 6, 28 };
int n = sizeof(array) / sizeof(array[0]);
printf("initial array :\t");
printfunc(array,n);
arrange(array, n);
printf("rearranged array:\t");
printfunc(array, n);
return 0;
}
b) O(n)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void printfunc(int array[], int n) // function to print the array
{
for (int i = 0; i < n; i++)
printf("%d ", array[i]);
printf("\n");
}
void arrange(int array[], int n) // function to rearrange
{
int j = 0;
for (int i = 0; i < n; i++) {
if (array[i] < 0) { // to check whether array[i] i.e., array element is negative or positive
if (i != j)
swap(array[i], array[j]); // rearange the array elements
j++;
}
}
}
int main() //main function
{
int array[] = { -8, -1, 3, 11, -4, -14, 6, 6, 28 };
int n = sizeof(array) / sizeof(array[0]);
printf("initial array :\t");
printfunc(array,n);
arrange(array, n);
printf("rearranged array:\t");
printfunc(array, n);
return 0;
}
Consider an array of length n containing positive and negative integers in random. Write a C++...
Consider an array of length n containing positive and negative integers in random order. Write C++ code that rearranges the integers so that the negative integers appear before the positive integers. Your solution should use O(n2) operations O(n) operations
Consider the array called myArray declared below that contains positive V and negative integers. Write a loop statement to count and display the number of positive integers in the array. That is, your loop should display the message "Number of positive integers is 5". int myArray[]={-1,3,-9,9,33,-4,-5,100,4,-23};
need help in this algorithm question Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...
Write a recursive C program that receives an array of positive integers and its length and returns the largest number in the array. For example: int arr[5] = {-2, 31, 4, 12, -8}; findMax(arr, ...) -> 31 int findMax ( int * arr , int p , int max ) { }
in matlab (a)Write a function to generate n random integers between -b and b. (b) Using the function you wrote in (a) generate 50 random numbers in a vector A where b = 100; (c) This array A will contain both negative, positive and possibly zero entries. Write a MATLAB script to compute the percentage of positive, negative and zero entries in the array.
Given an array A of integers, -100,000 <= A[i] <= 100,000, 1 <= 10,000 (n = size of array), some elements appear multiple times and others appear once. write a function that returns all the elements that appear multiple times in this array. (solution should be O(n) or faster) public static List<Integer> find duplicates (List<Integer> nums) { }
Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...
Write an application that does the following: (1) fill a 32-bit array with 10 pseudo-random integers between -50 and +49 ; (2) Loop through the array, displaying each value, and count the number of negative values; (3) After the loop finishes, display the count. Below is an assembly program template that you can refer, and complete the programming by filling in the codes into the specified place: Comment ! Title: Counting Array Values Description: Write an application that does the...
Please write in C# Write a method in C# int[]GetDuplicatesIndices(int[]array), which takes an array of integers and returns an array that holds the indices of all the duplicates. Example if the array has the following value 10, 5, 3, 7, 5, 11, 15, 13, 11, 6, 3, 19 then note that the value 5 has duplicate at index 4, value 3 has a duplicate at index 11, and value 7 has a duplicate at index 6. So the method should...
7. i) Let n and k be some given positive integers, and x a 1-dimensional NumPy array of length n. Write a Python code that creates the 2-dimensional NumPy array which has k columns all identical to x. You may import numpy as np Perform the test case: ne5 k-3 x = np.arange(0,1,0.2) # the output should be [[. 0. 0.] [0.2 0.2 0.2] [0.4 0.4 0.4) (0.6 0.6 0.6) [0.8 0.8 0.8]] 7. ii) Let a, b, c be...