Posts

Showing posts from July, 2022
best time to sell stock   package com.company ; import java.util.* ; public class Main { public static void main (String[] args) { // best time to buy and sell stocks int arr[]={ 7 , 1 , 5 , 4 , 3 } ; int profit = 0 ; int minimum=Integer. MAX_VALUE ; for ( int i= 0 ; i<arr. length ; i++) { if (arr[i]<minimum) { minimum=arr[i] ; } else if (arr[i]-minimum>profit) { profit=arr[i]-minimum ; } } System. out .println(profit) ; } }
 chocolate distribution problem  package com.company ; import java.util.* ; public class Main { public static void main (String[] args) { // sum of subarray Scanner sc = new Scanner(System. in ) ; int arr[]={ 7 , 3 , 2 , 4 , 9 , 12 , 56 } ; for ( int i= 0 ; i<arr. length - 1 ; i++) { int smallest = i ; for ( int j=i+ 1 ; j<arr. length ; j++) { if (arr[j] < arr[smallest]) { smallest = j ; } } //swap int temp = arr[smallest] ; arr[smallest] = arr[i] ; arr[i] = temp ; } //choclate problem //{2,3,4,7,9,12,56} int m= 3 ; int n=arr. length ; for ( int l= 0 ; l<m ; l++){ System. out .println(arr[l]) ; } int diff=arr[m- 1 ]-arr[ 0 ] ; System. out .println( "diffrence of the two numbers is : " + diff) ; }
  duplicate element in array  package com.company ; import java.util.* ; public class Main { public static void main (String[] args) { // sum of subarray Scanner sc = new Scanner(System. in ) ; int a[]={- 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; for ( int i= 0 ; i<a. length ; i++){ for ( int j= 0 ; j<a. length ; j++){ if (a[i]==a[j]){ System. out .println( true ) ; break; } else { System. out .println( false ) ; break; } } } } }
 dsa  Maximum-Subarray package com.company ; import java.util.* ; public class Main { public static void main (String[] args) { // sum of subarray Scanner sc = new Scanner(System. in ) ; int a[]={- 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int sum= 0 ; int max=a[ 0 ] ; for ( int i= 0 ; i<a. length ; i++) { sum+=a[i] ; if (sum > max) { max = sum ; } else if (sum < 0 ) { max = max ; sum = 0 ; } } System. out .println(max) ; } }