Sapling Module 1 (Feb)
Recordings Link - https://course.acciojob.com/modules
Live Doubt link - https://course.acciojob.com/live-doubts
Community Link - https://community.acciojob.com/communities/4568/group/2688/channels/
29954/forumposts
Community for Repeating batch -
https://community.acciojob.com/communities/4568/group/2688/channels/29992/forumposts
Calendar Link - https://course.acciojob.com/mycalendar
1:1 Session - https://course.acciojob.com/onevone (Talk to batch manager for enabling this)
Gym Section - https://course.acciojob.com/gym
Online Compiler link - https://www.onlinegdb.com/
Discord - https://bit.ly/acciodiscord
Orientation Recording - https://vimeo.com/795348886/76f6be607b?
embedded=true&source=vimeo_logo&owner=182753595
If you watch the recording then only 100%
Attend Open doubt sessions FOR SURE
Skill - You should try daily finding mistake in your code
Revision List (Module 1)
https://docs.google.com/document/d/1IhwsyJN7Zx-
5aBG84WMAGguEPQmMBEjtllJG6sqUOWI/edit?usp=sharing
Notes link -
https://drive.google.com/drive/folders/1rLyAmZQb_CzcO5K5iU-
uCBkVjcw4eu8E?usp=sharing
Coding Module 1 (Feb): Hello World, Variables & Data Types,
Taking Input, Conditionals, Loops,
Nested Loops, Functions, 1D Array,
Subarray, Time & Space complexity & 2D Arrays
2nd Feb
- You don't have to make any notes in the class
- I will provide the notes (They will be present in the community)
- Weekly contest is compulsory to clear the module test
Coding Module 1 (Feb): Hello World, Variables & Data Types,
Taking Input, Conditionals, Loops,
Nested Loops, Functions, 1D Array,
Subarray, Time & Space complexity & 2D Arrays
Basic grammar topics will be covered in this module
like articles, tenses, verbs etc.
Watch live classes
Live Doubts -> https://course.acciojob.com/live-doubts
Admin related -> Batch Manager
Coding/ Notes/ Coding Test --> Saksham
2nd Feb
Theory Part
Examples
Questions
- Logic
- How to think that logic
- STEP by STEP implementation
Coding Contest: 3 Questions
Test will be of 90 minutes
Target -> 20 LPA+
500+ Quality questions 3-4 months
100 Easy - 200 Medium - 200 Hard
3rd Feb (Basic of Programming)
Program
- Set of instructions using which we want computer to do something
Programming Language
- JAVA
Computer only understands binary Language
0110110101
Humans can understand English language
Programming Language is English with some set of rules
Compiler will check our program and convert it into binary
language
Computer will understand instructions in binary language
and do what is required
3rd Feb (Basic of Java program)
// Java is based upon Class and object
// public here means that class is public property it can be used
by other classes as well
// keywords are the words with special meaning - public, class,
// We will learn about functions (void, String arg[]) in next week
// We will learn what is class, what is object, what is static
(Module 3)
// Rule 1 -> Every Java program will start with a class
// Rule 2 -> Every java program is present in a file & filename
will be as same as class name
// Rule 3 -> Every program has main function so line 17 is
compulsory
public class Main // Using this we create a main class
{
// public means main function is a public property it can be
used by other functions as well
// static denotes here that main function is static
// void is a return type (Medium concept we will learn next
week)
// main is a function name
public static void main(String arg[]) // this is creating a
main function
{
}
}
3rd Feb (Welcome to Java Assignment)
public class Main
{
public static void main(String arg[])
{
System.out.println("Hello, World.");
System.out.println("Hello, Java.");
}
}
3rd Feb (Understanding System.out.println())
public class Main
{
public static void main(String arg[])
{
// System.out.println("");
// System.out.print("");
// First it will print and then it will move to the next
line
// if we write ln in System.out.print then cursor will
move to the next line
System.out.println("Hello");
System.out.println("Anything");
System.out.print("12345");
System.out.println("789");
System.out.println("0");
}
}
3rd Feb (Datatypes - 1)
public class Main
{
public static void main(String arg[])
{
// Variable is container/box which will store something
// - Name
// - Type
// - Value stored inside a variable
// Rule 5 -> Every variable will have a type.
// The type of variable is called data type
// datatype variableName = valueStoredInTheVariable;
int a = 5000;
// int can store integer values -2 * 10^9 to 2 * 10^9
// for decimal numbers we will use double or float
// double is better than float because it can store more
digits after the decimal point
double b = 4.57894561;
float c = 4.5; // With float you should always use F or f
// If we want to store a word or sentence then we use
String ""
String d = "Acciojob is good";
String e = "WORD";
String f = "z"
// char is used for storing single letter ''
char f = 'z';
}
}
N star question you should skip, we will do it using Loops (DONE on 8th
Feb)
3rd Feb (Variables Assignment - 1)
public class Main
{
public static void main(String arg[])
{
int var = 10;
// Rule 6 -> If we want to print value of a variable
// then we will not write it inside double quote
// If we want to join English sentence with the value of
// variable then we need to use + operator
System.out.println("My num is "+var);
}
}
3rd Feb (Reassigning the variable)
public class Main
{
public static void main(String arg[])
{
// When variable is created for the first time
// then only datatype is written
// Rule -> datatype variableName = valueStoredInVariable
int a = 75;
// When variable is changed with some value
// (it was already created) that time we will write the
datatype
a = 100;
System.out.println(a);
}
}
3rd Feb (Variable Assignment - 2)
public class Main
{
public static void main(String arg[])
{
// General Rule -> datatype variableName =
valueStoredInTheVariable;
float var = 10.45f;
// If we don't write F or f it will also work
// F or f you can write it with float (It is a good
practice)
var = 20.55f;
System.out.println(var);
}
}
3rd Feb (Variable Assignment - 3)
public class Main
{
public static void main(String arg[])
{
// General Rule -> datatype variableName =
valueStoredInVariable
int a = 10;
int b = 20;
// To add two integers we can use + operator
int c = a+b;
// To print value of a variable we will not
write that in double quote
System.out.println(c);
}
}
3rd Feb (Variable Assignment - 4)
public class Main
{
public static void main(String arg[])
{
int a = 1042;
int b = 7;
// / (Division symbol -> Quotient)
// % (Modulus symbol -> Remainder)
int quo = a/b; // Quotient
int rem = a%b; // Remainder
// Combining english sentence with a variable
using +
System.out.println(quo + " " + rem);
}
}
Variable Question 5 and 8 are homework questions
6th Feb (Long data type)
public class Main
{
public static void main(String arg[])
{
// with big values in long you need to write L
or l
long d = 10000000000000000l;
System.out.println(d);
}
}
6th Feb (Type casting - 1)
public class Main
{
public static void main(String arg[])
{
// System.out.println("Hello World");
// converting one datatype into another is
called type casting
// implicit type casting
// Smaller datatype to larger datatype we can
easily go
// There is no loss of data
// It is automatically performed by the compiler
int a = 500; // a is an int datatype and stores
500 value
long b = a; // automatically b is storing a
value
System.out.println(a + " " + b);
// explicit type casting
// Bigger data type to smaller datatype then we
say it is explicit type casting
// There can be loss of data
// Force the compiler to convert a value in
bigger datatype to smaller datatype
// CASE 1 when we have small long value (it can
be stored in integer)
long c = 500000L;
int d = (int)c;
System.out.println(c + " " + d);
// CASE 2 when we have large long value
// (this cannot be stored in integer and it will
overflow negative value will be printed)
// there is loss of data
long e = 500000000000000000L;
int f = (int)e;
System.out.println(e + " " + f);
// In explicit typecasting we will write the
datatype again
// We want to tell the compiler to do a
FORCEFULL conversion
// RULE -> datatype variableName2 =
(datatype)variableName1;
}
}
6th Feb (Type casting - 2)
public class Main
{
public static void main(String arg[])
{
// System.out.println("Hello World");
// converting one datatype into another is
called type casting
// Order of the datatypes -> int, float, long,
double
// implicit type casting
// Smaller datatype to larger datatype we can
easily go
// There is no loss of data
// It is automatically performed by the compiler
int a = 500; // a is an int datatype and stores
500 value
long b = a; // automatically b is storing a
value
System.out.println(a + " " + b);
// explicit type casting
// Bigger data type to smaller datatype then we
say it is explicit type casting
// There can be loss of data
// Force the compiler to convert a value in
bigger datatype to smaller datatype
// CASE 1 when we have small long value (it can
be stored in integer)
long c = 500000L;
int d = (int)c;
System.out.println(c + " " + d);
// CASE 2 when we have large long value
// (this cannot be stored in integer and it will
overflow negative value will be printed)
// there is loss of data
long e = 500000000000000000L;
int f = (int)e;
System.out.println(e + " " + f);
double x = 0.578945;
// integers cannot store the decimal part
(decimal part is lost)
int y = (int)x; // explicit type casting
System.out.println(x + " " + y);
// In explicit typecasting we will write the
datatype again
// We want to tell the compiler to do a
FORCEFULL conversion
// RULE -> datatype variableName2 =
(datatype)variableName1;
}
}
6th Feb (Variable Assignment - 5)
public class Main
{
public static void main(String arg[])
{
float v1 = 10.5F;
float v2 = 12.5f;
float v3 = 12.15f;
float average = (v1+v2+v3)/3.0f;
// EXPLICIT TYPE CASTING
// Since there is loss of data we used EXPLICIT
type casting
int avg = (int)average;
System.out.println(avg);
}
}
6th Feb (Variable Assignment - 8)
public class Main
{
public static void main(String arg[])
{
System.out.println("8 x 1 = " + 8*1);
System.out.println("8 x 2 = " + 8*2);
System.out.println("8 x 3 = " + 8*3);
System.out.println("8 x 4 = " + 8*4);
System.out.println("8 x 5 = " + 8*5);
System.out.println("8 x 6 = " + 8*6);
System.out.println("8 x 7 = " + 8*7);
System.out.println("8 x 8 = " + 8*8);
System.out.println("8 x 9 = " + 8*9);
System.out.println("8 x 10 = " + 8*10);
}
}
6th Feb (Scanner class - 1)
// We need outside classes (Scanner) for input output
purpose
// Scanner class --> It provides functionality to input
anything in java
// A package is a group of classes and subclasses
(Module 3)
// import is used to import outside classes
// RULES for inputing anything in java
// - import scanner class
// - Create object of scanner class
// - Now we have object, we can use functionality
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
// with values you will write f or F
long a1 = 147865L;
float a3 = 1.5f;
float a2 = a3;
// Creation of object of a class
// in is the name of the object
// new is creating a new object
Scanner in = new Scanner(System.in);
int a = in.nextInt(); // input a normal integer
number
long y = in.nextLong(); // input a normal
integer number and store in long
double b = in.nextDouble(); // input a decimal
number
float c = in.nextFloat(); // input a decimal
number and store in float
String s = in.nextLine(); // input a single
sentence
String f = in.next(); // input a single word
}
}
6th Feb (Scanner Class Rules)
// We need outside classes (Scanner) for input output
purpose
// Scanner class --> It provides functionality to input
anything in java
// A package is a group of classes and subclasses
(Module 3)
// import is used to import outside classes
// RULES for inputing anything in java
// - import scanner class
// - Create object of scanner class
// - Now we have object, we can use functionality
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
// Creation of object of a class
// in is the name of the object
// new is creating a new object
Scanner in = new Scanner(System.in);
// If we write a program that takes input then
we should always provide input
int a = in.nextInt();
System.out.println(a);
}
}
6th Feb (Scanner Class - 2)
// We need outside classes (Scanner) for input output
purpose
// Scanner class --> It provides functionality to input
anything in java
// A package is a group of classes and subclasses
(Module 3)
// import is used to import outside classes
// RULES for inputing anything in java
// - import scanner class
// - Create object of scanner class
// - Now we have object, we can use functionality
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
// Creation of object of a class
// in is the name of the object
// new is creating a new object
Scanner in = new Scanner(System.in);
// Implicit type casting (smaller to bigger)
int a = 7;
double c = a;
System.out.println(c);
// If we write a program that takes input then
we should always provide input
double b = in.nextDouble();
System.out.println(b);
}
}
6th Feb (Scanner Class - 3)
// We need outside classes (Scanner) for input output
purpose
// Scanner class --> It provides functionality to input
anything in java
// A package is a group of classes and subclasses
(Module 3)
// import is used to import outside classes
// RULES for inputing anything in java
// - import scanner class
// - Create object of scanner class
// - Now we have object, we can use functionality
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
// Creation of object of a class
// in is the name of the object
// new is creating a new object
Scanner in = new Scanner(System.in);
String s = in.next();
System.out.println(s);
}
}
6th Feb (Input/ output in java)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
6th Feb (Circle - Perimeter)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
int pi = 3;
Scanner in = new Scanner(System.in);
int radius = in.nextInt();
int area = pi * radius * radius;
int perimeter = 2 * pi * radius;
System.out.println(area);
System.out.println(perimeter);
}
}
6th Feb (Mathematical Operations assignment)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
}
}
6th Feb (Agent Binod Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println("Hi my name is Agent " + s);
}
}
6th Feb (Celsius to Fahrenheit)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int tempInCelcius = in.nextInt();
int tempInFeh = (tempInCelcius*9)/5 + 32;
System.out.println(tempInFeh);
}
}
8th Feb (Boolean datatype)
public class Main
{
public static void main(String[] args)
{
int a = 7;
char z = 'y'; // single letter (single qoute)
double b = 7.8;
long c = 5000000000000000L;
float d = 7.48F;
// boolean datatype can store only two thing
true or false
boolean e = true;
String s = "true is an English sentence"; //
(double quotes)
}
}
8th Feb (Basic operators - 1)
public class Main
{
public static void main(String[] args)
{
// this will assign some value
int a = 5;
int b = 5;
int x = a/b; // / gives us quotient
int z = a%b; // % it give us -> remainder
boolean c = a>b;
System.out.println(c);
// It will check if first number is less than
second number
boolean d = a<b;
System.out.println(d);
// It will check if first number is less than
or equal to second number or not
boolean e = a<=b;
System.out.println(e);
boolean f = a>=b;
System.out.println(f);
// Equal to operator
// It will check if first number is exactly
equal with second number then it gives true
boolean g = a==b;
System.out.println(g);
// Not equal to operator
// It will check if first number is different
from second number then it gives true
boolean h = a!=b;
System.out.println(h);
}
}
8th Feb (Basic operators for conditions - 2)
public class Main
{
public static void main(String[] args)
{
// this will assign some value
int a = 5;
int b = 5;
int x = a/b; // / gives us quotient
int z = a%b; // % it give us -> remainder
boolean c = a>b;
System.out.println(c);
// It will check if first number is less than
second number
boolean d = a<b;
System.out.println(d);
// It will check if first number is less than
or equal to second number or not
boolean e = a<=b;
System.out.println(e);
boolean f = a>=b;
System.out.println(f);
// Equal to operator
// It will check if first number is exactly
equal with second number then it gives true
boolean g = a==b;
System.out.println(g);
// Not equal to operator
// It will check if first number is different
from second number then it gives true
boolean h = a!=b;
System.out.println(h);
// && -> All conditions connected using AND
operator need to be true for final outcome to be true
boolean j = (5!=8 && 8>3 && 3>1);
System.out.println(j);
// OR operator -> Either of the condition is
true then final output will be true
boolean k = (5!=8 || 3<8);
System.out.println(k);
// ! not operator -> Reverse the outcome
boolean l = !(!(5>3) || (3>1 && 3!=1));
System.out.println(l);
}
}
8th Feb (If conditionals - 1)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
// Even numbers -> remainder with 2 as 0
(divisible by 2) (2,4,6,8,10,12)
// odd numbers -> numbers are not divisible by
2 (1,3,5,7,9,11,13)
// basic rule of writing conditianal statement
// if (condition)
// {
// there will no semicolon after the
condition bracket
//if condition is true then all lines
written inside bracket they will get executed
//if condition is false then all lines
written inside bracket will get skipped
//any number of statements
// }
if(a==8)
{
System.out.println("Number is 7");
System.out.println("Anything");
}
}
}
8th Feb (If conditionals - 2)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
// Even numbers -> remainder with 2 as 0
(divisible by 2) (2,4,6,8,10,12)
// odd numbers -> numbers are not divisible by
2 (1,3,5,7,9,11,13)
// basic rule of writing conditianal statement
// if (condition)
// {
// there will no semicolon after the
condition bracket
//if condition is true then all lines
written inside bracket they will get executed
//if condition is false then all lines
written inside bracket will get skipped
//any number of statements
// }
// if condition is false and else is present the
all lines written inside else will run
// else is always written with if
if(a%2 == 0)
{
System.out.println("Even");
}
else
{
System.out.println("Odd");
System.out.println("Anything");
}
}
}
8th Feb (Basic if else - 1)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
if(a%2==0 && a%3==0)
{
System.out.println("Divisible by both 2 and
3");
}
else
{
System.out.println("Not Divisible by both 2
and 3");
}
}
}
8th Feb (Basic if else - 2)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
// shift + Below backspace key
if(a%2==0 || a%3==0)
{
System.out.println("Divisible by either 2
or 3");
}
else
{
System.out.println("Not Divisible by both 2
and 3");
}
}
}
8th Feb (Conditionals Problem - 1 Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n==28)
{
System.out.println("i am young");
}
else
{
System.out.println("i am not young");
}
}
}
8th Feb (Conditionals Problem - 2 Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n<30)
{
System.out.println("less important");
}
else
{
System.out.println("more important");
}
}
}
8th Feb (Conditional Problem - 3 Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n>1) // (n = 2,3,4,5,6,7,8,9...........)
{
System.out.println("You entered more");
}
else // n = (-ve,0,1)
{
System.out.println("You entered less");
}
}
}
8th Feb (Conditional Problem 5)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n%6==0)
{
System.out.println("Divisible");
}
else
{
System.out.println("Not divisible");
}
}
}
8th Feb (Verify Cube Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// int -> -2*10^9 to 2*10^9
// long -> -10^18 to 10^18
// a and b is upto 10^5 and since we calculate
a^3, b^3
// we will use long to prevent overflow
long a = in.nextLong();
long b = in.nextLong();
long leftHandSide = (a+b)*(a+b)*(a+b);
long rightHandSide = a*a*a + b*b*b + 3*a*a*b +
3*a*b*b;
System.out.println(leftHandSide);
System.out.println(rightHandSide);
if(leftHandSide==rightHandSide)
{
System.out.println("VERIFIED");
}
else
{
System.out.println("NOT VERIFIED");
}
}
}
8th Feb (Number of days Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int month = in.nextInt();
// if(condition1)
// {
// Statement1
// }
// else if(condition2)
// {
// Statement2
// }
// else
// {
// Statement3
// }
// Either of the month is this matching month
then we use OR
if(month==1 || month==3 || month==5 || month==7
|| month==8 || month==10 || month==12)
{
System.out.println("31");
}
else if(month==2)
{
System.out.println("28");
}
else
{
System.out.println("30");
}
}
}
8th Feb (Celcius to Feh - 2 Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
double tempInCel = in.nextDouble();
double tempInFeh = (tempInCel*9.0)/5.0 + 32.0;
// We want certain number of digits after the
decimal point
// printf is a function which helps us to print
certain number of digits after decimal point
// (number of digits we want let's say 5) -->
"%.5f"
System.out.printf("%.6f" , tempInFeh);
//
System.out.println(String.format("%.6f",tempInFeh));
}
}
9th Feb (Conditional Problem 6)
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
if(a%2!=0 && b%2!=0)
{
System.out.println("we are odd");
}
else
{
System.out.println("we are simple");
}
}
}
9th Feb (Quadrants Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = in.nextInt();
// both x and y are non zero
if(x>0 && y>0)
{
System.out.println("1");
}
else if(x<0 && y>0)
{
System.out.println("2");
}
else if(x<0 && y<0)
{
System.out.println("3");
}
else
{
System.out.println("4");
}
}
}
9th Feb (Nested If Example)
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
// If we use if inside another if then it is
called nested if
int a = 10;
if(a%2 == 0)
{
if(a%5 == 0)
{
System.out.println("Number is divisble
by both 5 and 2");
}
else if(n%4 == 0)
{
System.out.println("Number is divisble
by 2 and 4 but not 5");
}
}
}
}
9th Feb (Nested If )
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
// If we use if inside another if then it is
called nested if
int a = 30;
if(a%2 == 0)
{
if(a%5 == 0)
{
System.out.println("Number is divisble
by both 5 and 2");
}
else if(a%7 == 0)
{
System.out.println("Number is divisble
by 2 and 7 but not 5");
}
else
{
System.out.println("Number is divisble
by 2 but not 7 and 5");
}
}
else
{
System.out.println("Number is not divisble
by 2");
}
}
}
9th Feb (ASCII values in Java)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
// char, int, float, long, double
// Every character in java has a integer value
associated with it
// That integer value is called ASCII value
char a = 'a';
int b = a; //
// 'A' - 'Z' --> 65 - 90
// 'a' - 'z' --> 97 - 122
// '0' - '9' --> 48 - 57
// 'A' --> 65
// 'B' --> 66
// 'C' --> 67
// 'D' --> 68
// 'E' --> 69
// 'F' --> 70 so on
// 'Z' --> 90
// 'a' --> 97
// 'b' --> 98
// 'c' --> 99
// 'd' --> 100 so on
// 'z' --> 122
// char is a smaller datatype, integer is
bigger as compared to character
// smaller to bigger --> IMPLICIT conversion
System.out.println(a);
System.out.println(b);
// larger to smaller --> EXPLICIT TYPE CASTING
int c = 97;
char d = (char)c;
System.out.println(c);
System.out.println(d);
}
}
9th Feb (Input a character in JAVA)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// int n = in.nextInt();
// double a = in.nextDouble();
// String s = in.nextLine(); (TO input SENTENCE
(multiple words))
// String word = in.next(); (TO input word (SINGLE
WORD))
// boolean f = in.nextBoolean();
// float r = in.nextFloat();
// charAt(0) --> This is a string function
// We will study this in strings LATER ON in
module 2
// This functionality will find out what is the
first character from the string
// charAt(1), charAt(4) are all WRONG, DON'T
write that
// To input a character in java we need to use
this line -> in.next().charAt(0)
char c = in.next().charAt(0); // (TO input
single letter)
System.out.println(c);
}
}
9th Feb (Which Case Assignments)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
char c = in.next().charAt(0);
// implicit type casting (WATCH 6th FEB
Recording)
int a = c;
// 'A' - 'Z' --> 65 - 90 (Upper case letters)
// 'a' - 'z' --> 97 - 122 (Lower case letters)
if(a>=65 && a<=90)
{
System.out.println("1"); // UPPER CASE
LETTER
}
else if(a>=97 && a<=122)
{
System.out.println("0"); // lower CASE
LETTER
}
else
{
System.out.println("-1");
}
}
}
9th Feb (Which Case Assignment Approach - 2)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
char c = in.next().charAt(0);
// 'A' - 'Z' --> 65 - 90 (Upper case letters)
// 'a' - 'z' --> 97 - 122 (Lower case letters)
if(c>='A' && c<='Z')
{
System.out.println("1"); // UPPER CASE
LETTER
}
else if(c>='a' && c<='z')
{
System.out.println("0"); // lower CASE
LETTER
}
else
{
System.out.println("-1");
}
}
}
9th Feb (Leap year Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String[] arg)
{
Scanner in = new Scanner(System.in);
int year = in.nextInt();
if(year%4 == 0)
{
if(year%100 == 0)
{
if(year%400 == 0)
{
// 2000, 1600, 2400 (They are leap
years)
System.out.println("1");
}
else // they are not divisible by 400
{
// 1700, 1900
System.out.println("0");
}
}
else // they are not divisible by 100
{
// 2004, 2016, 2012, 2008 (They are
leap year)
System.out.println("1");
}
}
else // they are not divisible by 4
{
// 1997,2003,2007,2011 (they are not leap
years)
System.out.println("0");
}
}
}
9th Feb (Big light Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String[] arg)
{
Scanner in = new Scanner(System.in);
int h1 = in.nextInt();
int h2 = in.nextInt();
int v1 = in.nextInt();
int v2 = in.nextInt();
// time = (h1-h2)/(v2-v1)
if(v1!=v2)
{
// At the end of each second Doraemon check
if their heights are equal or not.
// That is why time should be integer
// time is integer as per the formula
if((h1-h2)%(v2-v1) == 0)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
else
{
// if speeds are same, then time will be
infinity according to the formula
System.out.println("false");
}
}
}
9th Feb (Contest details)
Contest limit - Sat 12 PM to Sun 12 PM
90 minutes (3 coding questions)
Sunday class timing - 1 PM afternoon
Contest is COMPULSORY
10th Feb (Grading System Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int marks = in.nextInt();
// If there multiple condition which are
independent then we use if-else if
// If conditions depend upon each other then we
can say we will nested if
if(marks>90)
{
System.out.println("Excellent");
}
else if(marks>80 && marks<=90)
{
System.out.println("Good");
}
else if(marks>70 && marks<=80)
{
System.out.println("Fair");
}
else if(marks>60 && marks<=70)
{
System.out.println("Meets Expectations");
}
else
{
System.out.println("Below Expectations");
}
}
}
10th Feb (Maximum of two numbers)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
if(a>b)
{
System.out.println("First number is
larger");
}
else if(b>a)
{
System.out.println("Second number is
larger");
}
else
{
System.out.println("Number are equal");
}
}
}
10th Feb (Which Angle Triangle Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// STEP 1 (Input three number)
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
// STEP 2 We need to find out the maximum side
// If we don't initialise with some value then
program will throw a error
int maximumSide = 0;
// maximumSide = a, maximumSide = b, maximumSide
= c (Either of them is correct)
if(a>b && a>c)
{
// For a to be the maximum side
maximumSide = a;
}
else if(b>a && b>c)
{
// For b to be the maximum side
maximumSide = b;
}
else
{
maximumSide = c;
}
// acute angle triangle
// A triangle is acute-angled, if twice the
square of the largest side is less than the sum of
squares of all the sides.
// STEP 3
if(2*maximumSide*maximumSide < (a*a + b*b +
c*c))
{
// for acute angle triangle we need to print
1
System.out.println("1");
}
else if(2*maximumSide*maximumSide > (a*a + b*b +
c*c))
{
// for obtuse angle triangle
System.out.println("3");
}
else
{
// for right angle triangle
System.out.println("2");
}
}
}
10th Feb (Details of FIRST Contest)
PRACTICE Contest
- SAT 12PM to SUN 12PM
- COMPULSORY
- If you get 0 marks then nothing will happen
- You will learn from your mistakes
- 3 coding question
- For every question spend 5 mins to understand it
- Delete the entire code and write code by yourself
- No negative marking SO Submit all 3 questions
- 90 minutes
- Easy questions
- Confidence is the key
- 2 questions (conditionals, Datatype, Scanner class,
print)
- 1 easy simple question on loops approx
10th Feb (For loop - 1)
public class Main
{
public static void main(String arg[])
{
// If we want to do something multiple times
// If we want to repeat something some number of
times
// We will use loops in question
// If we want to print Hello multiple times then
we use loops
// 3 Types of loops
// - For loops
// - While loop
// - Do while loop
// for(initialization statement; test condition;
update statement)
// {
// anything which need to be done
multiple times
// }
// initialization statement -> It will
initialize the variable which will count how many times
loop ran
// test condition -> To stop a loop we write
test conditon
// update statement -> Increase/Decrease the
variable which counts number of times loop ran
// STEP 1: i=1, 1<=5 (True), Hello will be
printed, i will be updated = 2
// STEP 2: i=2, 2<=5 (True), Hello will be
printed, i will be updated = 3
// STEP 3: i=3, 3<=5 (True), Hello will be
printed, i will be updated = 4
// STEP 4: i=4, 4<=5 (True), Hello will be
printed, i will be updated = 5
// STEP 5: i=5, 5<=5 (True), Hello will be
printed, i will be updated = 6
// STEP 6: i=6, 6<=5 (False), The loop will stop
for(int i=1; i<=5; i=i+1)
{
System.out.println("Hello");
}
// STEP 1: i=1, 1<=5 (True), Hello will be
printed, i will be updated = 3
// STEP 2: i=3, 3<=5 (True), Hello will be
printed, i will be updated = 5
// STEP 3: i=5, 5<=5 (True), Hello will be
printed, i will be updated = 7
// STEP 4: i=7, 7<=5 (False), The loop will stop
for(int i=1; i<=5; i=i+2)
{
System.out.println("Hello");
}
// STEP 1: i=1, 1<5 (True), Hello will be
printed, i will be updated = 2
// STEP 2: i=2, 2<5 (True), Hello will be
printed, i will be updated = 3
// STEP 3: i=3, 3<5 (True), Hello will be
printed, i will be updated = 4
// STEP 4: i=4, 4<5 (True), Hello will be
printed, i will be updated = 5
// STEP 5: i=5, 5<5 (false), The loop will stop
for(int i=1; i<5; i=i+1)
{
System.out.println("Hello");
}
// STEP 1: i=0, 0<5 (True), Hello will be
printed, i will be updated = 1
// STEP 2: i=1, 1<5 (True), Hello will be
printed, i will be updated = 2
// STEP 3: i=2, 2<5 (True), Hello will be
printed, i will be updated = 3
// STEP 4: i=3, 3<5 (True), Hello will be
printed, i will be updated = 4
// STEP 5: i=4, 4<5 (True), Hello will be
printed, i will be updated = 5
// STEP 6: i=5, 5<5 (False), The loop will stop
for(int i=0; i<5; i=i+1)
{
System.out.println("Hello");
}
// STEP 1: i=-2, -2<=2 (True), Hello will be
printed, i will be updated = 1
// STEP 2: i=1, 1<=2 (True), Hello will be
printed, i will be updated = 4
// STEP 3: i=4, 4<=2 (False), the loop will stop
for(int i=-2;i<=2;i=i+3)
{
System.out.println("Hello");
}
// STEP 1: i=5, 5>=1 (True), Hello will be
printed, i will be updated = 4
// STEP 2: i=4, 4>=1 (True), Hello will be
printed, i will be updated = 3
// STEP 3: i=3, 3>=1 (True), Hello will be
printed, i will be updated = 2
// STEP 4: i=2, 2>=1 (True), Hello will be
printed, i will be updated = 1
// STEP 5: i=1, 1>=1 (True), Hello will be
printed, i will be updated = 0
// STEP 6: i=0, 0>=1 (false), The loop will stop
for(int i=5; i>=1; i=i-1)
{
System.out.println("Hello");
}
// The test condition it will never become
false, loop will never stop
// Infinite Loop -> THe loops that don't stop
// We should never write infinite loop in our
program
// Hello will be printed infinite number of
times
for(int i=5; i<10; i=i-1)
{
System.out.println("Hello");
}
}
}
10th Feb (N star Question)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// It will print all stars horizontally
// let's suppose n = 3
// STEP 1: i=1, 1<=3 (True), * will be printed,
i will be updated to 2
// STEP 2: i=2, 2<=3 (True), * will be printed,
i will be updated to 3
// STEP 3: i=3, 3<=3 (True), * will be printed,
i will be updated to 4
// STEP 4: i=4, 4<=3 (False), The loop will stop
for(int i=1; i<=n; i=i+1)
{
System.out.print("* ");
}
// We need to move cursor to the next line
System.out.println();
// It will print all stars vertically
for(int i=1; i<=n; i=i+1)
{
System.out.println("*");
}
}
}
10th Feb (Power Question Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
long a = in.nextLong();
long b = in.nextLong();
long power = 1L;
// a = 2, b = 5
// STEP 1: i=1, 1<=5 (True), power = 1*2 = 2, i
will be updated to 2
// STEP 2: i=2, 2<=5 (True), power = 2*2 = 4, i
will be updated to 3
// STEP 3: i=3, 3<=5 (True), power = 4*2 = 8, i
will be updated to 4
// STEP 4: i=4, 4<=5 (True), power = 8*2 = 16, i
will be updated to 5
// STEP 5: i=5, 5<=5 (True), power = 16*2 = 32,
i will be updated to 6
for(int i=1; i<=b; i=i+1)
{
power = power * a;
}
// This needs to be done 1 time so we are
writing outside the loop
System.out.println(power);
}
}
10th Feb (Sum of Natural Number)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// you can take here sum as long also
long sum = 0L;
for(int i=1; i<=n; i=i+1)
{
sum = sum + i;
}
// we want to print it only one time
System.out.println(sum);
}
}
10th Feb (While Loops)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// for(initialization statement; test condition;
update statement)
// {
// anything which we want to do multiple
times
// }
// ANY question which involves digits you should
always use while loop
// initialization statement
// while(test condition)
// {
// anything which we want to do multiple
times
// update statement
// }
// STEP 1: i = 1, 1<=5 (True), Hello will be
printed, i will be updated to 2
// STEP 2: i = 2, 2<=5 (True), Hello will be
printed, i will be updated to 3
// STEP 3: i = 3, 3<=5 (True), Hello will be
printed, i will be updated to 3
// STEP 4: i = 4, 4<=5 (True), Hello will be
printed, i will be updated to 4
// STEP 5: i = 5, 5<=5 (True), Hello will be
printed, i will be updated to 5
// STEP 6: i = 6, 6<=5 (False), The loop will
stop
int i = 1;
while(i<=5)
{
System.out.println("Hello");
i = i+1;
}
// STEP 1: j = 1, 1<=5 (True), Hello will be
printed, i will be updated to 3
// STEP 2: j = 3, 3<=5 (True), Hello will be
printed, i will be updated to 5
// STEP 3: j = 5, 5<=5 (True), Hello will be
printed, i will be updated to 7
// STEP 4: j = 7, 7<=5 (False), The loop will
stop
int j = 1;
while(j<=5)
{
System.out.println("Hello");
j = j+2;
}
// Test condition will always be true, the loop
will never ever stop
// Infinite loop
int k = 1;
while(k<5)
{
System.out.println("Hello");
k=k-1;
}
int a = 5;
while(a>=1)
{
System.out.println("Hello");
a=a-1;
}
}
}
10th Feb (Modulus and Division Concept WILL COME IN Module Test)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = 1085;
System.out.println(n%10);
n = n/10;
System.out.println(n%10);
n = n/10;
System.out.println(n%10);
n = n/10;
System.out.println(n%10);
}
}
12th Feb(Contest questions)
1. https://course.acciojob.com/idle?question=7b29040d-43e4-47b2-8baf-
f702dabb47ee
2. https://course.acciojob.com/idle?question=9e07724e-2774-4344-83bf-
5027a90bd708
3. https://course.acciojob.com/idle?question=8f1ba066-ae9e-4a57-a8df-
c984942c0d98
12th Feb (BlackBoard Question)
// STEP 1 Input a character in JAVA
// STEP 2 How to find out character is vowel or
consonant
// STEP 3 Character is upper case or lower case
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
char c = in.next().charAt(0);
// implicit type casting (WATCH 6th FEB
Recording)
int a = c;
// vowels -> a, e, i, o, u
if(c=='a' || c=='e' || c=='i' || c=='o' ||
c=='u' || c=='A' || c=='E' || c=='O' || c=='U' ||
c=='I')
{
// 'A' - 'Z' --> 65 - 90 (Upper case letters)
// 'a' - 'z' --> 97 - 122 (Lower case letters)
if(a>=65 && a<=90) // uppercase or capital
letter
{
System.out.println("Capital Vowel");
}
else // lowercase
{
System.out.println("Lower Vowel");
}
}
else // Consonant
{
// 'A' - 'Z' --> 65 - 90 (Upper case letters)
// 'a' - 'z' --> 97 - 122 (Lower case letters)
if(c>='A' && c<='Z') // uppercase or capital
letter
{
System.out.println("Capital Consonant");
}
else // lowercase
{
System.out.println("Lower Consonant");
}
}
}
}
12th Feb (Village Attacked)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// Remainder -> % (it will find out the
Remainder)
// STEP 1 -> Input a number
// STEP 2 -> Condition with OR operator |
int n = in.nextInt();
if(n%2 == 0 || n%3 == 0)
{
System.out.println("Attacked");
}
else
{
System.out.println("Not Attacked");
}
}
}
12th Feb (Open Sesame)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// STEP 1 -> Input 2 numbers
// STEP 2 -> Run a loop why ? (One point to
another point)
// STEP 3 -> 1 to minimum number (smaller
number)
// STEP 4 -> condition to find out whether
current number divides both the numbers
int a = in.nextInt();
int b = in.nextInt();
// int min = 1;
// if(a<b)
// {
// min = a;
// }
// else
// {
// min = b;
// }
int min = b;
if(a<b)
{
min = a;
}
int gcd = 0;
// a = 3, b = 6
// STEP 1: i = 1, 1<=3 (True), 1 can divide both
3 and 6, gcd = 1, i will updated to 2
// STEP 2: i = 2, 2<=3 (True), 2 cannot divide
both 3 and 6, gcd = 1, i will updated to 3
// STEP 3: i = 3, 3<=3 (True), 3 can divide both
3 and 6, gcd = 3, i will updated to 4
// STEP 4: i = 4, 4<=3 (False), The loop will
stop
for(int i=1; i<=min; i=i+1)
{
if(a%i==0 && b%i==0)
{
gcd = i;
}
}
// GCD needs to printed only one time, that's
why we write it outside the loop
System.out.println(gcd);
}
}
12th Feb (Link to PDF)
https://drive.google.com/drive/folders/1rLyAmZQb_CzcO5K5iU-uCBkVjcw4eu8E?
usp=sharing`
13th Feb (HCF)
HCF of two Numbers is as same as 12th Feb 3rd question Open sesame
13th Feb (Prime Number)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// If we want to store only two things in form
of yes or no
boolean isPrime = true; // It will two values
// Assumption -> All numbers are prime
// isPrime is true this means n is prime
// isPrime is false this means n is not prime
// STEP 1 -> Write a loop 2 to n-1
// STEP 1: i=2, 2<=24 (True), (n%i==0) (False),
i will be updated be 3
// STEP 2: i=3, 3<=24 (True), (n%i==0) (False),
i will be updated be 4
// STEP 3: i=4, 4<=24 (True), (n%i==0) (False),
i will be updated be 5
// STEP 4: i=5, 5<=24 (True), (n%i==0) (True) (n
is not prime)
// we can n-1 to sqrt(n) (LATER on we will
discuss this)
for(int i=2; i<=(n-1); i=i+1)
{
if(n%i==0)
{
// if there is any divisor for n then we
will make the number as not prime
isPrime = false;
}
}
// Since we want to print only one time, we will
write it outside the loop
if(isPrime == true)
{
System.out.println(n + " is a prime
number");
}
else
{
System.out.println(n + " is not a prime
number");
}
}
}
13th Feb (Odd numbers Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// USER will always enter a odd number at last
// If user enters a even number then he will
again give us a number
// If user enters a odd number then user will
not give us any more number
// It is capable of stopping the loop
// break;
int count = 0;
// INPUT by user -> 4 8 7
// STEP 1 i=1, True, n = 4, count = 0 + 1 = 1,
(n%2 != 0) false, we will continue
// STEP 2 i=2, True, n = 8, count = 1 + 1 = 2,
(n%2 != 0) false, we will continue
// STEP 3 i=3, True, n = 7, count = 2 + 1 = 3,
(n%2 != 0) true, we will STOP
for(int i=1; i>0; i=i+1) // infinite loop and
test condition is always true
{
// Since we require to take input multiple
times
int n = in.nextInt();
count = count + 1;
if(n%2 != 0)
{
// we should STOP (how can we stop)
break;
}
}
// PRINT total number given by the user
System.out.println(count);
}
}
13th Feb (Reverse Digits of a number)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// STEP 1 (Find out last digit from the original
number)
// STEP 2 (We will store last digit at the end
of reversed number )
// STEP 3 (We will remove last digit from
original number)
// Since these steps need to be done multiple
times we will use a LOOP
int n = in.nextInt();
int reversedNumber = 0;
// STEP 1) n=12345, lastDigit = n%10 = 5,
reversedNumber = 5, n = 1234
// STEP 2) n=1234, lastDigit = n%10 = 4,
reversedNumber = 5*10 + 4 = 54, n = 123
// STEP 3) n=123, lastDigit = n%10 = 3,
reversedNumber = 54*10 + 3 = 543, n = 12
// STEP 4) n=12, lastDigit = n%10 = 2,
reversedNumber = 543*10 + 2 = 5432, n = 1
// STEP 5) n=1, lastDigit = n%10 = 1,
reversedNumber = 5432*10 + 1 = 54321, n = 0
// STEP 1) n=100, lastDigit = n%10 = 0,
reversedNumber = 0, n = 10
// STEP 2) n=10, lastDigit = n%10 = 0,
reversedNumber = 0, n = 1
// STEP 3) n=1, lastDigit = n%10 = 1,
reversedNumber = 1, n = 0
// STEP 1) n=304, lastDigit = n%10 = 4,
reversedNumber = 0*10 + 4 = 4, n = 30
// STEP 2) n=30, lastDigit = n%10 = 0,
reversedNumber = 4*10 + 0 = 40, n = 3
// STEP 3) n=3, lastDigit = n%10 = 3,
reversedNumber = 40*10 + 3 = 403, n = 0
// Loop will run number of digit times (n =
123456789 then loops will run 9 times)
// For n = 100 if you want to print n = 001 (Using
strings we will do this)
while(n>0)
{
// STEP 1 (Find out last digit from the
original number)
int lastDigit = n%10;
// STEP 2 (We will store last digit at the
end of reversed number )
reversedNumber = reversedNumber*10 +
lastDigit;
// STEP 3 (We will remove last digit from
original number)
n = n/10;
}
// We will print this reversed number only one
time
System.out.println(reversedNumber);
}
}
13th Feb (Palindrome Number)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int reversedNumber = 0;
int copyOfN = n;
// Since these steps need to be done multiple
times we will use a LOOP
// STEP 1 (Find out last digit from the original
number)
// STEP 2 (We will store last digit at the end
of reversed number)
// STEP 3 (We will remove last digit from
original number)
// STEP 1) n=12345, lastDigit = n%10 = 5,
reversedNumber = 5, n = 1234
// STEP 2) n=1234, lastDigit = n%10 = 4,
reversedNumber = 5*10 + 4 = 54, n = 123
// STEP 3) n=123, lastDigit = n%10 = 3,
reversedNumber = 54*10 + 3 = 543, n = 12
// STEP 4) n=12, lastDigit = n%10 = 2,
reversedNumber = 543*10 + 2 = 5432, n = 1
// STEP 5) n=1, lastDigit = n%10 = 1,
reversedNumber = 5432*10 + 1 = 54321, n = 0
// STEP 6) n=0, n>0 (False), the loop will stop
while(n>0)
{
// STEP 1 (Find out last digit from the
original number)
int lastDigit = n%10;
// STEP 2 (We will store last digit at the
end of reversed number )
reversedNumber = reversedNumber*10 +
lastDigit;
// STEP 3 (We will remove last digit from
original number)
n = n/10;
}
if(reversedNumber == copyOfN)
{
// THIS will palindrome number
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
13th Feb (Even Sum Question)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// you can take here sum as long also
long sum = 0L;
for(int i=2; i<=n; i=i+2)
{
sum = sum + i;
}
// we want to print it only one time
System.out.println(sum);
}
}
13th Feb (Sum of Digits Assignment)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// Since these steps need to be done multiple
times we will use a LOOP
// STEP 1 (Find out last digit from the original
number)
// STEP 2 (We will add the last digit we found
out in a variable)
// STEP 3 (We will remove last digit from
original number)
int sum = 0;
// STEP 1) n=12345, lastDigit = n%10 = 5, sum =
0 + 5 = 5, n = 1234
// STEP 2) n=1234, lastDigit = n%10 = 4, sum = 5
+ 4 = 9, n = 123
// STEP 3) n=123, lastDigit = n%10 = 3, sum = 9
+ 3 = 12, n = 12
// STEP 4) n=12, lastDigit = n%10 = 2, sum = 12
+ 2 = 14, n = 1
// STEP 5) n=1, lastDigit = n%10 = 1, sum = 14 +
1 = 15, n = 0
// STEP 6) n=0, n>0 (False), the loop will stop
Why? (Because all digits are finished)
while(n>0)
{
// STEP 1 (Find out last digit from the
original number)
int lastDigit = n%10;
// STEP 2 (We will add the last digit we
found out, in a variable)
sum = sum + lastDigit;
// STEP 3 (We will remove last digit from
original number)
n = n/10;
}
System.out.println(sum);
}
}
13th Feb (Factorial Question)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// RULES for initialization
// If that variable is multiplied/division what
is the value we should choose -> 1
// If that variable is added/subtraction what is
the value we should choose -> 0
long fact = 1L;
// N = 3
// STEP 1: i=1, 1<=3 (True), fact = 1 * 1 = 1, i
will be updated to 2
// STEP 2: i=2, 2<=3 (True), fact = 1 * 2 = 2, i
will be updated to 3
// STEP 3: i=3, 3<=3 (True), fact = 2 * 3 = 6, i
will be updated to 4
for(int i=1; i<=n; i=i+1)
{
fact = fact * i;
}
System.out.println(fact);
}
}
13th Feb (Nested Loop Concept)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// STEP 1: i=1, 1<=3 (True), i will be updated
to 2
// STEP 2: i=2, 2<=3 (True), i will be updated
to 3
// STEP 3: i=3, 3<=3 (True), i will be updated
to 4
for(int i=1; i<=3; i=i+1) // ROWS (number of
lines to be printed )
{
// THIS IS when i = 1
// STEP 1: j=1, 1<=2 (True), j will be
updated to 2, Hello is printed
// STEP 2: j=2, 2<=2 (True), j will be
updated to 3, Hello is printed
// THIS IS when i = 2
// STEP 1: j=1, 1<=2 (True), j will be
updated to 2, Hello is printed
// STEP 2: j=2, 2<=2 (True), j will be
updated to 3, Hello is printed
// THIS IS when i = 3
// STEP 1: j=1, 1<=2 (True), j will be
updated to 2, Hello is printed
// STEP 2: j=2, 2<=2 (True), j will be
updated to 3, Hello is printed
for(int j=1; j<=2; j=j+1) // COLUMNS (What
is printed in that line)
{
System.out.println("Hello");
}
}
}
}
13th Feb (Nested Loop Star Pattern print)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// STEP 1: i=1, 1<=3 (True), i will be updated
to 2
// STEP 2: i=2, 2<=3 (True), i will be updated
to 3
// STEP 3: i=3, 3<=3 (True), i will be updated
to 4
// i is denoting row number/ line number
for(int i=1; i<=3; i=i+1) // ROWS (number of
lines to be printed )
{
// For i value 1 -->
// STEP 1: j=1, 1<=1 (True), j will be
updated 2
// * is printed
// For i value 2 -->
// STEP 1: j=1, 1<=2 (True), j will be
updated 2
// STEP 2: j=2, 2<=2 (True), j will be
updated 3
// ** is printed (Both the stars are printed
in the same line)
// For i value 3 -->
// STEP 1: j=1, 1<=3 (True), j will be
updated 2
// STEP 2: j=2, 2<=3 (True), j will be
updated 3
// STEP 3: j=3, 3<=3 (True), j will be
updated 4
// *** is printed (All in the same line
print)
for(int j=1; j<=i; j=j+1) // COLUMNS (What
is printed in that line)
{
System.out.print("*");
}
System.out.println(); // since we want to go
into next line we change cursor position
}
}
}
14th Feb (StairCase Question - 1)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// STEP 1 Write outer loop (It is associated
with number of rows ?)
// STEP 2 Write inner loop (We need to print
spaces and hashes)
// n = 4
int spaces = n-1;
for(int i=1; i<=n; i=i+1)
{
// i=1, Row - 1, spaces = 3, hashes = 1, n
= 4, n-i = 3
// i=2, Row - 2, spaces = 2, hashes = 2, n
= 4, n-i = 2
// i=3, Row - 3, spaces = 1, hashes = 3, n
= 4, n-i = 1
// i=4, Row - 4, spaces = 0, hashes = 4, n
= 4, n-i = 0
// Row number is as same as i and hashes is
as same as Row number
// printing spaces (In a single we need to
print)
for(int j=1; j<=spaces; j=j+1)
{
System.out.print(" ");
}
// we need to decrease the spaces by 1
everytime
spaces = spaces - 1;
// printing hashes (In a single we need to
print)
for(int j=1; j<=i; j=j+1) // i times (Row
number times)
{
System.out.print("#");
}
System.out.println();
}
}
}
14th Feb (StairCase Assignment - 2)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// STEP 1 Write outer loop (It is associated
with number of rows ?)
// STEP 2 Write inner loop (We need to print
spaces and hashes)
for(int i=1; i<=n; i=i+1) // First loop -->
ROWS
{
// STEP 1 --> i=1, Row - 1, spaces = 3,
hashes = 1, n = 4, n-i = 3
// STEP 2 --> i=2, Row - 2, spaces = 2,
hashes = 2, n = 4, n-i = 2
// STEP 3 --> i=3, Row - 3, spaces = 1,
hashes = 3, n = 4, n-i = 1
// STEP 4 --> i=4, Row - 4, spaces = 0,
hashes = 4, n = 4, n-i = 0
// Row number is as same as i and hashes is
as same as Row number
// printing spaces (In a single we need to
print)
for(int j=1; j<=(n-i); j=j+1) // What is
printed in that row
{
System.out.print(" ");
}
// printing hashes (In a single we need to
print)
for(int j=1; j<=i; j=j+1) // i times (Row
number times)
{
System.out.print("#");
}
// We need to change line or move to the
next line
System.out.println();
}
}
}
14th Feb (Print Continuous Character Pattern)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// First character of every row depend upon the
row number (Starting character)
// How to many characters are there in a every
row
// How many row are there ?
// STEP 1 Write outer loop (It is associated
with number of rows ?)
// STEP 2 Write inner loop (We need to print
spaces and hashes)
for(int i=1; i<=n; i=i+1) // First loop -->
ROWS
{
// i=1, Row - 1, inner for loop should run
1 time, starting character = 'A'
// i=2, Row - 2, inner for loop should run
2 times, starting character = 'B'
// i=3, Row - 3, inner for loop should run
3 times, starting character = 'C'
// i=4, Row - 4, inner for loop should run
4 times, starting character = 'D'
// i = 1, 64 + 1 = 65 --> character --> 'A'
|| A
// i = 2, 64 + 2 = 66 --> character --> 'B'
|| BC
// i = 3, 64 + 3 = 67 --> character --> 'C'
|| CDE
// i = 4, 64 + 4 = 68 --> character --> 'D'
|| DEFG
// Row 3 --> we started from 'C', 'D', 'E'
// 6th Feb recording - Explicit type
casting
// charAt is only in case of input from
user, we will not write charAt
// starting character
char ch = (char)(64 + i);
// Row 1) i=1, ch = 'A', Inner loop will
run 1 time, A printed, we will move to next line
// Row 2) i=2, ch = 'B', Inner loop will
run 2 time, BC printed, we go to next line
// Row 3) i=3, ch = 'C', Inner loop will
run 3 time, CDE printed, we go to next line
for(int j=1; j<=i; j=j+1)
{
// print the character
System.out.print(ch);
// increasing the character
ch++; // ch = ch + 1 (It will not work)
// if ch becomes greater than 'Z', we
will bring ch to 'A'
if(ch > 'Z')
{
ch = 'A';
}
}
System.out.println();
}
}
14th Feb (T test cases concept IMPORTANT WILL COME IN MODULE
TEST)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//Q1 Input a number a from user and print it
// int n = in.nextInt();
// System.out.println(n);
// Q2 Input a number a from user and print it
for all T testcases
// The first line contains the number of test
cases.
// For each test case: The first line contains
an integer N and print it.
// Solution -> T testcase (How to solve it)
// Logic will be written inside a loop
int t = in.nextInt();
for(int i=1; i<=t;i=i+1)
{
int n = in.nextInt();
System.out.println(n);
}
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i=1; i<=t; i=i+1)
{
// LOGIC of the program is written inside a
loop
// whole logic will run t times
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a+b);
}
}
}
14th Feb (Print Continuous Character Pattern)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int k=1; k<=t; k=k+1)
{
int n = in.nextInt();
for(int i=1; i<=n; i=i+1) // First loop -->
ROWS
{
char ch = (char)(64 + i);
for(int j=1; j<=i; j=j+1)
{
// print the character
System.out.print(ch);
// increasing the chacter
ch++; // ch = ch + 1 (It will not work)
// if ch becomes greater than 'Z', we
will bring ch to 'A'
if(ch > 'Z')
{
ch = 'A';
}
}
System.out.println();
}
}
}
}
14th Feb (Armstrong number in a range - 2)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int m = in.nextInt(); // starting point
int n = in.nextInt(); // ending point
// RULE -> inside the for loop you will never
makea any change in i or counter variable
// m = 100
// n = 200
// Values first loop -> 100, 101,
102, ................. 153, ............... 200
for(int i=m; i<=n; i=i+1)
{
// whether this i number is armstrong
number
// Any question which has DIGITs of a
number, we will use while loop
// For checking armstrong number
// STEP 1 -> number of digits of i by
storing copy of i
int numberOfDigits = 0;
int temp = i;
// (STEP 1) temp = 153, numberOfDigits = 0
+ 1 = 1, temp = 153/10 = 15
// (STEP 2) temp = 15, numberOfDigits = 1 +
1 = 2, temp = 15/10 = 1
// (STEP 3) temp = 1, numberOfDigits = 2 +
1 = 3, temp = 1/10 = 0
// (STEP 4) temp>0 will false, The loop
will stop
while(temp>0)
{
numberOfDigits = numberOfDigits + 1;
// remove the last digit everytime
temp = temp/10;
}
// Sum of digits of a number (where every
digit is raised to power number of digits)
int sum = 0;
temp = i;
// numberOfDigits = 3
// (STEP 1) temp = 153, lastDigit = temp%10
= 3, power = 3^3 = 27, sum = 0 + 27 = 27, temp = 15
// (STEP 2) temp = 15, lastDigit = 15%10 =
5, power = 5*5*5 = 125, sum = 125 + 27 = 152, temp = 1
// (STEP 3) temp = 1, lastDigit = 1%10 = 1,
power = 1*1*1 = 1, sum = 152 + 1 = 153
while(temp>0)
{
// STEP 1 (Find out last digit from the
original number)
int lastDigit = temp%10;
// lastDigit ^ numberOfDigits
(numberOfDigits = 3) (lastDigit^3)
int power = 1;
// k = 1, k<=3, lastDigit = 3,
numberOfDigits = 3, power = 1 * 3 = 3
// k = 2, k<=3, lastDigit = 3,
numberOfDigits = 3, power = 3 * 3 = 9
// k = 3, 3<=3, lastDigit = 3,
numberOfDigits = 3, power = 9 * 3 = 27
for(int k=1; k<=numberOfDigits; k=k+1)
{
power = power * lastDigit;
}
// STEP 2 (We will add the
lastDigit^numberOfDigits we found out, in a variable)
sum = sum + power;
// STEP 3 (We will remove last digit
from original number)
temp = temp/10;
}
if(sum == i)
{
// Then it will be an armstrong number
System.out.print(i + " ");
}
}
}
14th Feb (Armstrong Number Approach - 2)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int m = in.nextInt(); // starting point
int n = in.nextInt(); // ending point
// RULE -> inside the for loop you will never
makea any change in i or counter variable
// m = 100
// n = 200
// Values first loop -> 100, 101,
102, ................. 153, ............... 200
for(int i=m; i<=n; i=i+1)
{
// whether this i number is armstrong
number
// Any question which has DIGITs of a
number, we will use while loop
// For checking armstrong number
// STEP 1 -> number of digits of i by
storing copy of i
int numberOfDigits = 0;
int temp = i;
// (STEP 1) temp = 153, numberOfDigits = 0
+ 1 = 1, temp = 153/10 = 15
// (STEP 2) temp = 15, numberOfDigits = 1 +
1 = 2, temp = 15/10 = 1
// (STEP 3) temp = 1, numberOfDigits = 2 +
1 = 3, temp = 1/10 = 0
// (STEP 4) temp>0 will false, The loop
will stop
while(temp>0)
{
numberOfDigits = numberOfDigits + 1;
// remove the last digit everytime
temp = temp/10;
}
// Sum of digits of a number (where every
digit is raised to power number of digits)
int sum = 0;
temp = i;
// numberOfDigits = 3
// (STEP 1) temp = 153, lastDigit = temp%10
= 3, power = 3^3 = 27, sum = 0 + 27 = 27, temp = 15
// (STEP 2) temp = 15, lastDigit = 15%10 =
5, power = 5*5*5 = 125, sum = 125 + 27 = 152, temp = 1
// (STEP 3) temp = 1, lastDigit = 1%10 = 1,
power = 1*1*1 = 1, sum = 152 + 1 = 153
while(temp>0)
{
// STEP 1 (Find out last digit from the
original number)
int lastDigit = temp%10;
// STEP 2 (We will add the
lastDigit^numberOfDigits we found out, in a variable)
sum = sum + (int)Math.pow(lastDigit,
numberOfDigits);
// STEP 3 (We will remove last digit
from original number)
temp = temp/10;
}
if(sum == i)
{
// Then it will be an armstrong number
System.out.print(i + " ");
}
}
}
Diamond Question and Optimus Prime
These questions we can do in Sunday class (Revision Class)
15th Feb (Function rules BASIC)
import java.util.Scanner;
public class Main
{
// RULE -> There will no function inside another
function (EXAMPLE)
// Program will start running from the main function
always
// access specifier tells where this function is
accessible
// access specifier --> private or public
// In module 1 we will always write public function
// In module 3 (We will discuss private in detail)
// static --> We will make all functions as static
// In module 3 (We will discuss word in complete
detail)
// static --> static does not require any object to
call it
// return type
// Function returns a integer value then return type
will be int
// Function returns a decimal value then return type
will be double/float
// Function returns a English Word then return type
will be String
// Function returns a English letter then return
type will be char
// Function returns a English Sentence then return
type will be String
// When we don't want to return anything from a
function then return type will be void
// functionName (It can be anything you want)
// parameter list
// anything which is given to a function will come
under parameter list
//access specifier static return type
functionName(parameter list)
// datatype variable, datatype variable
// Every function will be called using function
name, and parameter list
// Every function will be called from main function
and when function is completed
// it come back to main function
// using return statement function will end
immediately
// Q -> Write a function which returns sum of two
integer values
public static int sum(int a,int b)
{
return a+b;
}
// Q -> Write a function which returns sum of three
decimal values
// public static double sum2(double a, double b,
double c)
// {
// }
public static void main(String[] args) // main
function
{
int ans = sum(15000, 9);
System.out.println(ans);
}
15th Feb (Examples of function - 1)
import java.util.Scanner;
public class Main
{
// Q -> Write a function which returns sum of three
decimal values
public static double sum2(double a, double b, double
c)
{
return a+b+c;
}
// Q -> Write a function which prints number from 1
to n
public static void printUsingLoops(int n)
{
for(int i=1; i<=n; i=i+1)
{
System.out.print(i + " ");
}
}
public static void main(String[] args) // main
function
{
double ans1 = sum2(3.0, 4.5, 4.5);
System.out.println(ans1);
}
15th Feb (Functions with Loops)
import java.util.Scanner;
public class Main
{
// Q -> Write a function which returns sum of three
decimal values
public static double sum2(double a, double b,
double c)
{
return a+b+c;
}
// Q -> Write a function which prints number from 1
to n
public static void printUsingLoops(int n)
{
for(int i=1; i<=n; i=i+1)
{
System.out.print(i + " ");
}
}
public static void main(String[] args) // main
function
{
double ans1 = sum2(3.0, 4.5, 4.5);
System.out.println(ans1);
Scanner in = new Scanner(System.in);
int n = in.nextInt();
printUsingLoops(n);
}
15th Feb (Functions with Scanner Class)
import java.util.Scanner;
public class Main
{
// For input we will use Scanner class
// It will be written in main function
// Execution will start from which function ?
// Main function
// Q -> Write a function which returns sum of three
decimal values
public static double sum2(double a, double b, double
c)
{
return a+b+c;
}
// Q -> Write a function which prints number from 1
to n
public static void printUsingLoops(int n)
{
for(int i=1; i<=n; i=i+1)
{
System.out.print(i + " ");
}
// Since there is no return statement & this
function is not returning anything
// the return type will be void
}
public static void main(String[] args) // main
function
{
double ans1 = sum2(3.0, 4.5, 4.5);
System.out.println(ans1);
Scanner in = new Scanner(System.in);
int n = in.nextInt();
printUsingLoops(n);
}
15th Feb (Int return type function example)
import java.util.Scanner;
public class Main
{
// Q -> Input two integers and write a function
which finds sum of those two integers
public static int sum(int a,int b)
{
return a+b;
}
public static void main(String[] args) // main
function
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int ans = sum(a,b);
System.out.println(ans);
}
15th Feb (Void function Example)
import java.util.Scanner;
public class Main
{
// CALLING A FUNCTION - functionName(parameter
list)
// EXAMPLE - sum(a,b)
// Q -> Input two integers and write a void
function which print sum of those two integers
public static void sum(int a,int b)
{
// This function cannot return anything
System.out.print(a+b);
}
public static void main(String[] args) // main
function
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
// this function sum is void function (so
it will not return anything)
sum(a,b);
}
15th Feb (Function Questions CONCEPT IMPORTANT)
import java.util.Scanner;
public class Main
{
public static void main(String[] args) // main
function
{
// 1. In coding TEST (Module Test or company
Test)
// In some companies we will write the whole
code
// In 99% of the companies we will write
only the function
}
}
15th Feb (Function Problem 1)
import java.util.*;
import static java.lang.Math.ceil;
public class Main {
//Make a function here and call it in main()
public static void fun() {
System.out.println("I am another function");
}
public static void main(String[] args) {
fun();
}
}
15th Feb (Function Problem 2)
import java.util.Scanner;
public class Main {
public static void fun(int n, int m)
{
System.out.print(n+m);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
fun(n, m);
}
}
15th Feb (Function Problem 3)
import java.io.*;
import java.util.*;
public class Main {
// You will always write public, if it is not written
then don't worry
static int fun(int n){
// WHAT IS THE Return type here ?
// We should integer value
// What should be that value ?
// We just have to complete the function and
// not worry about the printing part
return n+5;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int result = fun(n);
System.out.print(result);
}
}
15th Feb (Frequency Of Digit)
import java.util.*;
public class Main {
// IT wants you to return FrequencyofDigits
static int FrequencyofDigits(long n, int d) {
// while loop
int count = 0;
while(n>0)
{
long lastDigit = n%10L;
if(lastDigit == d)
{
count = count + 1;
}
n = n/10L; // Since n is long that is why we
write here 10 as 10L
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
int d = sc.nextInt();
System.out.println(FrequencyofDigits(n, d));
}
}
15th Feb (Super Hero)
import java.util.*;
public class Main {
static String ModifyName(String name) {
return name + " is a Super Hero";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println(ModifyName(name));
}
}
15th Feb (Place Value Checker Functions)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
boolean res = determineSecondLastDigit(n);
if (res)
System.out.println("Yes");
else
System.out.println("No");
}
public static boolean determineSecondLastDigit(int
n) {
// n = 931
// Remove the last digit
n = n/10; // n = 93
// Finding out the last digit
int lastDigit = n%10; // lastDigit = 3
// RULE --> If there is any return type then
function will always return
// We need to make sure that our function is
always returning
// IN function make to use else also in case of if-else
statement
if(lastDigit == 0) // this condition will be
false
{
return true;
}
else
{
return false;
}
// If you are using else if make sure you use else also
}
}
Binary To Decimal
String is involved, we will skip it as of now (Since it will come in module
2)
17th Feb (Calculate NcR Approach 1)
import java.util.*;
public class Main {
static long calculate_nCr(int n, int r) {
// nCr = n!/((n-r)! * r!)
// factorial of a number is a big number
// 20! = 2.43290201e18 (IT is approximately
2 * 10^18)
// Which datatype we can use ?
// We will use here long datatype
long nFactorial = 1L;
for(int i=1;i<=n;i=i+1)
{
nFactorial = nFactorial * i;
}
long nMinusRFactorial = 1L;
for(int i=1;i<=(n-r);i=i+1)
{
nMinusRFactorial = nMinusRFactorial * i;
}
long rFactorial = 1L;
for(int i=1;i<=r;i=i+1)
{
rFactorial = rFactorial * i;
}
long ans = nFactorial/nMinusRFactorial;
ans = ans/rFactorial;
return ans;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int r = scanner.nextInt();
long ans = calculate_nCr(n,r);
System.out.print(ans);
}
}
17th Feb (Calculate NcR - Approach 2)
import java.util.*;
public class Main {
public static long factorial(int n)
{
long fact = 1L;
for(int i=1; i<=n; i=i+1)
{
fact = fact * i;
}
return fact;
}
static long calculate_nCr(int n, int r) {
// nCr = n!/((n-r)! * r!)
// factorial of a number is a big number
// 20! = 2.43290201e18 (IT is approximately
2 * 10^18)
// Which datatype we can use ?
// We will use here long datatype
long nFactorial = factorial(n); // value of
n in the above function will be 1500
// value of n in the above function will be
1
long nMinusRFactorial = factorial(n-r); //
factorial(5-4) = factorial(1)
long rFactorial = factorial(r);
long ans = nFactorial/nMinusRFactorial;
ans = ans/rFactorial;
return ans;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int r = scanner.nextInt();
long ans = calculate_nCr(n,r);
System.out.print(ans);
}
}
17th Feb (Functions Concept Revision)
public class Main {
// Question -> Take a double value and a integer
value in a function
// Convert double to integer and return the sum
in integer format
public static int sum(double a,int b)
{
// int a = 10; (YOU cannot create here a and
b since they are already created)
// int b = 500;
// Try to convert double to integer and then
we will find the sum
// Explicit type casting
int acciojob = (int)a;
return acciojob + b;
}
public static void main(String[] args)
{
double s = 1.45;
int e = 10;
int ans = sum(s+2.1,e);
System.out.println(ans);
}
}
17th Feb Contest COMPULSORY
2 or 1 question of Loops
1 Nested Loops
1 question on 1D array (Basic) (Maybe)
Total - 3 Questions
90 mins on coding
60 mins on APTI
Sunday CODING class timing is 12:30PM to 3:30 PM
Some questions of LOOPs & contest questions
Saturday NO CODING class
MARKS don’t matter here
17th Feb (Array Basic - 1)
public class Main
{
public static void main(String args[])
{
// RULE of array
// datatype nameOfArray[] = new
datatype[sizeofArray];
// RULE --> size will always be integer
// more discussion on new keyword we will do
on Monday
// Question -> create an array of size 5 for
storing integers
int arr[] = new int[5]; // [0, 0, 0, 0, 0]
// Print value stored at 0th position
System.out.println(arr[0]);
arr[1] = 100;
// Print value stored at 1st position
System.out.println(arr[1]);
// All numbers are stored one by one
char crr[] = new char[10];
// storing 100 decimal number
double drr[] = new double[100];
// Store 10 words together
String s[] = new String[10];
// Rule of variable
// datatype variable =
valueStoredInTheVariable;
}
}
17th Feb (Array Basic - 2)
public class Main
{
public static void main(String args[])
{
// RULE of array
// datatype nameOfArray[] = new
datatype[sizeofArray];
// RULE --> size will always be integer
// more discussion on new keyword we will do
on Monday
// Question -> create an array of size 5 for
storing integers
int arr[] = new int[5]; // [0, 0, 0, 0, 0]
arr[1] = 10;
arr[2] = 5;
arr[3] = 16;
arr[4] = 1;
arr[0] = 7;
// Print value stored at 0th position
System.out.println(arr[0]);
// Print value stored at 1st position
System.out.println(arr[1]);
// Print value stored at 2nd position
System.out.println(arr[2]);
// Print value stored at 3rd position
System.out.println(arr[3]);
// Print value stored at 4th position
System.out.println(arr[4]);
// STEP 1 i=0, 0<5 (True), arr[0] --> 7
// STEP 2 i=1, 1<5 (True), arr[1] --> 10
// STEP 3 i=2, 2<5 (True), arr[2] --> 5
// STEP 4 i=3, 3<5 (True), arr[3] --> 16
// STEP 5 i=4, 4<5 (True), arr[4] --> 1
for(int i=0; i<5; i=i+1)
{
System.out.println(arr[i]);
}
// All numbers are stored one by one
char crr[] = new char[10];
// storing 100 decimal number
double drr[] = new double[100];
// Store 10 words together
String s[] = new String[10];
// Rule of variable
// datatype variable =
valueStoredInTheVariable;
}
}
17th Feb (Input and print an array)
// Question -
// Marks of n students and we need to find out sum
of their marks
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// We will input size of the array from the
user
int size = in.nextInt();
// marks are integers and how many marks we
want to store ?
int arr[] = new int[size];
// size = 10
// arr -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
// index- 0 1 2 3 4 5 6 7 8 9
// Inputting elements into the array using
scanner class
// STEP 1) i=0, arr[0] = 78
// STEP 2) i=1, arr[1] = 98
// STEP 3) i=2, arr[2] = 99
// STEP 4) i=3, arr[3] = 100
for(int i=0; i<size; i=i+1)
{
arr[i] = in.nextInt();
}
// This also can be done
// for(int i=0; i<size; i=i+1)
// {
// arr[i] = in.nextInt();
// System.out.print(arr[i] + " ");
// }
// Print the array
for(int i=0; i<size; i=i+1)
{
System.out.print(arr[i] + " ");
}
int sum = 0;
// arr[] = 78 98 99 100 50 40 60 65 100 99
// STEP 1) i=0, 0<10 (True), sum = 0 + 78 =
78
// STEP 2) i=1, 1<10 (True), sum = 78 + 98 =
176
for(int i=0; i<size; i=i+1)
{
sum = sum + arr[i];
}
System.out.println(sum);
}
}
17th Feb (Pass array to function)
public class Main
{
public static int func(int a) // a is referring
to a variable
{
public static int func1(int a[]) // array is
passed or given to a function
{
}
public static int func2(int[] a) // array is
passed or given to a function
{
public static void main(String args[])
{
}
}
17th Feb (Array Problem 1)
import java.util.*;
public class Main {
public static void main(String[] args) throws
Throwable {
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 ans=ArrayProblem1(n,arr);
System.out.println(ans);
}
public static int ArrayProblem1(int n, int[]
arr)
{
// GIVEN to function - size of array and we
are given a array
// RETURN/ OUTPUT - Index of largest element
// STEP 1 (Find out largest element)
// arr = [5,1,2,7,10,4,10,100]
// largestElement should not be initialised
with zero
// arr = [-1, -2, -9, -10]
int largestElement = arr[0];
// largestElement = 5
// STEP 1) i=0, arr[i] = 5, 5>5 (False),
largestElement = 5
// STEP 2) i=1, arr[i] = 1, 1>5 (False),
largestElement = 5
// STEP 3) i=2, arr[i] = 2, 2>5 (False),
largestElement = 5
// STEP 4) i=3, arr[i] = 7, 7>5 (True),
largestElement = 7
// STEP 5) i=4, arr[i] = 10, 10>7 (True),
largestElement = 10
// STEP 6) i=5, arr[i] = 4, 4>10 (False),
largestElement = 10
// STEP 7) i=6, arr[i] = 10, 10>10 (False),
largestElement = 10
for(int i=0; i<n; i=i+1)
{
if(arr[i] > largestElement)
{
largestElement = arr[i];
}
}
// STEP 2 (Find out index of largest
element)
// arr[i] --> storing WHAT ? (What is the
value in the box is represented by arr[i])
// i representing --> index (Box number is
represented by this)
// Searching where this element is present
in the array
int index = 0;
// STEP 1) i=0, arr[i] = 5, 5 == 10 (False),
largestElement = 10
// STEP 2) i=1, arr[i] = 1, 1 == 10 (False),
largestElement = 10
// STEP 3) i=2, arr[i] = 2, 2 == 10 (False),
largestElement = 10
// STEP 4) i=3, arr[i] = 7, 7 == 10 (False),
largestElement = 10
// STEP 5) i=4, arr[i] = 10, 10 == 10
(True), largestElement = 10
for(int i=0; i<n; i=i+1)
{
if(arr[i] == largestElement)
{
index = i;
break; // WE found out the smallest
position at which largestElement is present
}
}
return index;
}
}
17th Feb (Array Problem 2)
import java.util.*;
public class Main {
public static void main(String[] args) throws
Throwable {
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 ans=ArrayProblem2(n,arr);
System.out.println(ans);
}
public static int ArrayProblem2(int n, int[]
arr){
// Return - Count of numbers which are
greater than 35
int count = 0;
for(int i=0; i<n; i=i+1)
{
if(arr[i]>35)
{
count = count + 1;
}
}
return count;
}
}
17th Feb (Array Operations)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++)
arr[i] = scanner.nextInt();
ArrayOperations(arr, n);
}
public static void ArrayOperations(int[] arr,
int n) {
int sum = 0;
int largestElement = arr[0];
for(int i=0; i<n; i=i+1)
{
if(arr[i] > largestElement)
{
largestElement = arr[i];
}
sum = sum + arr[i];
}
System.out.println(sum + " " + sum/n + " " +
largestElement);
}
}
17th Feb (Maximum difference between two elements)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int []arr=new int[n];
for(int i=0;i<n;++i){
arr[i]=sc.nextInt();
}
ArrayProblem(arr);
}
public static void ArrayProblem(int arr[]) {
// When array size is not given
// arr.length --> size of the array
int n = arr.length; // inbuilt function
(automatically give you the length)
// STEP 1 (Find largest element)
// STEP 2 (Find Smallest element)
// STEP 3 (Print largest - smallest (This
will be the maximum difference))
}
}
18th Feb Contest Questions
https://course.acciojob.com/idle?question=e27354cf-c810-4c9d-bca6-
4127e1705870
https://course.acciojob.com/idle?question=29143be0-0117-4544-95c3-
b112bfc5947d
https://course.acciojob.com/idle?question=69d6bdb7-fcb7-4145-bab9-
576daab746c2
19th Feb (Contest Discussion)
import java.util.*;
public class Main{
// Driver Code
public static void main(String[] args)
{
int n;
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println(squaresum(n));
}
static int squaresum(int n)
{
// STEP 1 (LOOP from 1 to n)
int sum = 0;
// 1 <= n <= 12 (Value of n is small, so we
can use int datatype)
// n = 3
// STEP 1) i=1, 1<=3, sum = 0+1*1 = 1, i new
value will be 2
// STEP 2) i=2, 2<=3, sum = 1+2*2 = 5, i new
value will be 3
// STEP 3) i=3, 3<=3, sum = 5+3*3 = 14, i
new value will be 3
for(int i=1; i<=n; i=i+1)
{
sum = sum + i*i;
}
return sum;
}
}
19th Feb (Squared Natural Number)
import java.util.*;
public class Main {
public static void main(String[] args) throws
Throwable {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
printSameNumberPyramid(n);
}
public static void printSameNumberPyramid(int n)
{
// 2 LOOPS
// STEP 1 First loop will be rows
// STEP 2 Inside there will be one loop for
space and other will be for numbers
for(int i=1; i<=n; i=i+1) // For printing n
rows
{
// Row is 1, i = 1, n = 4, n-i = 3 (We
will print 3 spaces)
// Row is 2, i = 2, n = 4, n-i = 2 (We
will print 2 spaces)
// Row is 3, i = 3, n = 4, n-i = 1 (We
will print 1 spaces)
// printing spaces
for(int j=1; j<=(n-i); j=j+1)
{
System.out.print(" ");
}
// printing numbers
for(int j=1; j<=i; j=j+1)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
19th Feb (Grade Students)
import java.util.*;
class Solution {
public void markStudents(int[] marks, int thres)
{
// we will be doing some printing part
// array, threshold
// If size is not given, How to find the
size ?
int n = marks.length;
// Loop will go from 0 to n
// marks[] -> [6, 7, 10, 9, 5]
// 0 1 2 3 4
// marks[3] --> 4th element
for(int i=0; i<n; i=i+1)
{
if(marks[i] > thres)
{
System.out.print("pass ");
}
else
{
System.out.print("fail ");
}
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] marks = new int[n];
for (int i = 0; i < n; i++)
marks[i] = sc.nextInt();
int thres = sc.nextInt();
Solution Obj = new Solution();
Obj.markStudents(marks, thres);
sc.close();
}
}
19th Feb (Optimus Prime Question)
import java.util.*;
public class Main {
public static void main(String[] args) throws
Throwable {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// CONCEPT ->
// Divisors exist in pairs (Half of the
divisors are less than sqrt(N) and
// half of them are above sqrt(N))
// If we want to find out divisors of a
number then we can check till sqrt(N)
// For Loop
// As 1 is not prime, we can directly start
the loop from 2
for(int i=2; i<=n; i=i+1)
{
boolean isPrime = true; // It will two
values
int sqrtOfI = (int)Math.sqrt(i);
// Math.sqrt(i) will return decimal
number or double value
// For every value of i, 2 to i-1
for(int j=2; j<=sqrtOfI; j=j+1)
{
if(i%j==0)
{
// if there is any divisor for n
then we will make the number as not prime
isPrime = false;
}
}
if(isPrime == true)
{
System.out.println(i);
}
}
}
}
19th Feb (Diamond Pattern)
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int k=1; k<=t; k=k+1) // For t test
cases
{
int n = in.nextInt(); // rows given by
user
// N = 7
// UPPER PART OF DIAMOND
// i = 1, 3, 5 ... (These will be the
values of i)
// STEP 1) i=1, 1<=7 (True), 1 star, n-i
= 6 spaces
// STEP 2) i=3, 3<=7 (True), 3 star, n-i
= 4 spaces
// STEP 3) i=5, 5<=7 (True), 5 star, n-i
= 2 spaces
// STEP 4) i=7, 7<=7 (True), 7 star, n-i
= 0 spaces
for(int i=1; i<=n; i=i+2)
{
// Row 1 --> 1 star, 4 spaces, i =
1, n = 5, n-i = 4
// Row 2 --> 3 stars, 2 spaces, i =
3, n = 5, n-i = 2
// Row 3 --> 5 stars, 0 spaces, i =
5, n = 5, n-i = 0
// for loop number of spaces
for(int j=1; j<=(n-i); j=j+1)
{
System.out.print(" ");
}
// for loop number of stars
for(int j=1; j<=i; j=j+1)
{
System.out.print("* ");
}
System.out.println();
}
// STEP 1) i = 5, n = 7, stars = 5,
spaces = n-i = 2
// STEP 2) i = 3, n = 7, stars = 3,
spaces = 4 (n-i)
// STEP 3) i = 1, n = 7, stars = 1,
spaces = 6 (n-i)
// LOWER PART OF DIAMOND
for(int i=(n-2); i>=1; i=i-2)
{
// i will representing number of
stars
// for loop number of spaces
for(int j=1; j<=(n-i); j=j+1)
{
System.out.print(" ");
}
// for loop number of stars
for(int j=1; j<=i; j=j+1)
{
System.out.print("* ");
}
System.out.println();
}
19th Feb (Diamond Pattern Approach - 2)
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int k=1; k<=t; k=k+1) // For t test
cases
{
int n = in.nextInt(); // rows given by
user
// N = 7
// UPPER PART OF DIAMOND
// i = 1, 3, 5 ... (These will be the
values of i)
// STEP 1) i=1, n = 7, 1<=7 (True), 1
star, n-i = 6 spaces
// STEP 2) i=3, n = 7, 3<=7 (True), 3
star, n-i = 4 spaces
// STEP 3) i=5, 5<=7 (True), 5 star, n-i
= 2 spaces
// STEP 4) i=7, 7<=7 (True), 7 star, n-i
= 0 spaces
int spaces = n-1;
int star = 1;
// n = 7
// i = 1, spaces = 6, star = 1
// i = 2, spaces = 4, star = 3
// i = 3, spaces = 2, star = 5
// i = 4, spaces = 0, star = 7
for(int i=1; i<=(n/2+1); i=i+1) // rows
{
// for loop number of spaces
for(int j=1; j<=spaces; j=j+1)
{
System.out.print(" ");
}
// for loop number of stars
for(int j=1; j<=star; j=j+1)
{
System.out.print("* ");
}
spaces = spaces - 2;
star = star + 2;
System.out.println();
}
// STEP 1) i = 5, n = 7, stars = 5,
spaces = n-i = 2
// STEP 2) i = 3, n = 7, stars = 3,
spaces = 4 (n-i)
// STEP 3) i = 1, n = 7, stars = 1,
spaces = 6 (n-i)
// LOWER PART OF DIAMOND
spaces = 2;
star = n-2;
// i = 1, spaces = 2, star = 5
// i = 2, spaces = 4, star = 3
// i = 3, spaces = 6, star = 1
for(int i=1; i<=(n/2); i=i+1)
{
// i will representing number of
stars
// for loop number of spaces
for(int j=1; j<=spaces; j=j+1)
{
System.out.print(" ");
}
// for loop number of stars
for(int j=1; j<=star; j=j+1)
{
System.out.print("* ");
}
spaces = spaces + 2;
star = star - 2;
System.out.println();
}
}
20th Feb (IMPORTANT DATES)
Module Test - 4th March
If you start revision now, you will clear it
Class till 28th Feb
- LOOPS and conditionals (Data Types, Functions &
basic part)
- 1D arrays
- Other techniques
Passing marks - 60%
20th Feb (Maximum difference between two elements)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int []arr=new int[n];
for(int i=0;i<n;++i){
arr[i]=sc.nextInt();
}
ArrayProblem(arr);
}
public static void ArrayProblem(int arr[]) {
// When array size is not given
// arr.length --> size of the array
int n = arr.length; // inbuilt function
(automatically give you the length)
// STEP 1 (Find largest element)
// 17th Feb
int largestElement = arr[0];
// largestElement = 5
// STEP 1) i=0, arr[i] = 5, 5>5 (False),
largestElement = 5
// STEP 2) i=1, arr[i] = 1, 1>5 (False),
largestElement = 5
// STEP 3) i=2, arr[i] = 2, 2>5 (False),
largestElement = 5
// STEP 4) i=3, arr[i] = 7, 7>5 (True),
largestElement = 7
// STEP 5) i=4, arr[i] = 10, 10>7 (True),
largestElement = 10
// STEP 6) i=5, arr[i] = 4, 4>10 (False),
largestElement = 10
// STEP 7) i=6, arr[i] = 10, 10>10 (False),
largestElement = 10
for(int i=0; i<n; i=i+1)
{
if(arr[i] > largestElement)
{
largestElement = arr[i];
}
}
// STEP 2 (Find Smallest element)
int smallest = arr[0];
for(int i=0; i<n; i=i+1)
{
if(arr[i] < smallest)
{
smallest = arr[i];
}
}
// STEP 3 (Print largest - smallest (This
will be the maximum difference))
System.out.println(largestElement -
smallest);
}
}
20th Feb (Index of an Element)
import java.util.*;
public class Main {
public static void IndexOfElement(int N, int X,
int[] arr){
// Since return type is void, we will print
the indexes
// If element is X, then we need to print
index (1 based)
// If X is not present in the entire array
How do we print it ?
int count = 0;
// Inside array indexing is always zero
based
// arr = [3,4,3,1]
// 1 2 3 4 (1 based indexing)
// N = 4, X = 3
// STEP 1) i = 0, arr[0] = 3, X = 3, arr[i]
== X (True), 1 will be printed, count = 1
// STEP 2) i = 1, arr[1] = 4, X = 3, arr[i]
== X (False), count = 1
// STEP 3) i = 2, arr[2] = 3, X = 3, arr[i]
== X (True), 3 will be printed, count = 2
// STEP 3) i = 3, arr[3] = 1, X = 3, arr[i]
== X (False), count = 2
for(int i=0; i<N; i=i+1)
{
if(arr[i] == X)
{
count = count + 1; // we will
increase the value of count
// You have to give output in 1-
based indexing
// i is 0-based index , but we need
1 based indexing
System.out.print(i+1 + " ");
}
}
// We need print -1 only once (We will write
it outside the loop)
if(count == 0) // X was not present in the
array
{
System.out.print("-1");
}
}
public static void main(String[] args) throws
Throwable {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int X = sc.nextInt();
int arr[]=new int[N];
for(int i = 0; i < N; i++)
{
arr[i] = sc.nextInt();
}
IndexOfElement(N,X,arr);
}
}
20th Feb (Reverse an array - 1)
import java.io.*;
import java.util.*;
public class Main {
public static void reverseArray(int arr[], int
start, int end)
{
// this void function will only change the
array
// modification of the array
// If print statement was not there in the
main function, then we will do the printing part
// Since print is DONE in main function,
that's why we will not print
// Either we will print in void if main
function is not doing that Only function will print
(either main function or void function)
// start = 0, end = n-1
int n = arr.length;
int i = 0; // first arrow
int j = n - 1; // second arrow
// TWO POINTERS
// STOPPING POINT -> If arrows meet or
arrows cross each other
// Continue Point -> first arrow < second
arrow
// arr = [5 1 2 3 4], n = 5
// i = 0, j = 4, i<j (True), arr[0] and
arr[4] will be swapped
// i = 1, j = 3, i<j (True), arr[1] and
arr[3] will be swapped
// i = 2, j = 2, i<j (false)
// arr = [5 1 2 6 3 4], n = 6
// i = 0, j = 5, i<j (True),
// i = 1, j = 4, i<j (True),
// i = 2, j = 3, i<j (True),
// i = 3, j = 2, i<j (False)
while(i<j)
{
// interchange the elements
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
// first arrow will go towards right (it
will move forward)
i = i + 1;
// second arrow will go towards left (it
will go backwards)
j = j - 1;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int arr1[] = new int[n];
for(int i=0;i<n;i++)
arr1[i] = sc.nextInt();
reverseArray(arr1, 0, n-1);
for (int i = 0; i < n; i++)
System.out.print(arr1[i] + " ");
System.out.println('\n');
}
}
20th Feb (Reverse an Array - 2)
import java.io.*;
import java.util.*;
public class Main {
public static void reverseArray(int arr[], int
start, int end)
{
// this void function will only change the
array
// modification of the array
// If print statement was not there in the
main function, then we will do the printing part
// Since print is DONE in main function,
that's why we will not print
// start = 0, end = n-1
// TWO POINTERS
// STOPPING POINT -> If arrows meet or
arrows cross each other
// Continue Point -> first arrow < second
arrow
// arr = [5 1 2 3 4], n = 5
// i = 0, j = 4, i<j (True), arr[0] and
arr[4] will be swapped
// i = 1, j = 3, i<j (True), arr[1] and
arr[3] will be swapped
// i = 2, j = 2, i<j (false)
// arr = [5 1 2 6 3 4], n = 6
// i = 0, j = 5, i<j (True),
// i = 1, j = 4, i<j (True),
// i = 2, j = 3, i<j (True),
// i = 3, j = 2, i<j (False)
while(start<end)
{
// interchange the elements
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
// first arrow will go towards right (it
will move forward)
start = start + 1;
// second arrow will go towards left (it
will go backwards)
end = end - 1;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int arr1[] = new int[n];
for(int i=0;i<n;i++)
arr1[i] = sc.nextInt();
reverseArray(arr1, 0, n-1);
for (int i = 0; i < n; i++)
{
System.out.print(arr1[i] + " ");
}
}
}
20th Feb (A contest Question)
import java.io.*;
import java.util.*;
public class Main {
public static void printFindParticipants(int[]
arr, int n, int k) {
// Since main function is not doing
printing, we will do it
// arr, size of the array, kth person
// kth person will be present at k-1 index
int count = 0;
// k value can be from 1 to N
// k = 1 (1st participant which means 0
index participant)
// k = N (last participant which means n-1
index participant)
for(int i=0; i<n; i=i+1)
{
// The contestant who has a score
greater than or equal to the score
// the contestant has a positive score
if(arr[i] >= arr[k-1] && arr[i]>0)
{
count = count + 1;
}
}
System.out.println(count);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int[] arr= new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
printFindParticipants(arr,n,k);
sc.close();
}
}
20th Feb (2nd Largest Element)
import java.io.*;
import java.util.*;
public class Main {
public static void SecondLargest(int[] arr, int
n) {
// WE need to solve it in single for loop
(in interview)
// 1. Is the current element greater than
the largest element ?
// if(currentElement > largestElement)
// --> secondLargestElement = largestElement
// --> largestElement = currentElement
// 2. Is the current element greater than
the second largest element ?
// if(currentElement > secondLargestElement)
// --> secondLargestElement = currentElement
int largestElement = Integer.MIN_VALUE;
int secondLargestElement =
Integer.MIN_VALUE;
// -5 -4 -1 -6
// largest = -2147483648 (arr[0] or -
2147483648)
// Integer.MIN_VALUE --> -2147483648 (BIG
NEGATIVE VALUE)
// Integer.MAX_VALUE --> 2147483647 (BIG
POSITIVE VALUE)
// For largest number or smallest number -->
initialize with arr[0] or -2147483648
// sum/addition/subtraction of numbers -->
initialize with 0
// product of number --> initialize with 1
// 2nd largest/ 3rd largest --> initialize
with -2147483648 always
// arr = [5 1 2 3 6], n = 5
// largestElement = 6
// secondLargestElement = 5
// STEP 1) i=0, arr[i] = 5,
arr[i]>largestElement (TRUE)
// STEP 2) i=1, arr[i] = 1,
arr[i]>largestElement (FALSE),
arr[i]>secondLargestElement (True)
// STEP 3) i=2, arr[i] = 2,
arr[i]>largestElement (FALSE),
arr[i]>secondLargestElement (True)
// STEP 4) i=3, arr[i] = 3,
arr[i]>largestElement (FALSE),
arr[i]>secondLargestElement (True)
// STEP 5) i=4, arr[i] = 6,
arr[i]>largestElement (TRUE)
for(int i=0; i<n; i=i+1)
{
if(arr[i] > largestElement)
{
secondLargestElement =
largestElement; // previous value of largest element
largestElement = arr[i]; //
largestElement is the current element
}
else if(arr[i] > secondLargestElement)
{
secondLargestElement = arr[i];
}
}
// main function is not printing, then who
will print
System.out.println(secondLargestElement);
}
public static void main(String[] args) {
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();
SecondLargest(arr,n);
sc.close();
}
}
20th Feb (Array Problem 6)
import java.util.*;
public class Main {
public static void main(String[] args) throws
Throwable {
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 ans=ArrayProblem6(n,arr);
System.out.println(ans);
}
public static int ArrayProblem6(int n, int[]
arr){
int diff=n+1;
int lastIndexOfEven = -1;
for (int i=0;i<arr.length;i++)
{
if(arr[i]>0 && arr[i]%2==0)
{
if(lastIndexOfEven == -1)
{
lastIndexOfEven = i;
}
else
{
int currentDiff = i -
lastIndexOfEven;
if(currentDiff < diff)
{
diff = currentDiff;
}
lastIndexOfEven = i;
}
}
}
if(diff == (n+1))
{
diff = -1;
}
return diff;
}
}
21st Feb (Largest Number twice that of others)
import java.util.*;
class Solution {
public int LargestElement(int[] nums, int n) {
// Write your code here
int largestElement = nums[0];
int index = 0;
// arr = [1 2 3 4]
// largestElement = 4
// index = 3
for(int i=0;i<n;i=i+1)
{
if(nums[i]>largestElement)
{
largestElement = nums[i];
index = i;
}
}
// STEP 1) i=0, nums[i] = 1, 1!=4 (True) &&
2*1 > 4 (False)
// STEP 2) i=1, nums[i] = 2, 2!=4 (True) &&
2*2 > 4 (False)
// STEP 3) i=2, nums[i] = 3, 3!=4 (True) &&
2*3 > 4 (True)
for(int i=0; i<n; i=i+1)
{
// For every number expect largest
number (we need to see that they should not be twice
the largest number)
if(nums[i] != largestElement && 2 *
nums[i] > largestElement)
{
return -1;
}
}
return index;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int[] nums = new int[n];
for(int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
Solution Obj = new Solution();
System.out.println(Obj.LargestElement(nums,
n));
}
}
21st Feb (Arrays Addition)
// Java program to sum two numbers
// represented two arrays.
import java.util.*;
public class Main {
static int[] calSum(int a[], int b[], int n, int
m) {
// return type is an array
int size = 0;
if(n>m)
{
size = n;
}
else
{
size = m;
}
int result[] = new int[size];
int k = size-1;
// a is the first array, n is size of that array
// a[] = [9, 9] n = 2
// b[] = [9, 9, 9] m = 3
// we need to add numbers from backwards
// we will starting our arrows from last
// Two pointer
int i = n-1; // last index of first array
int j = m-1; // last index of second array
// How to handle the carry part
// Array size will create an issue
int carry = 0;
// carry can never be 2
// a[] = [9, 9] n = 2
// b[] = [9, 9, 9] m = 3
// STEP 1) i=1, j=2, k=2, sum = 9 + 9 + 0 = 18,
carry = 1, result[2] = 8
// STEP 2) i=0, j=1, k=1, sum = 9 + 9 + 1 = 19,
carry = 1, result[1] = 9
// STEP 3) i=-1, j=0, k = 0 (False) i>=0 && j>=0
while(i>=0 && j>=0)
{
int sum = a[i] + b[j] + carry;
carry = sum/10;
result[k] = sum%10; // last digit of sum
// we want to move towards left that is why
decreasing i (first arrow)
i = i - 1;
// we want to move towards left that is why
decreasing j (second arrow)
j = j - 1;
k = k - 1;
}
// result[] = [0 9 8] (After first while loop)
// there is a chance that first array is bigger
while(i>=0)
{
int sum = carry + a[i];
carry = sum/10;
result[k] = sum%10; // last digit of sum
i = i-1;
k = k-1;
}
// this is case where we can see second array is
bigger
while(j>=0)
{
int sum = carry + b[j];
carry = sum/10;
result[k] = sum%10; // last digit of sum
j = j-1;
k = k-1;
}
// result[] = [0 9 8] (After second while loop),
carry = 1
if(carry == 1)
{
// if carry is 1 this means that there will
be some extra digit
int temp[] = new int[size+1]; // [0 0 0 0]
temp[0] = carry;
// temp = [1 0 9 8]
// result[] = [0 9 8] (After second while
loop), carry = 1
// STEP 1) t=1, 1<=3, temp[1] = result[0]
// STEP 2) t=2, 2<=3, temp[2] = result[1]
// STEP 3) t=3, 3<=3, temp[3] = result[2]
for(int t=1; t<=size; t=t+1)
{
temp[t] = result[t-1];
}
return temp;
}
else
{
return result;
}
}
/* Driver program to test above function */
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
int[] arr1 = new int[n1];
for (int i = 0; i < n1; i++) arr1[i] =
sc.nextInt();
int n2 = sc.nextInt();
int[] arr2 = new int[n2];
for (int i = 0; i < n2; i++) arr2[i] =
sc.nextInt();
sc.close();
int[] res = calSum(arr1, arr2, n1, n2);
for (int i : res) System.out.println(i);
}
}
21st Feb (Rotate Array)
import java.util.*;
public class Main {
public static void main(String[] args) throws
Throwable {
solve();
}
public static void solve() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // number of elements
int arr[] = new int[n];
// Initializing array elements
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int k = sc.nextInt(); // No. of times to
rotate
rotateArray(arr, n, k);
}
public static void rotateArray(int arr[], int n,
int k) {
// n = 5, k = 4, 9, 14, 19, 24 (All will
have same rotations)
k = k%n; // we will reduce the value of k if
it is bigger than the array size
int rotatedArray[] = new int[n];
// rotatedArray = [0 0 0 0 0]
// arr = [1 2 3 4 5]
// k = 2, n = 5
// STEP 1) i = 0, newIndex = 0-2 = -2+5 -->
3, rotatedArray[3] = 1 (arr[0] = 1)
// STEP 2) i = 1, newIndex = 1-2 = -1+5 -->
4, rotatedArray[4] = 2 (arr[1] = 2)
// STEP 3) i = 2, newIndex = 2-2 = 0,
rotatedArray[0] = arr[2] = 3
// STEP 4) i = 3, newIndex = 3-2 = 1,
rotatedArray[1] = arr[3] = 4
// STEP 5) i = 4, newIndex = 4-2 = 2,
rotatedArray[2] = arr[4] = 5
// rotatedArray = [3 4 5 1 2]
for(int i=0; i<n; i=i+1)
{
int newIndex = i-k;
if(newIndex<0)
{
newIndex = newIndex + n;
}
rotatedArray[newIndex] = arr[i];
}
// since the return type is void, and main
function is not doing the printing part
for(int i=0; i<n; i=i+1)
{
System.out.print(rotatedArray[i] + " ");
}
21st Feb (Escape Sequence)
public class Main
{
public static void main(String arg[])
{
// \ (It is capable of changing the meaning)
// \n (change the cursor to the nextline)
// \t (tab space)
// \n (This will not be printed)
System.out.print("*\t*");
}
}
22nd Feb (Building Question Logic)
1. Find out the largest number
Total rows = largest numer
2. There will be one loop for number of rows
What is required in that row (Either tab space is
printed or star will be printed)
Formula to identify we discussed
22nd Feb (Subarray Concept Important)
for(int i=0; i<n; i=i+1) // i is denoting the starting
arrow or the fixed point
{
for(int j=i; j<n; j=j+1) // j is denoting
the ending point or ending arrow
{
// STEP 1) i = 0, j = 0 (starting is at
0 and ending is at 0) (size = 1)
// STEP 2) i = 0, j = 1 (starting is at
0 and ending is at 1) (size = 2)
// STEP 3) i = 0, j = 2 (starting is at
0 and ending is at 2) (size = 3)
// STEP 4) i = 0, j = 3 (starting is at
0 and ending is at 3) (size = 4)
// When j for loop is finished then i
will change 1
// STEP 1) i = 1, j = 1 (starting is at
1 and ending is at 1) (size = 1)
// STEP 2) i = 1, j = 2 (starting is at
1 and ending is at 2) (size = 2)
// STEP 3) i = 1, j = 3 (starting is at
1 and ending is at 3) (size = 3)
// When j for loop is finished then i
will change 2
// STEP 1) i = 2, j = 2 (starting is at
2 and ending is at 2) (size = 1)
// STEP 2) i = 2, j = 3 (starting is at
2 and ending is at 3) (size = 2)
// When j for loop is finished then i
will change 3
// STEP 1) i = 3, j = 3 (starting is at
3 and ending is at 3) (size = 1)
}
}
IF questions ask all sizes subarray then only two loops,
if size is fixed, then it not compulsory to use two
loops
Size of subarray is fixed = 4, 2 , 3, 1 (We will always
use single for loops)
22nd Feb (Number of Subarrays with Zero Sum)
import java.util.*;
public class Main {
static void zeroSubarray(int[] arr) {
int n = arr.length; // already discussed
boolean isZeroSumPresent = false;
// int count = 0;
// arr = [3 2 -2 -3], n = 4
// Index- 0 1 2 3
for(int i=0; i<n; i=i+1) // i is denoting the
starting arrow or the fixed point
{
int sum = 0;
// i = 0, j = 0,1,2,3
// i = 1, j = 1,2,3
for(int j=i; j<n; j=j+1) // j is denoting
the ending point or ending arrow
{
sum = sum + arr[j];
if(sum == 0)
{
// count = count + 1;
isZeroSumPresent = true;
System.out.println("Subarray found
from Index " + i + " to " + j);
}
// STEP 1) i = 0, j = 0 (starting is at
0 and ending is at 0) sum = 3
// STEP 2) i = 0, j = 1 (starting is at
0 and ending is at 1) sum = 3 + 2 = 5
// STEP 3) i = 0, j = 2 (starting is at
0 and ending is at 2) sum = 5 - 2 = 3
// STEP 4) i = 0, j = 3 (starting is at
0 and ending is at 3) sum = 3 - 3 = 0
// When j for loop is finished then i
will change 1, sum = 0
// STEP 1) i = 1, j = 1 (starting is at
1 and ending is at 1) sum = 2
// STEP 2) i = 1, j = 2 (starting is at
1 and ending is at 2) sum = 2 - 2 = 0
// STEP 3) i = 1, j = 3 (starting is at
1 and ending is at 3) sum = 0 - 3 = -3
// When j for loop is finished then i
will change 2, sum = 0
// STEP 1) i = 2, j = 2 (starting is at
2 and ending is at 2) sum = -2
// STEP 2) i = 2, j = 3 (starting is at
2 and ending is at 3) sum = -5
// When j for loop is finished then i
will change 3, sum = 0
// STEP 1) i = 3, j = 3 (starting is at
3 and ending is at 3) sum = -3
}
}
if(isZeroSumPresent == false)
{
// we need to print only one time that's why
we write outside the loop
System.out.println("-1");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
zeroSubarray(arr);
sc.close();
}
}
22nd Feb (Number of Subarrays with Zero Sum)
import java.util.*;
public class Main {
static void zeroSubarray(int[] arr) {
int n = arr.length; // already discussed
int count = 0;
// arr = [3 2 -2 -3], n = 4
// Index- 0 1 2 3
for(int i=0; i<n; i=i+1) // i is denoting the
starting arrow or the fixed point
{
int sum = 0;
// i = 0, j = 0,1,2,3
// i = 1, j = 1,2,3
for(int j=i; j<n; j=j+1) // j is denoting
the ending point or ending arrow
{
sum = sum + arr[j];
if(sum == 0)
{
count = count + 1;
System.out.println("Subarray found
from Index " + i + " to " + j);
}
// STEP 1) i = 0, j = 0 (starting is at
0 and ending is at 0) sum = 3
// STEP 2) i = 0, j = 1 (starting is at
0 and ending is at 1) sum = 3 + 2 = 5
// STEP 3) i = 0, j = 2 (starting is at
0 and ending is at 2) sum = 5 - 2 = 3
// STEP 4) i = 0, j = 3 (starting is at
0 and ending is at 3) sum = 3 - 3 = 0
// When j for loop is finished then i
will change 1, sum = 0
// STEP 1) i = 1, j = 1 (starting is at
1 and ending is at 1) sum = 2
// STEP 2) i = 1, j = 2 (starting is at
1 and ending is at 2) sum = 2 - 2 = 0
// STEP 3) i = 1, j = 3 (starting is at
1 and ending is at 3) sum = 0 - 3 = -3
// When j for loop is finished then i
will change 2, sum = 0
// STEP 1) i = 2, j = 2 (starting is at
2 and ending is at 2) sum = -2
// STEP 2) i = 2, j = 3 (starting is at
2 and ending is at 3) sum = -5
// When j for loop is finished then i
will change 3, sum = 0
// STEP 1) i = 3, j = 3 (starting is at
3 and ending is at 3) sum = -3
}
}
if(count == 0)
{
// we need to print only one time that's why
we write outside the loop
System.out.println("-1");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
zeroSubarray(arr);
sc.close();
}
}
22nd Feb (Number of Subarrays with Sum Divisible by K)
import java.io.*;
import java.util.*;
public class Main {
public static int subarrayDivisbleByK(int arr[], int
n, int k){
int count = 0;
for(int i=0; i<n; i=i+1) // i is denoting the
starting arrow or the fixed point
{
int sum = 0;
for(int j=i; j<n; j=j+1) // j is denoting
the ending point or ending arrow
{
sum = sum + arr[j];
if(sum%k == 0) // sum is divisible by k
{
count = count + 1;
}
// STEP 1) i = 0, j = 0 (starting is at
0 and ending is at 0) (size = 1)
// STEP 2) i = 0, j = 1 (starting is at
0 and ending is at 1) (size = 2)
// STEP 3) i = 0, j = 2 (starting is at
0 and ending is at 2) (size = 3)
// STEP 4) i = 0, j = 3 (starting is at
0 and ending is at 3) (size = 4)
// When j for loop is finished then i
will change 1
// STEP 1) i = 1, j = 1 (starting is at
1 and ending is at 1) (size = 1)
// STEP 2) i = 1, j = 2 (starting is at
1 and ending is at 2) (size = 2)
// STEP 3) i = 1, j = 3 (starting is at
1 and ending is at 3) (size = 3)
// When j for loop is finished then i
will change 2
// STEP 1) i = 2, j = 2 (starting is at
2 and ending is at 2) (size = 1)
// STEP 2) i = 2, j = 3 (starting is at
2 and ending is at 3) (size = 2)
// When j for loop is finished then i
will change 3
// STEP 1) i = 3, j = 3 (starting is at
3 and ending is at 3) (size = 1)
}
}
return count;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;++i){
arr[i]=sc.nextInt();
}
int result = subarrayDivisbleByK(arr, n, k);
System.out.print(result);
System.out.println('\n');
}
}
22nd Feb (Find Split Point)
import java.io.*;
import java.util.*;
public class Main {
static void findSplitPoint(int arr[], int n) {
// find total sum
int totalSum = 0;
for(int i=0; i<n; i=i+1)
{
totalSum = totalSum + arr[i];
}
// arr = 6 -4 3 2 -3
// totalSum = 4
// STEP 1) i=0, sumOfFirstPart = 6,
sumOfSecondPart = 4 - 6 = -2
// STEP 2) i=1, sumOfFirstPart = 2,
sumOfSecondPart = 4 - 2 = 2, indexOfPartition = 1
int sumOfFirstPart = 0;
// [1 2 3 4 5] in this case we need to print
(Not possible)
int indexOfPartition = -1;
for(int i=0; i<n; i=i+1)
{
sumOfFirstPart = sumOfFirstPart + arr[i];
int sumOfSecondPart = totalSum -
sumOfFirstPart;
if(sumOfFirstPart == sumOfSecondPart)
{
indexOfPartition = i;
break;
}
}
if(indexOfPartition == -1)
{
System.out.println("Not Possible");
}
else
{
// 6 -4 3 2 -3
// 0 1 2 3 4
// indexOfPartition = 1
// print from 0 to 1 (6 -4 will be printed)
(FIRST PART)
// print from 2 to last element (3 2 -3)
(Second PART)
// First part of the array
for(int i=0; i<=indexOfPartition; i=i+1)
{
System.out.print(arr[i] + " ");
}
System.out.println();
// Second part of the array
for(int i=indexOfPartition+1; i<n; i=i+1)
{
System.out.print(arr[i] + " ");
}
}
}
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();
}
findSplitPoint(arr, n);
}
}
22nd Feb (Find Geometric Triplet)
import java.util.*;
public class Main {
public static void findGeometricTriplets(int arr[],
int n) {
// Write code here
// 2 3 4 5 6 8
for(int i=0; i<n; i=i+1) // first element
possible values
{
for(int j=i+1; j<n; j=j+1) // second element
possible values
{
for(int k=j+1; k<n; k=k+1) // element
element possible values
{
// if k=i+2 (This is wrong) (i = 1,
j = 2, k = 2)
// 5/3 = 1
// 7/5 = 1
double a = arr[i];
double b = arr[j];
double c = arr[k];
if(b/a == c/b)
{
System.out.println(arr[i] + " "
+ arr[j] + " " + arr[k]);
}
}
}
}
}
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();
}
findGeometricTriplets(arr, N);
sc.close();
}
}
23rd Feb (Pairs Question)
import java.io.*;
import java.util.*;
class Solution {
public int Pairs(int []arr,int k){
int n = arr.length;
int count = 0;
// arr = [2 3 4 5], k = 2
for(int i=0; i<n; i=i+1)
{
// i is representing the fixed element
// j is representing all pairs that can be
clubbed with fixed element
for(int j=i+1; j<n; j=j+1)
{
// Math.abs(-ve number) = +ve number (It
can convert negative number to positive number)
// Math.abs(-5) = 5 (-ve to +ve)
// Math.abs(5) = 5 (+ve number will
remain as +ve)
if(Math.abs(arr[i] - arr[j]) == k)
{
count = count + 1;
}
// if((arr[i] - arr[j] == k) || (arr[i]
- arr[j] == -k))
// {
// count = count + 1;
// }
// STEP 1) i = 0, j = 1 (arr[i], arr[j])
- (2, 3)
// STEP 2) i = 0, j = 2 (2, 4)
// STEP 2) i = 0, j = 3 (2, 5)
// STEP 1) i = 1, j = 2 - (3, 4)
// STEP 2) i = 1, j = 3 - (3, 5)
}
}
return count;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n,k;
n = sc.nextInt();
k = sc.nextInt();
int []arr=new int[n];
for(int i=0;i<n;++i){
arr[i]=sc.nextInt();
}
Solution Obj = new Solution();
System.out.println(Obj.Pairs(arr,k));
}
}
23rd Feb (Time Complexity - 1)
public class Main
{
public static void main(String arg[])
{
System.out.println("Hello, World."); // Single
Operation
int a = 5; // Single Operation
for(int i=1; i<=5; i=i+1) // 5 operations
{
System.out.println("Hello, World.");
}
// 25 operations
for(int i=1; i<=5; i=i+1)
{
// i = 1, j will run 5 times
// i = 2, j will run 5 times
// i = 3, j will run 5 times
// i = 4, j will run 5 times
for(int j=1; j<=5; j=j+1)
{
System.out.println("Hello, World.");
}
}
// Lesser the operation, lesser will be the
time taken
// BEST program will be the one which least
number of operation
// Basic way to do any question is the brute
force way (It will do more operations)
// Optimal way to do any question is using
minimum operations possible
}
}
23rd Feb (Time Complexity - 2)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
// Single Operation
// Time Complexity of below line is O(1)
System.out.println("Hello, World.");
// Single Operation, Time Complexity of this
line is O(1)
int a = 5;
// N operations
// Time complexity - O(N)
for(int i=1; i<=n; i=i+1)
{
System.out.println("Hello, World.");
}
// operations - m
// Time complexity - O(M)
for(int i=1; i<=m; i=i+1)
{
System.out.println("Hello, World.");
}
// operations - n+m
// Time complexity - O(N+M) (capital letter
does not matter)
for(int i=1; i<=(n+m); i=i+1)
{
System.out.println("Hello, World.");
}
// RULE --> Time complexity will never include
any constant except O(1)
// operations - N/2
// Time complexity - O(N)
for(int i=1; i<=n; i=i+2)
{
System.out.println("Hello, World.");
}
// operations - 2*n
// Time complexity - O(N)
for(int i=1; i<=2*n; i=i+1)
{
System.out.println("Hello, World.");
}
// operations - 5*n
// Time complexity - O(N)
for(int i=1; i<=5*n; i=i+1)
{
System.out.println("Hello, World.");
}
// operations - 10*n+17
// Time complexity - O(N)
for(int i=1; i<=(10*n+17); i=i+1)
{
System.out.println("Hello, World.");
}
// operations - 10*n+17*m
// Time complexity - O(N+M)
for(int i=1; i<=(10*n+17*m); i=i+1)
{
System.out.println("Hello, World.");
}
// operations - n*n
// Time complexity - O(N*N)
for(int i=1; i<=n; i=i+1)
{
// i = 1, j will run n times
// i = 2, j will run n times
// i = 3, j will run n times
// i = 4, j will run n times
for(int j=1; j<=n; j=j+1)
{
System.out.println("Hello, World.");
}
}
// operations - 12*n*n
// Time complexity - O(n*n) / O(n^2)
for(int i=1; i<=2*n; i=i+1) // 2*n times
{
// i = 1, j will run n times
// i = 2, j will run n times
// i = 3, j will run n times
// i = 4, j will run n times
for(int j=1; j<=6*n; j=j+1) // 6*n times
{
System.out.println("Hello, World.");
}
}
// operations - n*m/6
// Time complexity - O(n*m)
for(int i=1; i<=n; i=i+2) // n/2 times
{
for(int j=1; j<=m; j=j+3) // m/3 times
{
System.out.println("Hello, World.");
}
}
// operations - 12*n*n*n
// Time complexity - O(N^3) / O(n*n*n)
for(int i=1; i<=2*n; i=i+1)
{
for(int j=1; j<=6*n; j=j+1)
{
for(int k=1; k<=n; k=k+1)
{
System.out.println("Hello world");
}
}
}
// operations = n-5
// Time Complexity = O(N)
for(int i=1; i<=(n-5); i=i+1)
{
}
// Lesser the operation, lesser will be the
time taken
// BEST program will be the one which least
number of operation
// Basic way to do any question is the brute
force way (It will do more operations)
// Optimal way to do any question is using
minimum operations possible
}
}
23rd Feb (Time Complexity - 3)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
// Time complexity of a program = Time
complexity of that part of code which
// affects the program the most
// operations = n
// Time complexity - O(N)
for(int i=1; i<=n; i=i+2)
{
System.out.println("Hello, World.");
}
// operations - 12*n*n*n
// Time complexity - O(N^3) / O(n*n*n)
for(int i=1; i<=2*n; i=i+1)
{
for(int j=1; j<=6*n; j=j+1)
{
for(int k=1; k<=n; k=k+1)
{
System.out.println("Hello world");
}
}
}
// Time complexity of whole program - O(N*N*N)
}
}
23rd Feb (Time Complexity - 4)
import java.util.Scanner;
public class Main
{
public static int add(int a,int b)
{
return (a+b);
}
public static void print(int n)
{
// Operation - n+1
// Time Complexity - O(N)
for(int i=0; i<=n; i=i+1)
{
// Something
}
}
public static void main(String arg[])
{
// Operation = 1
// Time Complexity - O(1)
int c = add(5,5);
Scanner in = new Scanner(System.in);
int n = in.nextInt();
print(n); // Operation = n+1, Time Complexity -
O(N)
// Time Complexity of the whole program - O(N)
}
}
23rd Feb (Time Complexity - 5)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
int n = in.nextInt();
// Operations - log n
// Time Complexity - O(log n)
for(int i=1; i<=n; i=i*2) // log n
{
System.out.println("Anything");
}
// Operations - n-1
// Time Complexity - O(N)
int j = 1;
while(j<n) // n-1 times loop will run
{
j = j+1;
}
// Operations - log n
// Time Complexity - O(log n)
int j = 1;
while(j<n) // log n times loop will run
{
j = j*3;
}
// Time complexity of the program - O(N)
// O(1) < O(log n) < O(N) < O(N^2) < O(N^3)
// O(sqrt(N)), O(n logn)
}
}
23rd Feb (Time Complexity - 6)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt(); // n = 10^5
// RULE - Approx 10^8 operations in one second
// Time limit will be 1 second
// Maximum operations which we can do in our
program < 10^8
// operations = n*n = 10^10
// Error - Time Limit Exceeded
for(int i=1; i<=n; i=i+1)
{
for(int j=1; j<=n; j=j+1)
{
}
}
}
}
23rd Feb (Time Complexity - 7)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt(); // n = 10^5
// Infinite Loop (Yes here time limit will
exceed, program will never stop)
for(int i=0; i<n; i=i*2)
{
// i = 0, so it will always remain zero
}
// If your does more number of operation than
10^8 then we get time limit exceeded
// log n
// Time Complexity - O(log n)
for(int i=1; i<n; i=i*2)
{
}
}
}
23rd Feb (Time Complexity - 8)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// Let's suppose n = 10^9
// Operations = log n
// Time Complexity - O(log n)
for(int i=1; i<n; i=i*2)
{
}
}
}
23rd Feb (Time Complexity - 9)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// Sum of n natural number (1 + 2 + 3 + 4....
n) = n*(n+1)/2
// i = 0, j for loop will run 1 time
// i = 1, j for loop will run 2 times
// i = 2, j for loop will run 3 times
// i = 3, j for loop will run 4 times
// i = n-1, j for loop will run n times
// Operations = 1+2+3+4+5...n = n*(n+1)/2
// Time Complexity - O(N^2)
for(int i=0; i<n; i++)
{
for(int j=0; j<=i; j=j+1)
{
}
}
}
}
23rd Feb (Time Complexity - 10)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// Operations - N^(1/2)
// Time Complexity - sqrt(n) or N^(1/2)
for(int i=1; i<=(int)Math.sqrt(n); i=i+1)
{
// O(1) < O(log n) < O(sqrt(n)) < O(N) <
O(N*logn) < O(N^2) < O(N^3)
// For sorting the array - O(n*log n)
}
}
23rd Feb (Time Complexity - 11)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// Operations - N^(1/2)
// Time Complexity - sqrt(n) or N^(1/2)
for(int i=1; i<=(int)Math.sqrt(n); i=i+1)
{
// last value of i will sqrt(N), Loop will stop
when i>sqrt(N)
// Operations - sqrt(n) or N^(1/2)
// Time Complexity - sqrt(n) or N^(1/2)
for(int i=1; i*i<=n; i=i+1) // sqrt(N)
{
// O(1) < O(log n) < O(sqrt(n)) < O(N) <
O(N*logn) < O(N^2) < O(N^3)
// For sorting the array - O(n*log n)
}
}
23rd Feb (Time Complexity - 12)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// Operations - N^(1/2)
// Time Complexity - sqrt(n) or N^(1/2)
for(int i=1; i<=(int)Math.sqrt(n); i=i+1)
{
// last value of i will sqrt(N), Loop will stop
when i>sqrt(N)
// Operations - sqrt(n) or N^(1/2)
// Time Complexity - sqrt(n) or N^(1/2)
for(int i=1; i*i<=n; i=i+1) // sqrt(N)
{
// Operations -> nlog n
// Time Complexity -> O(nlog n)
for(int i=1; i<=n; i=i+1) // n times
{
for(int j=1; j<=n; j=j*2) // log n times
{
}
}
// Operations = 100
// Time Complexity - O(1)
for(int i=0; i<100; i=i+1)
{
}
// Operations = log n
// Time Complexity - O(log n)
int k = 1;
while(k<n)
{
k = k*10;
}
// Operations = log n
// Time Complexity - O(log n)
int j = n;
while(j>0)
{
j = j/10;
}
// Time Complexity of the program - O(nlog n)
// O(1) < O(log n) < O(sqrt(n)) < O(N) <
O(N*logn) < O(N^2) < O(N^3)
// For sorting the array - O(n*log n)
}
}
23rd Feb (Saturday Discussion)
Saturday Questions
Array Subtraction
Sorted Insertion
Subarray Print
24rd Feb (Module Test)
Giving the contest is compulsory
Module Test
- Attempt all 3 questions (Total 90 mins)
- 30 mins each (5 mins time on reading the question,
5-10 mins we will spend on logic,
Rest on coding that question)
- 3 questions (Loops/ Nested Loops
1D array/Subarray/ Pairs (17th Feb, 22th
Feb, 25th Feb)
2D arrays (24th, 27th, 28th)
- EASY level if you have understand the doc and done
revision
- 4th March (Module TEST date (CODING + APTI))
8pm to 12 am
- No classes on 1st, 2nd and 3rd March
- (1st, 2nd & 3rd BREAK for revision)
- MOCK Interview for Module 1 (- 1/2 coding question
- 1/2 Theory (TIME COMPLEXITY))
- Submit as many as you can module test
25th Saturday Revision class (1 PM - 3:30 PM)
24th Feb (2D arrays - 1)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
int n = in.nextInt();
int b = 100;
int c = 10;
int z = 5;
// Space complexity - O(n)
int arr[] = new int[5*n];
// RULE -> Data structure can affect the space complexity
only
// Only when we create a data structure
// Optimal program - less time as well as less space
// Minimize time by minimizing operations
// operations - N
// Time complexity - O(N)
for(int i=1; i<= n; i=i+1)
{
// operations - 5*n
// Time complexity - O(N)
for(int i=1; i<= 5*n; i=i+1)
{
}
}
}
24th Feb (Print 2D array)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int rows = in.nextInt(); // 4
int columns = in.nextInt(); // 3
// SPACE complexity - O(rows * columns)
int arr[][] = new int[rows][columns];
// datatype nameOf2DArray[][] = new datatype[]
[];
for(int i=0; i<rows; i=i+1)
{
for(int j=0; j<columns; j=j+1)
{
arr[i][j] = in.nextInt();
// printing can be done here as well
// System.out.print(arr[i][j]+" ");
// STEP 1) i=0, j=0
// STEP 2) i=0, j=1
// STEP 3) i=0, j=2
// STEP 1) i=1, j=0
// STEP 2) i=1, j=1
// STEP 3) i=1, j=2
}
}
// printing
for(int i=0; i<rows; i=i+1)
{
for(int j=0; j<columns; j=j+1)
{
// STEP 1) i=0, j=0, arr[0][0] = 1
// STEP 2) i=0, j=1, arr[0][1] = 2
// STEP 3) i=0, j=2, arr[0][2] = 3
// STEP 1) i=1, j=0
// STEP 2) i=1, j=1
// STEP 3) i=1, j=2
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
// Total Time complexity - O(n*m)
}
}
24th Feb (Print 2D array in Function)
import java.util.Scanner;
public class Main
{
public static void print(int arr)
{
int rows = arr.length;
int columns = arr[0].length;
// arr[0] --> first row
// (we are finding size of first row that is equal to
number of columns)
// printing
for(int i=0; i<rows; i=i+1)
{
for(int j=0; j<columns; j=j+1)
{
// STEP 1) i=0, j=0, arr[0][0] = 1
// STEP 2) i=0, j=1, arr[0][1] = 2
// STEP 3) i=0, j=2, arr[0][2] = 3
// STEP 1) i=1, j=0
// STEP 2) i=1, j=1
// STEP 3) i=1, j=2
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int rows = in.nextInt(); // 4
int columns = in.nextInt(); // 3
// SPACE complexity - O(rows * columns)
int arr[][] = new int[rows][columns];
// datatype nameOf2DArray[][] = new datatype[][];
for(int i=0; i<rows; i=i+1)
{
for(int j=0; j<columns; j=j+1)
{
arr[i][j] = in.nextInt();
// printing can be done here as well
// System.out.print(arr[i][j]+" ");
// STEP 1) i=0, j=0
// STEP 2) i=0, j=1
// STEP 3) i=0, j=2
// STEP 1) i=1, j=0
// STEP 2) i=1, j=1
// STEP 3) i=1, j=2
}
}
print(arr, rows, columns);
// Total Time complexity - O(n*m)
}
}
24th Feb (Print 2D array column wise)
import java.util.*;
class Solution {
public void printMatrixColumnwise(int[][] mat, int n, int m) {
// Write code here and print output
for(int j=0; j<m; j=j+1)
{
for(int i=0; i<n; i=i+1)
{
// STEP 1) i=0, j=0
// STEP 2) i=1, j=0
// STEP 3) i=2, j=0
// STEP 1) i=0, j=1
System.out.print(mat[i][j] + " ");
}
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t;
t = sc.nextInt();
while (t > 0) {
int n;
int m;
n = sc.nextInt();
m = sc.nextInt();
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
matrix[i][j] = sc.nextInt();
Solution Obj = new Solution();
Obj.printMatrixColumnwise(matrix, n, m);
System.out.println();
t--;
}
sc.close();
}
}
24th Feb (Alternate matrix traversal)
import java.util.*;
class Solution {
public void printElementsAlternately(int[][] mat,int m,int n)
{
// m - rows = 3
// n - columns = 3
for(int i=0; i<m; i=i+1)
{
// First row, i = 0
// Second row, i = 1
// Third row, i = 2
// Fourth row, i = 3
// Odd rows -> Left to right
// Even rows -> Right to left
// STEP 1) i = 0
// STEP 2) i = 1
if(i%2 == 0) // First row, third row
{
// odd row
for(int j=0; j<n; j=j+1)
{
// STEP 1) i=0, j=0, mat[0][0] = 7
// STEP 2) i=0, j=1, mat[0][1] = 2
// STEP 3) i=0, j=2, mat[0][2] = 3
System.out.print(mat[i][j]+" ");
}
}
else // second row, fourth row
{
// even row
for(int j=n-1; j>=0; j=j-1)
{
// STEP 1) i=1, j=2, mat[1][2] = 4
// STEP 2) i=1, j=1, mat[1][1] = 3
// STEP 3) i=1, j=2, mat[1][0] = 2
System.out.print(mat[i][j]+" ");
}
}
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m, n;
m = sc.nextInt();
n = sc.nextInt();
int[][] mat = new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
mat[i][j] = sc.nextInt();
Solution Obj = new Solution();
Obj.printElementsAlternately(mat,m,n);
sc.close();
}
}
24th Feb (Alternate matrix traversal)
import java.util.*;
class Solution {
public void printElementsAlternately(int[][] mat,int m,int n)
{
// m - rows = 3
// n - columns = 3
for(int i=0; i<m; i=i+1)
{
// First row, i = 0
// Second row, i = 1
// Third row, i = 2
// Fourth row, i = 3
// Odd rows -> Left to right
// Even rows -> Right to left
// STEP 1) i = 0
// STEP 2) i = 1
if(i%2 == 0) // First row, third row
{
// odd row
for(int j=0; j<n; j=j+1)
{
// STEP 1) i=0, j=0, mat[0][0] = 7
// STEP 2) i=0, j=1, mat[0][1] = 2
// STEP 3) i=0, j=2, mat[0][2] = 3
System.out.print(mat[i][j]+" ");
}
}
else // second row, fourth row
{
// even row
for(int j=n-1; j>=0; j=j-1)
{
// STEP 1) i=1, j=2, mat[1][2] = 4
// STEP 2) i=1, j=1, mat[1][1] = 3
// STEP 3) i=1, j=2, mat[1][0] = 2
System.out.print(mat[i][j]+" ");
}
}
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m, n;
m = sc.nextInt();
n = sc.nextInt();
int[][] mat = new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
mat[i][j] = sc.nextInt();
Solution Obj = new Solution();
Obj.printElementsAlternately(mat,m,n);
sc.close();
}
}
24th Feb (Transpose of matrix)
import java.util.*;
import java.io.*;
class Solution {
public int[][] matrixTranspose(int[][] mat, int n) {
// TIME COMPLEXITY - Operations (Loops)
// Space COMPLEXITY - Data Structure creation (size of
data Structure will affect)
// TIME COMPLEXITY OF answer code (Line 13 - 30) - O(N*N)
// SPACE COMPLEXITY OF answer code (Line 13 - 30) - O(1)
// Square matrix means both rows and columns are equal
for(int i=0; i<n; i=i+1)
{
for(int j=0; j<n; j=j+1)
{
if(i>j)
{
// lower elements
// swap (i,j) with (j,i)
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
}
return mat;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
// space COMPLEXITY - O(N*N)
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = sc.nextInt();
Solution Obj = new Solution();
int[][] ans = Obj.matrixTranspose(matrix, n);
for(int i=0; i<n; i++)
{ for(int j=0; j<n; j++)
{
System.out.print(ans[i][j] + " ");
} System.out.println("");
}
sc.close();
// Time COMPLEXITY of whole program - O(N^2)
// space COMPLEXITY of whole program - O(N^2)
}
}
24th Feb (Printing the diagonals)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int rows = in.nextInt(); // 4
int columns = in.nextInt(); // 3
// SPACE complexity - O(rows * columns)
int arr[][] = new int[rows][columns];
// datatype nameOf2DArray[][] = new datatype[][];
for(int i=0; i<rows; i=i+1)
{
for(int j=0; j<columns; j=j+1)
{
arr[i][j] = in.nextInt();
// printing can be done here as well
// System.out.print(arr[i][j]+" ");
// STEP 1) i=0, j=0
// STEP 2) i=0, j=1
// STEP 3) i=0, j=2
// STEP 1) i=1, j=0
// STEP 2) i=1, j=1
// STEP 3) i=1, j=2
}
}
// i>j lower triangle
// i<j upper triangle
// i==j left diagonal
// i+j == (n-1) right diagonal
// printing
for(int i=0; i<rows; i=i+1)
{
for(int j=0; j<columns; j=j+1)
{
if(i==j || (i+j) == (rows-1))
{
System.out.print(arr[i][j]+" ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
// Total Time complexity - O(n*m)
}
}
25th Feb (Printing the subarray)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// Subarray --> Fixing concept (ending = i, j=i)
// Question on pairs --> Fixing concept (ending = i+1)
int n = in.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i=i+1)
{
arr[i] = in.nextInt();
}
// TIME COMPLEXITY - O(N*N*N) / O(N^3)
for(int i=0; i<n; i=i+1) // staring point
{
for(int j=i; j<n; j=j+1) // ending point
{
// i is representing staring position
// j is representing ending position
for(int k=i; k<=j; k=k+1) // print Subarray from i
to j
{
System.out.print(arr[k]+" ");
}
System.out.println();
}
}
}
}
25th Feb (Print all pairs in array)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// Subarray --> Fixing concept (ending = i, j=i)
// Question on pairs --> Fixing concept (ending = i+1)
int n = in.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i=i+1)
{
arr[i] = in.nextInt();
}
// PRINT ALL PAIRS IN ARRAY
for(int i=0; i<n; i=i+1) // staring point
{
for(int j=i+1; j<n; j=j+1) // ending point
{
System.out.println(arr[i]+" "+arr[j]);
}
}
}
}
25th Feb (Sum of all pairs - 2 Elements)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// Subarray --> Fixing concept (ending = i, j=i)
// Question on pairs --> Fixing concept (ending = i+1)
int n = in.nextInt();
int arr[] = new int[n];
int k = in.nextInt();
for(int i=0; i<n; i=i+1)
{
arr[i] = in.nextInt();
}
// PRINT ALL PAIRS IN ARRAY which have sum as k
// arr = [1, 2, 3, 4]
for(int i=0; i<n; i=i+1) // staring point
{
for(int j=i+1; j<n; j=j+1) // ending point
{
// STEP 1) i = 0, j = 1, arr[i] = 1, arr[j] = 2
// STEP 2) i = 0, j = 2, arr[i] = 1, arr[j] = 3
// STEP 3) i = 0, j = 3, arr[i] = 1, arr[j] = 4
// STEP 4) i = 1, j = 2, arr[i] = 2, arr[j] = 3
if(arr[i] + arr[j] == k)
{
System.out.println(arr[i]+" "+arr[j]);
}
}
}
}
}
25th Feb (All 3 pairs)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// Subarray --> Fixing concept (ending = i, j=i)
// Question on pairs --> Fixing concept (ending = i+1)
int n = in.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i=i+1)
{
arr[i] = in.nextInt();
}
// PRINT ALL 3 PAIRS IN ARRAY
// arr = [1, 2, 3, 4, 5]
for(int i=0; i<n; i=i+1) // staring point
{
for(int j=i+1; j<n; j=j+1) // second element possible
values
{
for(int k=j+1; k<n; k=k+1) // third element
possible values
{
System.out.println(arr[i] + " " + arr[j] + " "
+ arr[k]);
}
}
}
}
}
25th Feb (Array Subtraction)
// Java program to sum two numbers
// represented two arrays.
import java.util.*;
public class Main {
// converting array to int/long (Leetcode, companies test it
will not pass)
static boolean biggerCheck(int n1[], int n2[])
{
// n1 = [1, 3, 1, 0]
// n2 = [1, 3, 0, 1]
// STEP 3) i = 2 (The loop will break, first number will
be bigger)
// what is each element of array denoting - (digits of a
number)
if(n1.length > n2.length)
{
return true;
}
else if(n1.length < n2.length)
{
return false;
}
else
{
// when their lengths are equal
// n1 = [1, 3, 0, 0]
// n2 = [1, 3, 0, 1]
// STEP 1) i = 0, both condition are false (1>1, 1<1)
// STEP 2) i = 1, both condition are false (3>3, 3<3)
// STEP 3) i = 2, both condition are false (0>0, 0<0)
// STEP 4) i = 3, (0>1 false) (0<1 TRUE (Second number
is bigger))
for(int i=0; i<n1.length; i=i+1)
{
if(n1[i] > n2[i])
{
return true; // n1 is bigger
}
else if(n1[i] < n2[i])
{
return false; // n2 is bigger
}
}
// when numbers are same
return true;
}
}
static int[] subtraction(int bigger[], int smaller[])
{
// bigger - smaller
// 100 - 99 = 001
int answer[] = new int[bigger.length];
int n = bigger.length;
int m = smaller.length;
int i = n-1;
int j = m-1;
int borrow = 0;
while(i>=0 && j>=0)
{
if(bigger[i]-borrow < smaller[j])
{
answer[i] = bigger[i] - borrow + 10 - smaller[j];
borrow = 1;
}
else
{
answer[i] = bigger[i] - borrow - smaller[j];
borrow = 0;
}
i = i-1;
j = j-1;
}
while(i>=0)
{
if(bigger[i] - borrow < 0)
{
answer[i] = bigger[i] - borrow + 10;
borrow = 1;
}
else
{
answer[i] = bigger[i] - borrow;
borrow = 0;
}
i = i - 1;
}
return answer;
}
static int[] removeExtraZeroFromFront(int result[])
{
int i = 0;
int n = result.length;
while(i<n)
{
if(result[i] != 0)
{
// first non zero digit we found
break;
}
i = i + 1;
}
if(i == n)
{
// all the digts are zero, there will be no non zero
digit
int ans[] = new int[1];
return ans;
}
// i is referring to first non zero digit
int size = n-i;
int answer[] = new int[size];
int j = 0;
while(j<size)
{
answer[j] = result[i];
j = j + 1;
i = i + 1;
}
return answer;
}
static int[] subtract(int[] n1, int[] n2) {
// 1. Find the bigger number
boolean isFirstNumberBiggerThanSecondNumber =
biggerCheck(n1, n2);
// declaring the array
int result[];
int sign = 1;
// 2. Bigger - Smaller number
if(isFirstNumberBiggerThanSecondNumber)
{
result = subtraction(n1, n2);
}
else
{
sign = -1;
result = subtraction(n2, n1);
}
// 3. There can be leading zero (there can zero's in front
100 - 99 = 001)
// These extra zero's need to removed
result = removeExtraZeroFromFront(result);
// 4. Second > First (Final result is negative, we need to
add negative sign)
result[0] = sign * result[0];
return result;
}
/* Driver program to test above function */
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
int[] arr1 = new int[n1];
for (int i = 0; i < n1; i++) arr1[i] = sc.nextInt();
int n2 = sc.nextInt();
int[] arr2 = new int[n2];
for (int i = 0; i < n2; i++) arr2[i] = sc.nextInt();
sc.close();
int[] res = subtract(arr1, arr2);
for (int i : res) System.out.println(i);
}
}
25th Feb (Print starting point, ending point, size of Subarray)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
// Subarray --> Fixing concept (ending = i, j=i)
// Question on pairs --> Fixing concept (ending = i+1)
int n = in.nextInt();
int arr[] = new int[n];
for(int i=0; i<n; i=i+1)
{
arr[i] = in.nextInt();
}
// TIME COMPLEXITY - O(N*N*N) / O(N^3)
for(int i=0; i<n; i=i+1) // staring point
{
for(int j=i; j<n; j=j+1) // ending point
{
// starting point, ending point and the sizes
System.out.println(i+", "+j+", " + (j-i+1));
}
}
}
}
26th Feb (Contest Questions)
https://course.acciojob.com/idle?question=b3225a96-8c0e-490d-8d01-e7f2fd842eac
https://course.acciojob.com/idle?question=62cedbb1-3c4a-404d-a05e-edd6a693aee8
https://course.acciojob.com/idle?question=a0c7a596-d981-45bb-9c68-4a26a2ea0156
26th Feb (Maximum Profit - Columns)
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void solve(int a[][],int n,int m) {
// profit = sum of all elements in a single column
// n - rows
// m - columns
int largestSum = 0;
for(int j=0; j<m; j=j+1)
{
int sum = 0;
// j = 0, sum = 0
// j = 1, sum = 0
for(int i=0; i<n; i=i+1)
{
// i = 0, sum = sum + a[0][0]
// i = 1, sum = sum + a[1][0]
// i = 2, sum = sum + a[2][0]
sum = sum + a[i][j];
}
if(sum > largestSum)
{
largestSum = sum;
}
System.out.println(largestSum);
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int a[][] = new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++)
a[i][j]=sc.nextInt();
}
solve(a,n,m);
}
}
26th Feb (First and Last Index)
import java.util.*;
class Solution {
static void findPosition(int a[], int n,int k)
{
int firstOccurence = -1;
int lastOccurence = -1;
// a = [6 8 2 3 8 8 7 8], k = 8
// 0 1 2 3 4 5 6 7
// firstOccurence = 1
// a = [6 7 4 1 2 3 9 5], k = 8
// firstOccurence = -1
for(int i=0; i<n; i=i+1)
{
if(a[i] == k)
{
firstOccurence = i;
break; // loop stops
}
}
for(int i=(n-1); i>=0; i=i-1)
{
if(a[i] == k)
{
lastOccurence = i;
break;
}
}
System.out.println(firstOccurence + " " + lastOccurence);
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n= sc.nextInt();
int k= sc.nextInt();
int array[] = new int[n];
for(int i=0; i<n; i++){
array[i]= sc.nextInt();
}
Solution Obj = new Solution();
Obj.findPosition(array,n,k);
}
}
26th Feb (Print Diamond Pattern Contest)
import java.util.*;
public class Main{
// Driver Code
public static void main(String[] args)
{
int r;
Scanner in = new Scanner(System.in);
r = in.nextInt();
printDiamond(r);
}
static void printDiamond(int n)
{
for(int i=1; i<=n; i=i+1) // rows
{
// i = 5, spaces = 0, stars = 5, n = 5, n-i = 0
(Spaces)
// i = 4, spaces = 1, stars = 4, n = 5, n-i = 1
(Spaces)
// i = 3, spaces = 2, stars = 3, n = 5, n-i = 2
(Spaces)
// i = 2, spaces = 3, stars = 2, n = 5, n-i = 3
(Spaces)
// i = 1, spaces = 4, stars = 1, n = 5, n-i = 4
(Spaces)
// spaces
for(int j=1; j<=n-i; j=j+1)
{
System.out.print(" ");
}
// stars
for(int j=1; j<=i; j=j+1)
{
System.out.print("* ");
}
System.out.println();
}
// Lower Diamond
for(int i=n; i>=1; i=i-1) // rows
{
// i = 5, spaces = 0, stars = 5, n = 5, n-i = 0
(Spaces)
// i = 4, spaces = 1, stars = 4, n = 5, n-i = 1
(Spaces)
// i = 3, spaces = 2, stars = 3, n = 5, n-i = 2
(Spaces)
// i = 2, spaces = 3, stars = 2, n = 5, n-i = 3
(Spaces)
// i = 1, spaces = 4, stars = 1, n = 5, n-i = 4
(Spaces)
// spaces
for(int j=1; j<=n-i; j=j+1)
{
System.out.print(" ");
}
// stars
for(int j=1; j<=i; j=j+1)
{
System.out.print("* ");
}
System.out.println();
}
}
}
26th Feb (Sorted Insert Position)
import java.io.*;
import java.util.*;
public class Main {
public static int searchInsert(int[] a, int b) {
int n = a.length;
int index = -1;
for(int i=0; i<n; i=i+1)
{
if(a[i] == b || b<a[i])
{
index = i;
break;
}
}
if(index == -1)
{
index = n;
}
return index;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] A = new int[N];
for(int i=0;i<N;i++){
A[i] = sc.nextInt();
}
int B = sc.nextInt();
System.out.println(searchInsert(A,B));
}
}
26th Feb (Automorphic Number GYM)
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
// square will overflow
long square = n*n;
while(n>0)
{
long lastDigit1 = n%10L;
long lastDigit2 = square%10L;
if(lastDigit2!=lastDigit1)
{
break;
}
n = n/10l;
square = square/10L;
}
if(n == 0L)
{
// all digits were matched
System.out.print("1");
}
else
{
System.out.print("0");
}
}
26th Feb (Building Code)
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
max = Math.max(max, arr[i]);
}
sc.close();
barGraph(arr, n);
}
public static void barGraph(int[] arr, int n) {
int largestElement = 0;
for(int i=0; i<n; i=i+1)
{
if(arr[i]>largestElement)
{
largestElement = arr[i];
}
}
for(int i=1; i<=largestElement; i=i+1) // rows
{
// arr = [3 4 1 2 5], largestElement = 5
// i = 1, ROW 1, largestElement = 5, j = 0 arr[j] = 3,
1> 5-3 Space will printed
// i = 1, ROW 1, largestElement = 5, j = 1 arr[j] = 4,
1> 5-4 Space will printed
// i = 1, ROW 1, largestElement = 5, j = 2 arr[j] = 1,
1> 5-1 Space will printed
// i = 1, ROW 1, largestElement = 5, j = 3 arr[j] = 2,
1> 5-2 Space will printed
// i = 1, ROW 1, largestElement = 5, j = 4 arr[j] = 5,
1 > 5-5 Space will be printed
for(int j=0; j<n; j=j+1) // what is printed in every
row
{
if(i > largestElement-arr[j])
{
System.out.print("*\t");
}
else
{
System.out.print("\t");
}
}
System.out.println();
}
}
}
26th Feb (Array Problem 6)
import java.util.*;
public class Main {
public static void main(String[] args) throws Throwable {
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 ans=ArrayProblem6(n,arr);
System.out.println(ans);
}
public static int ArrayProblem6(int n, int[] arr){
// Write code here
int minimumDifference = n+1;
int index = -1; // last time whether even was present
for(int i=0; i<n; i=i+1)
{
if(arr[i]>0 && arr[i]%2 == 0) // positive even numbers
{
// whether there was an even already present
if(index != -1)
{
int currentDifference = i - index;
if(currentDifference < minimumDifference)
{
minimumDifference = currentDifference;
}
index = i; // this as the index of last even
number we have
}
else
{
index = i; // last index where even number is
present is i
}
}
}
if(minimumDifference == (n+1))
{
return -1;
}
return minimumDifference;
}
}
27th Feb (Sum of upper and lower triangle)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = sc.nextInt();
triangleSums(n, matrix);
sc.close();
}
public static void triangleSums(int n, int[][] matrix) {
int sumOfUpperTriangle = 0;
int sumOfLowerTriangle = 0;
// upper
for(int i=0; i<n; i=i+1)
{
for(int j=0; j<n; j=j+1)
{
if(i<=j) // upper triangle (including diagonal)
{
sumOfUpperTriangle = sumOfUpperTriangle +
matrix[i][j];
}
if(i>=j) // lower triangle (including diagonal)
{
sumOfLowerTriangle = sumOfLowerTriangle +
matrix[i][j];
}
}
}
System.out.println(sumOfUpperTriangle + " " +
sumOfLowerTriangle);
}
}
27th Feb (Alternate Matrix Sum)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int [][]mat=new int[n][n];
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
mat[i][j]=sc.nextInt();
}
}
alternateMatrixSum(mat,n);
System.out.println('\n');
}
public void alternateMatrixSum(int [][]mat, int n) {
// black is present on (0,0)
// (0, 0) --> (0,2) --> (0,4) --> (0, 6)
// (1,1) --> (1, 3) --> (2,2)
// white
// (0,1) --> (0, 3) --> (0,5)
// (0, 1) --> (1, 0) --> (3, 0) --> (1, 2)
}
}
27th Feb (Matrix Multiplication)
import java.util.*;
class Solution {
public void printMultiplication(int[][] matrix1,int[][]
matrix2,int n)
{
int result[][] = new int[n][n];
// rows of first matrix are multiplied with columns of
second matrix
for(int i=0; i<n; i=i+1)
{
for(int j=0; j<n; j=j+1)
{
// First element of final matrix - 1*4 + 2*5 + 3*6
// first matrix (we are moving left to right)
(columns are chaning, row is fixed )
// second matrix (we are moving top to down) (rows
are chaning)
int sum = 0;
// for making the movement in first and second
matrix at the same speed, we will use the loop
// i = 0, j = 0 (first row * first column)
// k=0, matrix1[0][0] * matrix2[0][0]
// k=1, matrix1[0][1] * matrix2[1][0]
// k=2, matrix1[0][2] * matrix2[2][0]
// i = 0, j = 1 (first row * second column)
// k=0, matrix1[0][0] * matrix2[0][1]
// k=1, matrix1[0][1] * matrix2[1][1]
// k=2, matrix1[0][2] * matrix2[2][1]
// i = 0, j = 2 (first row * third column)
// k=0, matrix1[0][0] * matrix2[0][2]
// k=1, matrix1[0][1] * matrix2[1][2]
// k=2, matrix1[0][2] * matrix2[2][2]
// i = 1, j = 0 (second row * first column)
// k=0, matrix1[1][0] * matrix2[0][0]
// k=1, matrix1[1][1] * matrix2[1][0]
// k=2, matrix1[1][2] * matrix2[2][0]
for(int k=0; k<n; k=k+1)
{
sum = sum + matrix1[i][k]*matrix2[k][j];
}
result[i][j] = sum;
}
}
for(int i=0;i<n;i=i+1)
{
for(int j=0; j<n; j=j+1)
{
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n;
n = sc.nextInt();
int[][] matrix1 = new int[n][n];
int[][] matrix2 = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix1[i][j] = sc.nextInt();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix2[i][j] = sc.nextInt();
Solution Obj = new Solution();
Obj.printMultiplication(matrix1,matrix2,n);
}
sc.close();
}
}
27th Feb (Boundary Traversal)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m, n;
m = sc.nextInt();
n = sc.nextInt();
int[][] matrix = new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = sc.nextInt();
boundaryTraversal(m, n, matrix);
sc.close();
}
public static void boundaryTraversal(int m, int n, int[][]
matrix) {
// your code here
// m --> ROWS (Given)
// n --> COLUMNS (Given)
// 1st Row (columns changing, row = 0)
boolean didLoopRun = false;
for(int j=0; j<n; j=j+1)
{
didLoopRun = true;
System.out.print(matrix[0][j] + " ");
}
if(didLoopRun == false)
{
// IF first loop did not ran then don't run any
further loop
return; // (IT will not return anything but it
will stop the function)
}
didLoopRun = false;
// Last Column (column = n-1, rows are chaning)
for(int i=1; i<m; i=i+1)
{
didLoopRun = true;
System.out.print(matrix[i][n-1] + " ");
}
if(didLoopRun == false)
{
// IF second loop did not ran then don't run any
further loop
return; // (IT will not return anything but it
will stop the function)
}
// Last Row(columns are chaning, row = m-1)
// Last Row
didLoopRun = false;
for(int j=(n-2); j>=0; j=j-1)
{
didLoopRun = true;
System.out.print(matrix[m-1][j] + " ");
}
if(didLoopRun == false)
{
// IF third loop did not ran then don't run any
further loop
return; // (IT will not return anything but it
will stop the function)
}
// First column (rows will be chaning, column = 0)
for(int i=(m-2); i>=1; i=i-1)
{
System.out.print(matrix[i][0] + " ");
}
}
}
27th Feb (Rotate Matrix by 90)
import java.io.*;
import java.util.*;
class Solution {
public void rotateBy90(int [][]mat) {
int n = mat.length;
// STEP 1 (Transpose of the matrix)
// Square matrix means both rows and columns are equal
for(int i=0; i<n; i=i+1)
{
for(int j=0; j<n; j=j+1)
{
if(i>j)
{
// lower elements
// swap (i,j) with (j,i)
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
}
// STEP 2 (Printing the Reverse rows)
for(int i=0; i<n; i=i+1)
{
for(int j=(n-1); j>=0; j=j-1)
{
System.out.print(mat[i][j]+" ");
}
System.out.println();
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, m;
n = sc.nextInt();
m = sc.nextInt();
int [][]mat=new int[n][m];
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
mat[i][j]=sc.nextInt();
}
}
Solution Obj = new Solution();
Obj.rotateBy90(mat);
System.out.println('\n');
}
}
27th Feb (Rotate Matrix by 90 - 2)
import java.io.*;
import java.util.*;
class Solution {
public static void reverseArray(int arr[])
{
int n = arr.length;
int i = 0; // first arrow
int j = n - 1; // second arrow
while(i<j)
{
// interchange the elements
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
// first arrow will go towards right (it will move
forward)
i = i + 1;
// second arrow will go towards left (it will go
backwards)
j = j - 1;
}
}
public void rotateBy90(int [][]mat) {
int n = mat.length;
// STEP 1 (Transpose of the matrix)
// Square matrix means both rows and columns are equal
for(int i=0; i<n; i=i+1)
{
for(int j=0; j<n; j=j+1)
{
if(i>j)
{
// lower elements
// swap (i,j) with (j,i)
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
}
// STEP 2 (Reverse the rows)
for(int i=0; i<n; i=i+1)
{
// STEP 1) i = 0, mat[0] --> 1st row
// STEP 2) i = 1, mat[1] --> 2nd row
reverseArray(mat[i]);
}
for(int i=0; i<n; i=i+1)
{
for(int j=0; j<n; j=j+1)
{
System.out.print(mat[i][j]+" ");
}
System.out.println();
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, m;
n = sc.nextInt();
m = sc.nextInt();
int [][]mat=new int[n][m];
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
mat[i][j]=sc.nextInt();
}
}
Solution Obj = new Solution();
Obj.rotateBy90(mat);
System.out.println('\n');
}
}
28th Feb (Mock interview and Revision Discussion)
- BREAK Down the Question
- 5 mins to read the questions (You understand each and every
line)
- LOGIC
- REVISION
(Go to the concepts. revise them from doc and recording
for normal questions try to think logic only while revising
for tough questions try to implement/code them)
For mock interview
- Find a friend in Acciojob or nearby (ask him to take interview
and
take his interview as well
- Teach any questions as if you are taking a class)
IF you clear placement module and placement mock interview
- You will sit for companies
28th Feb (Memory Management)
import java.util.Scanner;
// Memory is divided into two parts (Stack and Heap memory)
// All the variables are stored in STACK memory
// How is array stored in the memory
// Array element are stored in HEAP memory
// Reference Variable (Module 3) is stored in STACK memory
// Whenever you write new keyword (HEAP memory will be involved)
public class Main
{
public static void main (String[] args)
{
int a = 5;
int b = a;
b = b+1;
//System.out.println(a);
//System.out.println(b);
int arr[] = new int[5];
arr[0] = 51;
int temp[] = new int[5];
temp[0] = 12;
int brr[] = arr; // brr will have same values and size as
arr
System.out.println(arr[0]);
System.out.println(brr[0]);
brr[0] = 70;
System.out.println(arr[0]);
System.out.println(brr[0]);
System.out.println(temp[0]);
}
}
28th Feb (Do while Loop - 1)
import java.util.Scanner;
public class Main
{
public static void main (String[] args)
{
// for(initialization; testcondition; updateExpression)
//{
// statement
//}
// initialization statement
//do
//{
// update expression
//}while(testcondition);
// STEP 1) i=0, 0 is printed, i will be updated to 1, 1<5
// STEP 2) i=1, 1 is printed, i will be updated to 2, 2<5
// STEP 3) i=2, 2 is printed, i will be updated to 3, 3<5
// STEP 4) i=3, 3 is printed, i will be updated to 4, 4<5
// STEP 5) i=4, 4 is printed, i will be updated to 5, 5<5
(FALSE, LOOP will stop)
int i = 0;
do
{
System.out.println(i);
i=i+1;
}while(i<5);
// IT will run one time and j value will be printed
int j = 0;
do
{
System.out.println(j);
j=j+1;
}while(j<0);
// WHILE LOOP
// Questions that involve digits
// Questions that involve Two pointer technique
// For loops
// One place to another (range)
// Do some task some number of times
}
}
28th Feb (Do while Loop - 2)
import java.util.Scanner;
public class Main
{
public static void main (String[] args)
{
// for(initialization; testcondition; updateExpression)
//{
// statement
//}
// initialization statement
//do
//{
// update expression
//}while(testcondition);
// STEP 1) i=0, 0 is printed, i will be updated to 1, 1<5
// STEP 2) i=1, 1 is printed, i will be updated to 2, 2<5
// STEP 3) i=2, 2 is printed, i will be updated to 3, 3<5
// STEP 4) i=3, 3 is printed, i will be updated to 4, 4<5
// STEP 5) i=4, 4 is printed, i will be updated to 5, 5<5
(FALSE, LOOP will stop)
int i = 0;
do
{
System.out.println(i);
i=i+1;
}while(i<5);
// IT will run one time and j value will be printed
int j = 0;
do
{
System.out.println(j);
j=j+1;
}while(j<0);
// WHILE LOOP
// Questions that involve digits
// Questions that involve Two pointer technique
// For loops
// One place to another (range)
// Do some task some number of times
}
}
28th Feb (Diagonally traverse a matrix)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
a[i][j] = sc.nextInt();
}
// first set of starting points
for(int i=n-1; i>=0; i=i-1)
{
// (STARTING point) row - 0, column = i
// FROM every starting point we need to move anti
diagonally
int row = 0;
int column = i;
// every element inside the matrix will have
index<n
while(row<n && column<n) // ensuring that we stop
as soon we go out the matrix
{
System.out.print(a[row][column] + " ");
row = row + 1;
column = column + 1;
}
}
// for the remaining starting points
for(int i=1; i<n; i=i+1)
{
// (STARTING point) row - 0, column = i
// FROM every starting point we need to move anti
diagonally
int row = i;
int column = 0;
// every element inside the matrix will have
index<n
while(row<n && column<n) // ensuring that we stop
as soon we go out the matrix
{
System.out.print(a[row][column] + " ");
row = row + 1;
column = column + 1;
}
}
}
}
28th Feb (Spiral matrix)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, m;
n = sc.nextInt();
m = sc.nextInt();
int [][]mat=new int[n][m];
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
mat[i][j]=sc.nextInt();
}
}
spirallyTraverse(mat);
System.out.println('\n');
}
public static void spirallyTraverse(int [][]mat) {
int n = mat.length; // number of rows
int m = mat[0].length; // number of columns
int top = 0; // top most row
int bottom = n-1; // bottom most row
int left = 0; // left most column
int right = m-1; // right most column
while(top<=bottom && left<=right)
{
boolean didFirstLoopRan = false;
boolean didSecondLoopRan = false;
boolean didThirdLoopRan = false;
// - Print left to right in top row
for(int i=left; i<=right; i=i+1) // First Arrow
{
didFirstLoopRan = true;
System.out.print(mat[top][i] + " ");
}
if(didFirstLoopRan == true)
{
// - Print top to bottom in right column
for(int i=top+1; i<=bottom; i=i+1) // Second arrow
{
didSecondLoopRan = true;
System.out.print(mat[i][right] + " ");
}
}
if(didSecondLoopRan == true)
{
// - Print right to left in bottom row
for(int i=right-1; i>=left; i=i-1) // Third Arrow
{
didThirdLoopRan = true;
System.out.print(mat[bottom][i] + " ");
}
}
if(didThirdLoopRan == true)
{
// - Print bottom to top in left column
for(int i=(bottom-1); i>top; i=i-1) // Fourth
Arrow
{
System.out.print(mat[i][left] + " ");
}
}
top = top + 1;
bottom = bottom - 1;
left = left + 1;
right = right - 1;
}
}
}
28th Feb (Find the Way Assignment)
import java.util.*;
public class Main {
public static int[] findTheWay(int[][] matrix) {
// final answer is the last point after which rat will get
out the matrix
// final answer is a array, first element is storing row
number,
// second element is storing column number
int ans[] = new int[2];
int n = matrix.length;
int m = matrix[0].length;
int currentRow = 0;
int currentCol = 0;
// MAPPING CONCEPT (OUT OF COURSE for MODULE 1, NOT COME
IN MODULE 1)
// east (left to right) --> 0
// south (top to bottom) --> 1
// west (right to left) --> 2
// north (bottom to top) --> 3
int direction = 0;
while(currentRow<n && currentCol<m && currentCol>=0 &&
currentRow>=0)
{
if(matrix[currentRow][currentCol] == 1)
{
matrix[currentRow][currentCol] = 0;
// change the direction
direction = direction + 1;
if(direction == 4)
{
direction = 0;
}
}
ans[0] = currentRow;
ans[1] = currentCol;
// AFTER THIS MOUSE CAN LEAVE OR EXIT THE MATRIX
// before this step only we will store what is the
position of the mouse
// we need to move in that direction
if(direction == 0)
{
currentCol = currentCol + 1;
}
else if(direction == 1)
{
currentRow = currentRow + 1;
}
else if(direction == 2)
{
currentCol = currentCol - 1;
}
else
{
currentRow = currentRow - 1;
}
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m, n;
m = sc.nextInt();
n = sc.nextInt();
int[][] matrix = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
matrix[i][j] = sc.nextInt();
}
int result[] = findTheWay(matrix);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
System.out.println();
sc.close();
}
}
28th Feb (Pattern Question Doubt discussion)
import java.util.Scanner;
public class Main
{
public static void main(String arg[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i=1; i<=n; i=i+1)
{
System.out.print("1");
// Row 1 --> 1, i = 1, j<=-1
// Row 2 --> 11, i = 2, j<=0
// Row 3 --> 121, i = 3, j<=1
// Row 4 --> 1221, i = 4, j<=2
// Row 5 --> 12221, i = 5, j<=3
for(int j=1; j<=(i-2); j=j+1)
{
System.out.print("2");
}
if(i!=1)
{
System.out.print("1");
}
System.out.println();
}
}
}
// for loop rows (even and odd)
// {
// for loop for spaces in left
// star printed
// for loop for spaces in middle
// star printed
// spacesMiddle = spacesMiddle - 1;
// spaceLeft = spaceLeft + 1;
//
// }