Skip to main content

Duplicate Zeroes in C++

Duplicate Zeroes

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

Note that elements beyond the length of the original array are not written.

Do the above modifications to the input array in place, do not return anything from your function.

Example 1:

Input: [1,0,2,3,0,4,5,0]

Output: null

Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]

Example 2:

Input: [1,2,3]

Output: null

Explanation: After calling your function, the input array is modified to: [1,2,3]

Note:

  1. 1 <= arr.length <= 10000
  2. 0 <= arr[i] <= 9
    void duplicateZeros(vector<int>& arr) {
        int n=arr.size();
        int newLength=n+count(arr.begin(),arr.end(),0);
        vector<int>newArray;
        int ind=0;
        for(int i=0;i<n;i++){
            newArray.push_back(arr[i]);
            if(arr[i]==0)
                newArray.push_back(0);
        }
        arr.clear();
        for(ind=0;ind<n;ind++)
        arr.push_back(newArray[ind]);
    }

_____________________________________________________________________
Related Posts:
Please Like, Comment and Share!😉

Comments

Popular posts from this blog

Fibonacci Pattern/Series C program

C Program to print fibonacci pattern till N Input: 5 (N value) Output: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Solution:   #include <stdio.h> int Fibonacci(int num){     if(num<=1)         return num;     else     return Fibonacci(num-1)+Fibonacci(num-2) ;//2,1 } int main() { int n,f=1;     scanf("%d",&n);     for(int row=1;row<=n;row++,printf("\n")){         for(int col=1;col<=row;col++){             printf("%d\t",Fibonacci(f++));         }     }     return 0; } ___________________________________________________________ Please Like, Comment and Share! 😉

Leetcode Solutions (Array101) in C++

Max Consecutive Ones Given a binary array  nums , return  the maximum number of consecutive  1 's in the array . Example1: Input: nums=[1,1,0,1,1,1] Output: 3 Explanation:   The first two digits or the last three digits are consecutive 1s.  The maximum number of consecutive 1s is 3. Example2: Input: nums=[1,0,1,1,0,1] Output: 2 Constraints: 1 <= nums.length <= 10 5 nums[i]  is either  0  or  1 . CPP Code:     int findMaxConsecutiveOnes(vector<int>& nums) {         int max=0,count=0;         for (int x: nums)         {             if(x==1){                 count++;                 if(count>max)                     max=count;             }         ...

Removing Odd numbers from a linked list

Deleting odd numbers from a linked list in python: In this program, we will see  how to remove odd numbers from a linked list in Python. This is a very interesting program where we will learn how to remove odd numbers from a linked list. This contains a very easy to understand method of making a linked list. So when you know how to use the linked list we will learn how to remove odd numbers from it. (Note that the program done here is done without using recursion for further simplicity). For removing odd number we first find out the position of the “ head “.  The head is the starting node in a linked list.   Source Code:   class Node:     def __init__(self,data=None,next=None):         self.data=data         self.next=next class SingleLinkedList:          def __init__(self):         self.head=None           def print_list(self):   ...