Sum of even and odd elements in an array

Java program for Sum of even and odd elements in an array

You will be given an array and you need to find odd numbers and add them, find even numbers and add them and print the absolute value of subtraction between those two sums of odd and even values.

Input Format
You will be taking a number as an input from STDIN which tells about the length of the array. On another line, array elements should be there with single space between them.

Sample Test Case :
Input
3
23 22 1
Output
2

Program :

import java.util.*;
public class SumofEvenOddNumbers {
      public static void main(String args[] ) throws Exception {
              Scanner sc = new Scanner(System.in);
              int n = sc.nextInt();
              int[] arr = new int[n];

              for(int i=0; i<n; i++){
                   arr[i] = sc.nextInt();
              }

             int evenSum = 0, oddSum = 0;

             for(int i=0; i<arr.length; i++){
                  if(arr[i] % 2 == 0)
                       evenSum = evenSum + arr[i]
                  else
                       oddSum = oddSum + arr[i];
             }

             System.out.print(evenSum>oddSum ? evenSum-oddSum : oddSum-evenSum);
      }
}

Output :

Enter n : 3
Enter array elements : 23   22   1
absolute value of subtraction : 2

Largest even number

You will be given an array and you are asked to find the largest even number

Input Format
you will be taking a number as an input from STDIN which tells about the length of the array. On another line, array elements should be there with single space between them.

Example :
Input : 
6
78   92   44   63   71   97
Output : 92

Program : 

import java.util.Scanner;
public class LargestEven {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n : ");
                int n = sc.nextInt();
                System.out.print("Enter numbers in array : ");
                int[] arr = new int[n];
                for(int i=0; i<n; i++){
                       arr[i] = sc.nextInt();
                }
        
                int largestEven = 0;
                for(int i=0; i<n; i++){
                if(arr[i]%2 == 0 && arr[i] > largestEven){
                       largestEven = arr[i];
                }
        }

        System.out.print("Largest even number : "+ largestEven);
   }
}

Output :
 
Enter n : 6
Enter numbers in array : 78   92    44 63   71    97
Largest even number : 92

Total number of non-decreasing numbers with n digits

A number is non-decreasing if every digit (except the first one) is greater than or equal to previous digit. For example, 223, 4455567, 899, are non-decreasing numbers.
So, given the number of digits n, you are required to find the count of total non-decreasing numbers with n digits.

Examples : 

Input : digits = 1
Output : count = 10

Input : digits = 2
Output : count = 55

Input : digits = 3
Output : count = 220

Program : 

import java.util.Scanner;

public class TotalNumberOfNonDecreasingNumbers {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of digits : ");
int noOfdigits = sc.nextInt();
/*Calculate the highest number to repeat the loop
  for example, if number of digits is 1, then we need to repeat loop until 9 
  if number of digits is 2, then we need to repeat loop until 99.*/
int n = 1;
for(int i=1; i<=noOfdigits; i++) {
n = n * 10;
}
n = n - 1;
/*loop until highest number and verify each number is non-decreasing or not ?
If yes, increase the count.*/
int count = 0;
for(int i=0; i<=n ; i++) {
if(isNonDecreasingNumber(i))
count ++;
}
System.out.println("Number of non-descreasing numbers : "+count);
}
/* First initialize the prevDigit value as -1.
* Take the last digit and compare previous digit (prevDigit) is greater than or equal to current digit (curDigit).
* If yes repeat the process, else break the loop and return false.
*/
public static boolean isNonDecreasingNumber(int n) {
boolean isNonDecreasngNum = true;
int prevDigit = -1;
while(n > 0) {
int curDigit = n % 10;
if(prevDigit == -1 || prevDigit >= curDigit)
prevDigit = curDigit;
else {
isNonDecreasngNum = false;
break;
}
n = n / 10;
}
return isNonDecreasngNum;
}
}

Output :

Enter number of digits : 1
Number of non-descreasing numbers : 10

Enter number of digits : 2
Number of non-descreasing numbers : 55

Enter number of digits : 3
Number of non-descreasing numbers : 220

Alyona and Numbers


After finishing eating her bun, Alyona came up with two integers n and m. She decided to write down two columns of integers — the first column containing integers from 1 to n and the second containing integers from 1 to m. Now the girl wants to count how many pairs of integers she can choose, one from the first column and the other from the second column, such that their sum is divisible by 5.

Formally, Alyona wants to count the number of pairs of integers (x, y) such that 1 ≤ x ≤ n, 1 ≤ y ≤ m and equals 0.

As usual, Alyona has some troubles and asks you to help.

InputThe only line of the input contains two integers n and m (1 ≤ n, m ≤ 1 000 000).

OutputPrint the only integer — the number of pairs of integers (x, y) such that 1 ≤ x ≤ n, 1 ≤ y ≤ m and (x + y) is divisible by 5.

Input
6 12
Output
14

Program:

import java.util.Scanner;
public class PairlsOfIntegers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n, m : ");
int n = sc.nextInt();
int m = sc.nextInt();

int count = 0;
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
if( (i+j)%5 == 0 )
count ++;
}
}
System.out.print(count);
}
}

Output 1 :
Enter n, m : 6   12
14

Output 2 :
Enter n, m : 11     14
31

Output 3 :
Enter n, m : 1   5
1

Berland Jacket

According to rules of the Berland fashion, a jacket should be fastened by all the brands except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not Swinging open.

You are given a jacket with n buttons. Determine if it is fastened in a right way.

Input

The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket.

The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1.

Output

In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO".

Program : 

import java.util.Scanner;

public class BerlandJacket {
public static void main(String args[] ) throws Exception {
        Scanner sc = new Scanner(System.in);
        int zerosCount = 0, onesCount = 0;

        System.out.print("Enter n : ");
        int n = sc.nextInt();
        System.out.print("Enter fastened buttons : ");
        for(int i=0; i<n; i++){
            int x = sc.nextInt();
            if(x == 1)
                onesCount ++;
            else
                zerosCount ++;
        }

        String output = "NO";
        if(n == 1 && onesCount == 1)
            output = "YES";
        else if(zerosCount == 1)
            output = "YES";
        
        System.out.print(output);
   }
}

Output 1:


Enter n : 3                                                                                                                       
Enter fastened buttons : 1   0    1                                                                                     
YES                                                                                                                                  

Output 2:


Enter n : 3                                                                                                                       
Enter fastened buttons : 1    0     0                                                                                   
NO                                                                                                                                  


Meeting of Old Friends

Today an outstanding event is going to happen in the forest  hedgehog Filya will come to his old fried Sonya!

Sonya is an owl and she sleeps during the day and stay awake from minute l1 to minute r1 inclusive. Also, during the minute k she prinks and is unavailable for Filya.Filya works a lot and he plans to visit Sonya from minute l2 to minute r2 inclusive.
Calculate the number of minutes they will be able to spend together.

Input Format
You will be given a function with five integers  l1, r1, l2, r2 and k, providing the segments of time for Sonya and Filya and the moment of time when Sonya prinks as arguments. 


Program:

public class MinutesStayTogether {

    public static void main(String args[] ) throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter l1, r1, l2 , r2 and k :");
        int l1 = sc.nextInt();
        int r1 = sc.nextInt();
        int l2 = sc.nextInt();
        int r2 = sc.nextInt();
        int k = sc.nextInt();
        System.out.print("Number of Mins Stay together : " + numberOfMinsStayTogether(l1, r1, l2, r2, k));
   }

   public static int numberOfMinsStayTogether(int l1, int r1, int l2, int r2, int k){
       int count = 0;
       if( r1 > l2 ){
           count = (r1 - l2 ) + 1;
           
           if(l2 < k && k < r1){
               count = count - 1;
           }
       }
       return count;
   }
}

Output 1 : 

Enter l1, r1, l2 , r2 and k : 1  10   9   20   1                                                                     
Number of Mins Stay together : 2                                                                                      


Output 2 : 

Enter l1, r1, l2 , r2 and k : 1   100    50    200   75                                                             
Number of Mins Stay together : 50                                                                                    

Typing Of Words / Crazy Computer

ZS the Coder is coding on a crazy computer. If you don’t type in a word for a c consecutive seconds, everything you typed disappear!

More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b - a > c, then everything on the screen disappears and after that the word you have typed appears on the screen.

For example, if c = 5 and you typed words at seconds 1, 3, 8, 14, 19, 20 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen.

You’re given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything.

Input

The first line contains two integers c and n (1 ≤ c ≤ 109, 1 ≤ n ≤ 100 000 ) — the number of words ZS the Coder typed and the crazy computer delay respectively.

The next line contains n integers t1, t2, …, tn (1 ≤ t1 < t2 < … < tn ≤ 109), where ti denotes the second when ZS the Coder typed the i-th word.


Output

Print a single positive integer, the number of words that remain on the screen after all n words was typed, in other words, at the secondtn.

Examples

Input:

5    6
1  3  8  14  19  20

Output:

3

Input:

1   6
1  3  5  7  9  10

Output:

2

Program:

import java.util.Scanner;

public class CountOfWordsOnScreen {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter c : ");
int c = sc.nextInt();
System.out.print("Enter n : ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.print("Enter time values : ");
for(int i=0; i<n; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Count of Words on Screen " +countOfWordsOnScreen(c, arr));
}
public static int countOfWordsOnScreen(int c, int[] arr) {
int count = 1;
for(int i=1; i<arr.length; i++) {
if(arr[i] - arr[i-1] <= c)
count ++;
else
count = 1;
}
return count;
}
}


Output 1:

Enter c : 5
Enter n : 6
Enter time values : 1 3 8 14 19 20
Count of Words on Screen 3

Output 2:

Enter c : 1
Enter n : 6
Enter time values : 1 3 5 7 9 10
Count of Words on Screen 2



Place in a Class


Place in a Class



Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture).
The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right.

The picture illustrates the first and the second samples.

Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right!
   

Program:

import java.util.Scanner;

public class PlaceInClass {
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n : ");
                int n = sc.nextInt();
                System.out.print("Enter m : ");
                int m = sc.nextInt();
                System.out.print("Enter k : ");
                int k = sc.nextInt();

                System.out.println(findPlaceInClass(n, m, k));
}

public static String findPlaceInClass(int n, int m, int k){
int count = -1;
char position = (k%2!=0) ? 'L' : 'R';
int lane = 0, desk = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
count = count + 2;
if(count == k || count+1 == k){
lane = i;
desk = j;
break;
}
}
}
return lane+" "+desk+" "+position;
}
}




Output:


Enter n : 2

Enter m : 4

Enter k : 4

Place : 1    2    R







Find maximum possible number of times Artem can give presents to Masha

Little Artem got n stones on his birthday and now wants to give some of them to Masha. He knows that Masha cares more about the fact of receiving the present, rather than the value of that present, so he wants to give her stones as many times as possible.

However, Masha remembers the last present she received, so Artem can't give her the same number of stones twice in a row. For example, he can give her 3 stones, then 1 stone, then again 3 stones, but he can't give her 3 stones and then again 3 stones right after that.

How many times can Artem give presents to Masha?

Input          Output
1                    1
2                    1
3                    2
4                    3
5                    3

Explanation:

Input
Output
1 1 1
2 2 1
3 1 + 2 2
4 1 + 2 + 1 3
5 1 + 3 + 1 3
6 1 + 2 + 1 + 2 4
7 1 + 2 + 1 + 2 + 1 5
8 1 + 3 + 1 + 2 + 1 5
9 1 + 2 + 1 + 2 + 1 + 2 6
10 1 + 2 + 1 + 2 + 1 + 2 + 1 7
11 1 + 3 + 1 + 2 + 1 + 2 + 1 7
12 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 8

import java.util.*;
public class MaxNumberOfGifts {
    public static void main(String args[] ) throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n : ");
        int n = sc.nextInt();

        System.out.print("Maximum possible number of times :" + maxNumberOftimes(n));
   }

   public static int maxNumberOftimes(int n){
       int count = 0;
       
       while(n != 0){
           if(n >=3){
               n = n -3;
               count = count + 2;
           }
           else if(n==1 || n==2){
               count = count + 1;
               break;
           }           
       }
       return count;
   }
}

Output 1:


Enter n : 2
Maximum possible number of times :1

Output 2:


Enter n : 4
Maximum possible number of times :3

Output 3:


Enter n : 6
Maximum possible number of times :4

Output 4:


Enter n : 12
Maximum possible number of times :8

Find the maximum number of chocolates

Little Joty has got a task to do. She has a line of n tiles indexed from 1 to n. She has to paint them in a strange pattern.

An unpainted tile should be painted Red if it's index is divisible by a and an unpainted tile should be painted Blue if it's index is divisible by b. So the tile with the number divisible by a and b can be either painted Red or Blue.

After her painting is done, she will get p chocolates for each tile that is painted Red and q chocolates for each tile that is painted Blue.
Note that she can paint tiles in any order she wants.

Given the required information, find the maximum number of chocolates Joty can get.

import java.util.Scanner;

public class Chocolates {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter n : ");
int n = sc.nextInt();
System.out.println("Enter a :");
int a = sc.nextInt();
System.out.println("Enter b:");
int b = sc.nextInt();
System.out.println("Enter p :");
int p = sc.nextInt();
System.out.println("Enter a :");
int q = sc.nextInt();     
    System.out.print("Max Chocolates :" +maxChocolates(n, a, b, p, q));
}


public static int maxChocolates(int n, int a, int b, int p, int q){
       int count = 0;
       for(int i=1; i<=n; i++){
           int maxChocs = 0;
         
           if(i % a == 0){
               maxChocs = p;
           }
           if(i % b == 0 && q > maxChocs){
               maxChocs = q;
           }
           count = count + maxChocs;
       }
        return count;
   }
}

Output 1:


Enter n : 5
Enter a : 2
Enter b : 3
Enter p : 12
Enter q : 15
Max Chocolates : 39



Output 2:


Enter n : 20
Enter a : 2
Enter b : 3
Enter p : 3
Enter q : 5
Max Chocolates : 51