Given an array of positive and negative integers, re-arrange it so that you have positive integers on one end and negative integers on other

后端 未结 30 2519
醉梦人生
醉梦人生 2020-12-07 07:37

I recently came across a Microsoft Interview Question for Software Engineer.

Given an array of positive and negative integers, re-arrange it so that you

30条回答
  •  [愿得一人]
    2020-12-07 08:28

    #include 
    
    using namespace std;
    
    void negativeFirst_advanced (int arr[ ], int size)
    
    {
    
        int count1 =0, count2 =0;
    
        while(count20 && arr[count2]<0)
            {       
                int temp = arr[count1];
                arr[count1] = arr[count2];
                arr[count2] = temp;
            }
    
            if (arr[count1]<0)
                count1++;
            if (arr [count2]>0)
                count2++;
    
        }
    }
    
    int main()
    {
    
            int arr[6] = {1,7,-5,9,-12,15};
            negativeFirst_advanced (arr, 6);
            cout<<"[";
            for (int i =0; i<6;i++)
                cout<

提交回复
热议问题