Found 7347 Articles for C++

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

357 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

563 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

221 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

113 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

280 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

220 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

700 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

1K+ 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

C++ program to rearrange all elements of array which are multiples of x in increasing order

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

315 Views

We are given an integer type array as ‘int arr[]’ and an integer type variable as ‘x’. The task is to rearrange all the elements of an array in such a manner that they will be divisible by a given integer value ‘x’ and the arrangement should be in an increasing order.Let us see various input output scenarios for this −Input − int arr[] = {4, 24, 3, 5, 7, 22, 12, 10}, int x = 2Output − Rearrangement of all elements of array which are multiples of x 2 in decreasing order is: 4 10 3 5 7 12 22 24Explanation −we ... Read More

Recursive sum of digit in n^x, where n and x are very large in C++

Sunidhi Bansal
Updated on 02-Nov-2021 06:04:24

119 Views

We are given positive integer variables as ‘num’ and ‘x’. The task is to recursively calculate the num ^ x then add the digits of a resultant number till the single digit isn’t achieved and the resultant single digit will be the output.Let us see various input output scenarios for this −Input − int num = 2345, int x = 3Output − Recursive sum of digit in n^x, where n and x are very large are: 8Explanation − we are given positive integer values as num and x with the values as 2345 and power as 3. Firstly, calculate 2345 ^ 3 i.e. ... Read More

Advertisements