2. Here we will check for every pair of integers in array and if their sum is equal to target the output the index.
void Find( int A[], int n, int target)
{
int i,j ;
for (i=0; i< n; i++)
for (j=0; j< n; j++)
{
if (i==j)
continue; //since we do not want repeated element
else if(A[i] + A[j] ==target) //then we got two index
{ printf ("index1 = %d, index2 = %d", i,j); return; }//print index and return
else
continue; //continue till we do not find the index
}
return; //return if no such index are found
}
3. Below is the algorithm to do this. We will maintain two arrays A and B of size M and N respectively. Here A[i] will 1 if row i of matrix C will be all 0s and B[j] will be 1 if column j of matrix C will be all 0s.
ALGO(C[][] , int M, int N)
1. Set all entries of array A and array B to be 0.
2. For i = 1 to M
3.......For j= 1 to N
4............. if (C[i][j] ==0 ) //we have to make row i and column j all 0s
5....................Then Set A[i] = 1 and B[j] = 1
6. For i= 1 to M
7.......For j= 1 to N
8...........if (A[i]==1 || B[j]==1) //if either of row i or column j set to be 0
9.................C[i][j] = 0 //then set C[i][j] to be 0
10. Return;
Please comment for any clarification.
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 su...
Write a program that asks the user to input 10 integers of an array. The program then inserts a new value at position (or index) given by the user, shifting each element right and dropping off the last element. For example, in the sample input/output below, the program will insert the value 30 at index 3. All the previous numbers of the array starting from position 3 (i.e., 7 1 20 9 23 8 22 will be shifted one position...
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...
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...
4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...
Given an array of integers as declared below, write C loop to fill it up (assign, store) with the numbers 1-100 column by column, i.e. a[0][0] = 1; a[1][0] = 2;... Use only the variables below as printed do not write them again. Just write the two loops. int a [10][10]; int value = 1; int r,c;
3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....
Java question Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...
Part 2) write a C++ program that declares a 2 dimensional array: int CSIIAssign3 [10][12]; Initialize your array using a text file, user input or assign value while declaring the array, you can choose the method. Then, a. Write a loop to print first row of the array on one line, using cout. b. Write a loop to print first column of the array on one line, using cout. c. Write a loop to print first five rows of the...
6. Write a function integerdivision that takes two input numbers and divides the first one with the second one. Question 2 a) Create a vector A with all integers between 5 and 400. 4 Marks ) Given a matrix Z, how to access element of the matrix. c) What is the output of this MATLAB code? A [2,4,10,13;16,3,7,18, 8,4,9,25:3,12,15,17); length(A) size(A) Ans) Ans) 6. Write a function integerdivision that takes two input numbers and divides the first one with the...
Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A if A[0] + A[1] + ... + A[i-1] = A[i+1] + A[i+2] + ... + A[n-1]; where 0 < i < n-1. Similarly, 0 is an stability index if (A[1] + A[2] + ... + A[n-1]) = 0 and...