Computer Science, asked by TransitionState, 11 months ago

You are given a sorted array containing both negative and positive values. Resort the array taking absolute value of negative numbers. Your complexity should be o(n)

Answers

Answered by cyberpunkdeath
0

Explanation:

reverse the array part with negative numbers then it is just merging 2 sorted array like in merge sort

ex:[-6, -5, -4, 1, 2]

step1: reverse array part with negative numbers

[-4, -5, -6, 1, 2]

step2: now perform merge operation

Answered by indiabrainly
1

Answer:

Explanation:

"// A C++ program to put positive

// numbers at even indexes (0, 2, 4,..)  

// and negative numbers at odd  

// indexes (1, 3, 5, ..)

#include <iostream>

using namespace std;  

class GFG

{     public:

   void rearrange(int [],int);

   void swap(int *,int *);

   void printArray(int [],int); };

void GFG :: swap(int *a, int *b)

{     int temp = *a;

   *a = *b;

   *b = temp; }

// A utility function to print an array

void GFG :: printArray(int arr[], int n)

{

   for (int i = 0; i < n; i++)

       cout << arr[i] << "" "";

}

// Driver Code

int main()  

{

   int arr[] = {-1, 2, -3, 4,  

                 5, 6, -7, 8, 9};

   int n = sizeof(arr) / sizeof(arr[0]);

   GFG test;

   test.rearrange(arr, n);

   test.printArray(arr, n);

   return 0;

}

"

Similar questions