Knowee
Questions
Features
Study Tools

Given an unsorted array Arr of N positive and negative numbers. Your task is to create an array of alternate positive and negative numbers without changing the relative order of positive and negative numbers.Note: Array should start with a positive number and 0 (zero) should be considered a positive element.

Question

Given an unsorted array Arr of N positive and negative numbers. Your task is to create an array of alternate positive and negative numbers without changing the relative order of positive and negative numbers.Note: Array should start with a positive number and 0 (zero) should be considered a positive element.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python solution for the problem:

def rearrange(arr, n):
    pos = []
    neg = []
    for i in range(n):
        if arr[i] >= 0:
            pos.append(arr[i])
        else:
            neg.append(arr[i])
    i = 0
    j = 0
    k = 0
    while (i < len(pos) and j < len(neg)):
        if k % 2 == 0:
            arr[k] = pos[i]
            i += 1
        else:
            arr[k] = neg[j]

This problem has been solved

Similar Questions

Jaskaran Singh is grappling with a programming task involving rearranging an array, specifically moving all negative elements to the beginning.To assist him, here's a program with two functions: rearrange(arr, n), which rearranges the array, and printArray(arr, n), a utility function to print the array elements.NoteThe order of elements is not important here.Input format :The first line of input consists of an integer N representing the size of the array.The second line of input consists of N space-separated integers arr[i], representing the array elements.Output format :The output displays the rearranged array of elements with negative elements placed at the beginning, separated by a space.Refer to the sample output for the formatting specification

Given an unsorted array Arr[] of N integers and a Key which is present in this array. You need to write a program to find the start index( index where the element is first found from left in the array ) and end index( index where the element is first found from right in the array ).If the key does not exist in the array then return -1 for both start and end index in this case.

Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.

As an array lover, your friends have given you a challenge to solve a question on arrays, so the challenge goes as - :You are given an array 'r' consisting of 'x' integers. Unfortunately, the size of 'r' is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array 'r' (in increasing order) by reversing exactly one segment of a? See definitions of segment and reversing in the notes.Input FormatThe first line of the input contains an integer x (1 ≤ x ≤ 10^5) — the size of array 'r'. The second line contains 'x' distinct space-separated integers.Constraints'x' distinct space-separated integers r[1], r[2], ..., r[x] (1 ≤ r[i] ≤ 10^9).Output FormatPrint "yes" or "no" (without quotes), depending on the answer.

ou have a list arr of all integers in the range [1, n] sorted in a strictly increasing order. Apply the following algorithm on arr:Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.Repeat the previous step again, but this time from right to left, remove the rightmost number and every other number from the remaining numbers.Keep repeating the steps again, alternating left to right and right to left, until a single number remains.Given the integer n, return the last number that remains in arr.Example 1:Input:n = 9Output:6Explanation:arr = [1, 2,3, 4,5, 6,7, 8,9] arr = [2,4, 6,8] arr = [2, 6] arr = [6]Example 2:Input:n = 1Output:1Constraints:- 1 <= n <= 109

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.