DSA Day 1 ๐ฉโ๐ป
Topic: Array
Questions Attempted : 3
Questions Successfully Completed: 2
Missing number in array
Time Complexity : O(n)
Space Complexity : O(1)
int MissingNumber(int array[], int n) {
int sum=0;
int two_sum=0;
for(int i=1;i<=n;i++){
two_sum = two_sum + i;
}
for(int i=0;i<n-1;i++){
sum = sum + array[i];
}
return two_sum-sum;
}
Peak Element
Time Complexity : O(n)
Space Complexity : O(1)
public int peakElement(int[] arr,int n)
{
if(n==1){
return 0;
}
if(arr[n-1]>=arr[n-2]){
return n-1;
}
if(arr[0]>=arr[1]){
return 0;
}
else{
for(int i=1;i<n-1;i++){
if((arr[i]>=arr[i+1]) && (arr[i]>=arr[i-1])){
return i;
}
}
}
return 0;
}
ย