Skip to content
Open

Pr4 #51

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions 4. Move all zeroes to end of array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
4. Move all zeroes to end of array
Easy Accuracy: 51.81% Submissions: 10335 Points: 2
Given an array arr[] of N positive integers. Push all the zero’s of the given array to the right end of the array while maitaining the order of non-zero elements.


Example 1:

Input:
N = 5
Arr[] = {3, 5, 0, 0, 4}
Output: 3 5 4 0 0
Explanation: The non-zero elements
preserve their order while the 0
elements are moved to the right.
Example 2:

Input:
N = 4
Arr[] = {0, 0, 0, 4}
Output: 4 0 0 0
Explanation: 4 is the only non-zero
element and it gets moved to the left.

Your Task:
You don't need to read input or print anything. Complete the function pushZerosToEnd() which takes the array arr[] and its size n as input parameters and modifies arr[] in-place such that all the zeroes are moved to the right.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)


Constraints:
1 ≤ N ≤ 105
0 ≤ arri ≤ 105


**/
// { Driver Code Starts
//Initial template for C++

#include<bits/stdc++.h>
using namespace std;



// } Driver Code Ends
//User function template for C++

class Solution{
public:
// nums: given vector
// return the Product vector P that hold product except self at each index
vector<long long int> productExceptSelf(vector<long long int>& nums, int n) {

//code here
}
};


// { Driver Code Starts.
int main()
{
int t; // number of test cases
cin>>t;
while(t--)
{
int n; // size of the array
cin>>n;
vector<long long int> arr(n),vec(n);

for(int i=0;i<n;i++) // input the array
{
cin>>arr[i];
}
Solution obj;
vec = obj.productExceptSelf(arr,n); // function call

for(int i=0;i<n;i++) // print the output
{
cout << vec[i] << " ";
}
cout<<endl;
}
return 0;
} // } Driver Code Ends
39 changes: 39 additions & 0 deletions C/linkedlrevRecu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
struct node
{
int x;
struct node *next;
};
struct node *head;


void reverseprint(struct node *p)
{
if(p==0)
{
return;
}
reverseprint(p->next);
printf("%d ",p->x);
}
int main()
{
struct node *one,*two,*three,*four;
one=(struct node*)malloc(sizeof(struct node));
two=(struct node*)malloc(sizeof(struct node));
three=(struct node*)malloc(sizeof(struct node));
four=(struct node*)malloc(sizeof(struct node));
scanf("%d",&(one->x));
scanf("%d",&(two->x));
scanf("%d",&(three->x));
scanf("%d",&(four->x));
one->next=two;
two->next=three;
three->next=four;
four->next=0;
head=one;
reverseprint(head);

head=four;


}
50 changes: 50 additions & 0 deletions linkedldelete.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
struct node
{
int data;
struct node *next;
};
struct node* head;
void Delete(int x)
{
int i=0;
struct node * temp=(struct node*)malloc(sizeof(struct node));
temp=head;
for(i=0;i<x-2;i++)
{temp=temp->next;

}
temp->next=temp->next->next;


}
void Print()
{
struct node* temp=head;
while (temp !=0)
{
printf("%d",temp->data);
temp=temp->next;
}
}
int main()
{
struct node* one ;

struct node *two;
struct node *three;
one=(struct node*)malloc(sizeof(struct node));
two=(struct node*)malloc(sizeof(struct node));
three=(struct node*)malloc(sizeof(struct node));
head=one;
one->data=1;
two->data=2;
three->data=3;
one->next=two;
two->next=three;
three->next=0;
int x;
scanf("%d",&x);
//printf("%d %d %d %d",one->data,one->next->data,one->next->next->data,one);
Delete(x);
Print();
}