Skip to main content

Posts

Showing posts from May, 2021

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 <= arr.length <= 10000 0 <= arr[i] <= 9 C++ Code :     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(in...

Squares of a Sorted Array

 Squares of a Sorted Array Given an integer array  nums  sorted in  non-decreasing  order, return  an array of  the squares of each number  sorted in non-decreasing order . Example1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example2: Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121] Constraints: 1 <= nums.length <=  10 4 -10 4  <= nums[i] <= 10 4 nums  is sorted in  non-decreasing  order. C++ Code:     vector<int> sortedSquares(vector<int>& nums) {         for(int i=0;i<nums.size();i++){             nums[i]*=nums[i];         }         sort(nums.begin(),nums.end());         return nums;     } _____________________________________________________...

Find Numbers With Even Number Of Digits in C++

 Find Numbers With Even Number Of Digits Given an array  nums  of integers, return how many of them contain an  even number  of digits. Example1: Input: nums=[12,345,2,6,7896] Output: 2 Explanation:   12 contains 2 digits (even number of digits).  345 contains 3 digits (odd number of digits).  2 contains 1 digit (odd number of digits).  6 contains 1 digit (odd number of digits).  7896 contains 4 digits (even number of digits).  Therefore only 12 and 7896 contain an even number of digits. Example2: Input: nums = [555,901,482,1771] Output: 1 Explanation: Only 1771 contains an even number of digits. Constraints: 1 <= nums.length <= 500 1 <= nums[i] <= 10^5 C++ Code:     int findNumbers(vector<int>& nums) {         int n=0,count=0;         for(int x:nums){             n=0;             while(x){ ...

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):   ...

Linked List in Python

Single Linked List Implementation in Python: What is a linked list? A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list. Below is the code for the following: Creating a linked list Inserting elements(at the beginning, at the end, at the middle, insert after an element, insert at an index, inserting a set of values together) Deleting elements ( remove by value , remove at an index) Length of the list Removing odd numbers from the 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    ...