-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.java
More file actions
48 lines (45 loc) · 1.13 KB
/
day8.java
File metadata and controls
48 lines (45 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// problem 1 : https://leetcode.com/problems/move-zeroes/description/
class Solution {
public void moveZeroes(int[] nums) {
int count=0,k=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
count++;
}
else{
nums[k]=nums[i];
k=k+1;
}
}
while(count!=0){
nums[k]=0;
count=count-1;
k++;
}
}
}
TC-O(n)
SC-O(1)
approach-we will count number of 0 place at end remaining place will be filled with non zero
// problem 2- https://leetcode.com/problems/rotate-array/description/
class Solution {
public void rotate(int[] nums, int k) {
int n=nums.length,j=0,i;
int []arr=new int[n];
while(n<k){
k-=n;
}
for(i=n-k;i<n;i++){
arr[j++]=nums[i];
}
for(i=0;i<n-k;i++){
arr[j++]=nums[i];
}
for(i=0;i<n;i++){
nums[i]=arr[i];
}
}
}
TC-O(n)
SC-O(n)
approach-Rotate array by reducing k with modulus, rearranging using a temporary array, and copying back to the original.