Last active
December 16, 2020 15:02
-
-
Save mrunderline/f02b86989933946e126c21d189ec2f10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class madihi { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
System.out.print("enter n = "); | |
int n = scan.nextInt(); | |
int[] a = new int[n+1]; | |
int sum1 = 0; | |
for(int i = 1 ; i < n ; i++){ | |
System.out.print("enter number " + i + " = "); | |
a[i] = scan.nextInt(); | |
sum1 = sum1 + a[i]; | |
} | |
System.out.print("enter number " + n + " = "); | |
a[n] = scan.nextInt(); | |
int sum2 = a[n]; | |
int min = Math.abs(sum1 - sum2); | |
for(int i = n-1 ; i > 1 ; i--){ | |
sum1 = sum1 - a[i]; | |
sum2 = sum2 + a[i]; | |
if( Math.abs(sum1 - sum2) < min ){ | |
min = Math.abs(sum1 - sum2); | |
} | |
} | |
System.out.println("minimum = " + min); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment