Skip to main content

Posts

Showing posts from 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    ...

Star Light....

Star Light The sky is colored purple It’s colder a night with the breeze There are familiar memories on my mind This night is full of you again I only remember thankful memories You and I, we were all laughing together I’m afraid you’ll disappear and turn into a star I’ll keep this heart in an empty place. When I closer my eyes, I see you clearer Because I miss you more In your place where you’ve spent a long night I’ll take a break and stay with you with a weary heart and tears When my song reaches the sky Come slowly like an eternal dream Please be a star by my side When your heart reaches me Sometimes the stars in the sky that shine on me are like Warmly covering my heart Thank you for your answer When I closer my eyes, I see you clearer Because I miss you more In your place where you’ve spent a long night I’ll take a break and stay with you with a weary heart and tears When my song reaches the sky Come slowly like an eternal dream Please be a star by my side The unforgettable days ...

Selection Sort in C

Selection Sort in C Selection sort  is a simple  sorting algorithm . This  sorting algorithm  is an in-place comparison-based  algorithm  in which the list is divided into two parts, the  sorted  part at the left end and the unsorted part at the right end. Initially, the  sorted  part is empty and the unsorted part is the entire list.  The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. Following example explains the above steps: arr[] = 64 25 12 22 11 // Find the minimum element in arr[0...4] // and place it at beginning 11 25 12 22 64 // Find the minimum element in arr[1...4] // and place it at beginning of arr[1...4] 11 12 25 22 64 // Find the minimum eleme...

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! 😉

Nth fibbonacci term C program

Print the fibonacci term at nth position Input: 3  (testcases) 5 4 6 Output: 5 3 8 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,result,t;     scanf("%d",&t);     while(t){     scanf("%d",&n);     result=Fibonacci(n);     printf("%d",result);     t--;     }     return 0;      } ________________________________________________________ Related Articles: Fibonacci Pattern in C Like, Comment and Share!😉

Sorting array in descending order C program

Sorting an array in descending order  Input:  7 3 4 2 6 8 10 40 Output: 40 10 8 6 4 3 2 Solution: C #include <stdio.h> void DescSortArray(int *arr,int n){     for(int outer=0;outer<n;outer++){     int temp;     int flag=0;         for(int i=0;i<n-1-outer;i++){           if(arr[i]<arr[i+1])           {           temp=arr[i];           arr[i]=arr[i+1];           arr[i+1]=temp;           flag=1;           }         }if(flag==0)             break;     } for(int i=0;i<n;i++){         printf("%d\t",arr[i]);     } } int main() {     int n,i;//size of the array     int arr[100];     scanf("%d",&n);   ...

Star pattern (right inverted)

PATTERN1 Input: 5 Output:         *       **     ***   **** ***** Solution: C #include<stdio.h> int main() {     int n;     scanf("%d",&n);     for(int i=1;i<=n;i++,printf("\n"))     {         for(int j=n;j>i;j--)         {             printf(" ");         }         for(int j=1;j<=i;j++)             printf("*");     }     return 0; } CPP #include<iostream> using namespace std; int main() {     int n;     cin>>n;     for(int i=1;i<=n;i++)     {         for(int j=n;j>i;j--)         {             cout<<" ";         }         for(int j=1;j<=i;j++) ...