C++ Articles - Page 174 of 719

Rearrange positive and negative numbers in O(n) time and O(1) extra space in C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:48:28

408 Views

We are given an integer type array containing both positive and negative numbers, let's say, arr[] of any given size. The task is to rearrange an array in such a manner that all positive and negative numbers should be at alternate positions and if there are extra positive or negative elements then they will be placed in the end of an array.Let us see various input output scenarios for this −Input − int arr[] = {4, 2, -1, -1, 6, -3}Output − Rearrangement of positive and negative numbers in O(n) time and O(1) extra space is: 2 - 1 6 -1 4 ... Read More

Rearrange array such that even positioned are greater than odd in C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:43:15

343 Views

We are given an integer type array containing both positive and negative numbers, let's say, arr[] of any given size. The task is to rearrange an array in such a manner that all the elements at an even position or index should be greater than the elements at an odd position or index and print the result.Let us see various input output scenarios for this −Input − int arr[] = {2, 1, 4, 3, 6, 5, 8, 7}Output − Array before Arrangement: 2 1 4 3 6 5 8 7 Rearrangement of an array such that even positioned are greater than odd ... Read More

Rearrange array such that even index elements are smaller and odd index elements are greater in C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:40:49

498 Views

We are given an integer type array containing both positive and negative numbers, let's say, arr[] of any given size. The task is to rearrange an array in such a manner that all the elements at an even position or index should be less than the elements at an odd position or index and print the result.Let us see various input output scenarios for this −Input − int arr[] = {2, 1, 4, 3, 6, 5, 8, 7}Output − Array before Arrangement: 2 1 4 3 6 5 8 7 Rearrangement of an array such that even index elements are smaller and ... Read More

Rearrange array in alternating positive & negative items with O(1) extra space in C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:38:07

723 Views

We are given an integer type array containing both positive and negative numbers, let's say, arr[] of any given size. The task is to rearrange an array in such a manner that there will be a positive number that will be surrounded by negative numbers. If there are more positive and negative numbers, then they will be arranged at the end of an array.Let us see various input output scenarios for this −Input − int arr[] = {-1, -2, -3, 1, 2, 3}Output − Array before Arrangement: -1 -2 -3 1 2 3 Rearrangement of an array in alternating positive & negative ... Read More

Rearrange an array to minimize sum of product of consecutive pair elements in C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:34:37

457 Views

We are given a positive integer type array, let's say, arr[] of any given size. The task is to rearrange an array in such a manner that when we multiply an element with its alternative element and then add all the resultant elements then it should return the minimum sum.Let us see various input output scenarios for this −Input − int arr[] = {2, 5, 1, 7, 5, 0, 1, 0}Output − Rearrangement of an array to minimize sum i.e. 7 of product of consecutive pair elements is: 7 0 5 0 5 1 2 1Explanation − we are given an integer array ... Read More

Rearrange an array such that every odd indexed element is greater than its previous in C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:31:03

230 Views

We are given a positive integer type array, let's say, arr[] of any given size. The task is to rearrange an array in such a manner that all the elements present at an odd index should value greater than the element presents at an even index and print the result.Let us see various input output scenarios for this −Input − int arr[] = {2, 1, 5, 4, 3, 7, 8}Output − Array before Arrangement: 2 1 5 4 3 7 8 Rearrangement of an array such that every odd indexed element is greater than it previous is: 1 4 2 5 3 ... Read More

Rearrange an array such that arr[i] = i in C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:27:57

443 Views

We are given a positive integer type array, let's say, arr[] of any given size such that elements in an array should value greater than 0 but less than the size of an array. The task is to rearrange an array in such a manner that if arr[i] is ‘i’, if ‘i’ is present in an array else it will set the arr[i] element with the value -1 and print the final result.Let us see various input output scenarios for this −Input − int arr[] = {0, 8, 1, 5, 4, 3, 2, 9 }Output − Rearrangement of an array such that ... Read More

Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:20:09

295 Views

We are given a positive integer type array, let's say, arr[] of any given size such that elements in an array should value greater than 0 but less than the size of an array. The task is to rearrange an array in such a manner that if arr[j] is ‘j’ then arr[j] becomes ‘i’ and print the final result.Let us see various input output scenarios for this −Input − int arr[] = {3, 4, 1, 2, 0}Outpu t− Array before Arrangement: 3 4 1 2 0 Rearrangement of an array such that arr[j] becomes i if arr[i] is j is: 4 ... Read More

Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:16:33

864 Views

We are given a positive integer type array, let's say, arr[] of any given size such that elements in an array should value greater than 0 but less than the size of an array. The task is to rearrange an array in such a manner that arr[i] becomes arr[arr[i]] within the given O(1) space only and print the final result.Let us see various input output scenarios for this −Input − int arr[] = {0 3 2 1 5 4 }Output − Array before Arrangement: 0 3 2 1 5 4 Rearrangement of an array so that arr[i] becomes arr[arr[i]] with O(1) extra ... Read More

C++ program to rearrange an array in maximum minimum form

Sunidhi Bansal
Updated on 02-Nov-2021 06:11:54

2K+ Views

We are given an integer array which can be arranged in sorted/unsorted manner. The task is to first sort the array if the values are unsorted then arrange the array in such a manner that the first element of array will be the maximum value, second will be the minimum value, third will be the second largest value, fourth will be the second minimest value and so on.Let us see various input output scenarios for this −Input − int arr[] = {7, 5, 2, 3, 4, 9, 10, 5 }Output − Array before Arrangement: 2 3 4 5 5 7 9 10 ... Read More

Advertisements