You have an array of 10 integers

  1. Program to find largest element in an Array
  2. Check if pair with given Sum exists in Array
  3. Find a pair with the given sum in an array
  4. C++ : Read array of 10 Integers and Display – Codes Explorer
  5. Arrays


Download: You have an array of 10 integers
Size: 26.46 MB

Program to find largest element in an Array

Step 2: Initiate an integer i = 0 and repeat steps 3 to 5 till i reaches the end of the array. Step 3: Compare arr[i] with max. Step 4: If arr[i] > max, update max = arr[i]. Step 5: Increment i once. Step 6: After the iteration is over, return max as the required answer. Below is the implementation of the above approach: Output Largest in given array is 9808 Time complexity:O(N), to traverse the Array completely. Auxiliary Space: O(1), as only an extra variable is created, which will take O(1) space. Recursive Approach to find the maximum of Array: The idea is similar to the iterative approach. Here the traversal of the array is done recursively instead of an iterative loop. Algorithm: See the below section for the algorithm. Step 1: Create a recursive function. Step 2: Set an integer i = 0 to denote the current index being searched. Step 3: Return steps 4 to 7 to get the final answer. Step 4: If i is the last index, return arr[i]. Step 5: Increment i and call the recursive function for the new value of i. Step 6: Compare the maximum value returned from the recursion function with arr[i]. Output Largest in given array is 9808 Time Complexity: O(N), where N is the size of the given array. Auxiliary Space: O(N), for recursive calls Find the maximum of Array using Library Function: Most of the languages have a relevant max() type in-built function to find the maximum element, such as Below is the implementation of the above approach:

Check if pair with given Sum exists in Array

Naive Approach: The basic approach to solve this problem is by nested traversal. • Traverse the array using a loop • For each element: • Check if there exists another in the array with sum as x • Return true if yes, else continue • If no such pair is found, return false. Below is the implementation of the above approach: Output Yes Time Complexity: O(N 2), Finding pair for every element in the array of size N. Auxiliary Space: O(1) Two Sum using Sorting and Two-Pointers technique : The idea is to use the two-pointer technique. But for using the two-pointer technique, the array must be sorted. Once the array is sorted the two pointers can be taken which mark the beginning and end of the array respectively. If the sum is greater than the sum of those two elements, shift the right pointer to decrease the value of the required sum and if the sum is lesser than the required value, shift the left pointer to increase the value of the required sum. Illustration: Let an array be Now, increment ‘l’ when the sum of the pair is less than the required sum and decrement ‘r’ when the sum of the pair is more than the required sum. This is because when the sum is less than the required sum then to get the number which could increase the sum of pair, start moving from left to right(also sort the array) thus “l++” and vice versa. Initialize l = 0, r = 5 A[l] + A[r] ( -8 + 45) > 16 => decrement r. Now r = 4 A[l] + A[r] ( -8 + 10) increment l. Now l = 1 A[l] + A[r] ( 1 + 10) increment l. Now ...

Find a pair with the given sum in an array

Input: nums = [8, 7, 2, 5, 3, 1] target = 10 Output: Pair found (8, 2) or Pair found (7, 3) Input: nums = [5, 2, 6, 8, 1, 9] target = 12 Output: Pair not found There are several methods to solve this problem using brute-force, sorting, and hashing. These are discussed below: 1. Using Brute-Force A naive solution is to consider every pair in the given array and return if the desired sum is found. This approach is demonstrated below in C, Java, and Python: Output: Pair found (8, 2) The time complexity of the above solution is O(n 2) and doesn’t require any extra space, where n is the size of the input. 2. Using Sorting The idea is to low and high) that initially points to two endpoints of the array. Then reduce the search space nums[low…high] at each iteration of the loop by comparing the sum of elements present at indices low and high with the desired sum. Increment low if the sum is less than the expected sum; otherwise, decrement high if the sum is more than the desired sum. If the pair is found, return it. Following is the C++, Java, and Python implementation based on the idea: Output: Pair found (2, 8) The time complexity of the above solution is O(n.log(n)) and doesn’t require any extra space. 3. Using Hashing We can use a nums[i] into a map. We also check if difference (nums[i], target - nums[i]) already exists in the map or not. If the difference is seen before, print the pair and return. The algorithm can be implemented as follows in C++, Java, and Python:

Single

In this article You create a single-dimensional array using the int[] array = new int[5]; This array contains the elements from array[0] to array[4]. The elements of the array are initialized to the 0 for integers. Arrays can store any element type you specify, such as the following example that declares an array of strings: string[] stringArray = new string[6]; Array Initialization You can initialize the elements of an array when you declare the array. The length specifier isn't needed because it's inferred by the number of elements in the initialization list. For example: int[] array1 = new int[] ; Console.WriteLine(weekDays2[0]); Console.WriteLine(weekDays2[1]); Console.WriteLine(weekDays2[2]); Console.WriteLine(weekDays2[3]); Console.WriteLine(weekDays2[4]); Console.WriteLine(weekDays2[5]); Console.WriteLine(weekDays2[6]); /*Output: Sun Mon Tue Wed Thu Fri Sat */ See also • • • •

C++ : Read array of 10 Integers and Display – Codes Explorer

Read array of 10 integers and displaying them. “Array is the limited storage(as size mentioned) with ordered series or arrangement of same kind of data(int or float or char)” Example :Input is asked as : Enter the Array elements, Enter Values like 00,10,20,30,40,50,60,70,80,90 are stored in particular space of array. Example: – This represents that the data collection is done in one array. – This data storage array starts from a[0], 0 refers to starting starting index i.e. a[0] and continues till a[7]. – a[0],a[1],a[2],……….a[9], each address has its individual data storing capacity. Sample Code for Storing of data(10 integer’s) in an Array and displaying it: #include //Header file. using namespace std; void read(int []); //reading a data stored in an array void display(int []); //displaying the data stored in an array main() Result of the Program: Input asked: Enter the number: 00 10 20 30 40 50 60 70 80 90 Output showed: 00 10 20 30 40 50 60 70 80 90

Arrays

In this article You can store multiple variables of the same type in an array data structure. You declare an array by specifying the type of its elements. If you want the array to store elements of any type, you can specify object as its type. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from type[] arrayName; Example The following example creates single-dimensional, multidimensional, and jagged arrays: class TestArraysClass // Output: The array has 2 dimensions. See also • • • • • • • • For more information, see the