Problem Statement
Given a binary array, find the maximum number of consecutive 1s in this array.Link to GitHub :Code
0, 1, 2, ..., n, find the one that is missing from the array.[1,1,2],2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.Example |
input = {1, 4, 9, 9}
| output ={1, 5, 0, 0 } | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | stocks=[7,1,5,2,4] def buy_sell_once(s): maxprofit=0 #first loop for i in range(len(s)-1): for j in range(i+1,len(s)): if s[j] - s[i] > maxprofit: maxprofit=s[j] - s[i] return maxprofit m=buy_sell_once(stocks) print(m) |
1 2 3 4 5 6 7 8 9 10 11 12 | stocks =[7,1,5,2,4] def buy_sell_once_ef(s): max_profit=0 min_price=stocks[0] for price in range(len(stocks)-1): min_price = min(min_price,price) profit =(price-min_price) max_profit=max(profit,max_profit) return max_profit m=buy_sell_once(stocks) print(m) |
public static int get(int [] prices) { int maxprofit=0; //loop through the array set . We need to keep on adding the profit //We need to buy and sell before buying others for(int i=0;i<prices.length-1;i++) { //We need to make sure that selling is greater than profit if(prices[i+1] >prices[i]) { maxprofit+=prices[i+1]-prices[i]; } } return maxprofit; }
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
[-2,1,-3,4,-1,2,1,-5,4],[4,-1,2,1] has the largest sum = 6⌊ n/2 ⌋ times. I/P : 3 3 4 2 4 4 2 4 4
O/P : 4
I/P : 3 3 4 2 4 4 2 4
O/P : NONE
[1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]