Found 7197 Articles for C++

Rearrange positive and negative numbers using inbuilt sort function in C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:51:10

1K+ 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 of an array are sorted using the inbuilt sort function of C++ STL as well as using recursive technique of coding and printing the result.Let us see various input output scenarios for this −Input − int arr[] = {4, 2, -1, -1, 6, -3, 0}Output − Rearrangement of positive and negative numbers using inbuilt sort function is: -3 -1 -1 0 2 4 6.Explanation − we are given ... Read More

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

397 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

334 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

485 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

706 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

437 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

218 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

421 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

281 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

850 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

Advertisements