The document discusses reversing arrays in C++. It provides an iterative approach that uses two indexes, start and end, to swap elements and reverse the array. The code sample implements this approach with a function called reverseArray() that takes the array, starting index, and ending index as parameters. It also contains a function to print the array and a main method to test the reverseArray() function.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
33 views
Array DS
The document discusses reversing arrays in C++. It provides an iterative approach that uses two indexes, start and end, to swap elements and reverse the array. The code sample implements this approach with a function called reverseArray() that takes the array, starting index, and ending index as parameters. It also contains a function to print the array and a main method to test the reverseArray() function.
• 1) Initialize start and end indexes as start = 0, end = n-
1 • 2) In a loop, swap arr[start] with arr[end] and change start and end as follows : • start = start +1, end = end – 1 // Iterative C++ program to reverse an array #include <bits/stdc++.h> using namespace std;
/* Function to reverse arr[] from start to
end*/ void rvereseArray(int arr[], int start, int end) { while (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } /* Utility function to print an array */ void printArray(int arr[], int size) { for (int i = 0; i < size; i++) cout << arr[i] << " ";
cout << endl;
}
/* Driver function to test above
functions */ int main() { int arr[] = {1, 2, 3, 4, 5, 6}; int n = sizeof(arr) / sizeof(arr[0]);
// To print original array Output :
printArray(arr, n); 123456 // Function calling rvereseArray(arr, 0, n-1); Reversed array is 654321 cout << "Reversed array is" << endl;
// To print the Reversed array
printArray(arr, n); Time Complexity: O(n)
return 0; } C++ Program for Largest Sum Contiguous Subarray Kadane’s Algorithm: • Initialize: • max_so_far = INT_MIN • max_ending_here = 0
• Loop for each element of the array
• (a) max_ending_here = max_ending_here + a[i] • (b) if(max_so_far < max_ending_here) • max_so_far = max_ending_here • (c) if(max_ending_here < 0) • max_ending_here = 0 • return max_so_far #include<iostream> #include<climits> return max_so_far; using namespace std; } int maxSubArraySum(int a[], int size) /*Driver program to test maxSubArraySum*/ { int main() int max_so_far = INT_MIN, max_ending_here = 0; { int a[] = {-2, -3, 4, -1, -2, 1, 5, -3}; for (int i = 0; i < size; i++) int n = sizeof(a)/sizeof(a[0]); { int max_sum = maxSubArraySum(a, n); max_ending_here = max_ending_here + a[i]; cout << "Maximum contiguous sum is " << if (max_so_far < max_ending_here) max_sum; max_so_far = max_ending_here; return 0; } if (max_ending_here < 0) max_ending_here = 0; Maximum contiguous sum is 7 } Time Complexity: O(n)