├── .vscode └── settings.json ├── 01. Wecome to Alpha ├── Telegram Channel.pdf └── website.pdf ├── 03. Prerequisites - Installation Resources └── Installation Resources.pdf ├── 06. Variables & DataTypes ├── Variables Questions (1).pdf ├── Variables Solutions.pdf ├── p10_Product_of_two_number.java ├── p11_Area_of_Circle.java ├── p12_Type_Conversion.java ├── p13_Type_Casting.java ├── p14_Type_promotion.java ├── p1_BoilerplateCode.java ├── p2_Output.java ├── p3_Print_a_Pattern.java ├── p4_Variables.java ├── p5_Data_Types.java ├── p6_sum_of_two_numbers.java ├── p7_Comments.java ├── p8_input_in_java.java └── p9_Sum_of_two_input_value.java ├── 07. Operators ├── Operator Precedence_V1.1.pdf ├── Operators Questions.pdf ├── Operators Solutions.pdf ├── p1_types_of_operators.java ├── p2_Arithmetic_Operators.java ├── p3_Relational_Operators.java ├── p4_Logical_Operators.java ├── p5_ASsignment_Operators.java ├── p6_StringConcatination.java └── p7_OperatorExample.java ├── 08. Conditional Statements ├── Conditional Questions.pdf ├── Conditional Solutions.pdf ├── p10_GradeCalculator.java ├── p1_if_else.java ├── p2_Print_the_largest_of_2_numbers.java ├── p3_Print_if_number_is_odd_or_Even.java ├── p4_using_else_if_Tax_calculator.java ├── p5_Print_the_largest_of_three_number.java ├── p6_ternary_operator.java ├── p7_check_if_a_student_will_pass_or_fail.java ├── p8_switch_statement.java └── p9_using_switch_Calculator.java ├── 09. Loops(Flow Control) ├── Loops Questions.pdf ├── Loops Solutions.pdf ├── p10_input_multiple_of_10.java ├── p11_Continue_Statement.java ├── p12_print_number_except_multiples_of_10.java ├── p13_Number_is_prime_or_not.java ├── p14_Optmize_way_to_prime.java ├── p1_while_Loop.java ├── p2_print_numbers_to_n_numbers.java ├── p3_sum_of_first_n_natural_number.java ├── p4_for_Loop.java ├── p5_print_square.java ├── p6_print_reverse_of_a_number.java ├── p7_reverse_the_given_nubmer.java ├── p8_do_while_loop.java └── p9_break_statement.java ├── 10. Patterns(Part-1) ├── 2-Advanced Patterns Note.pdf ├── p1_intro_to_Nested_Loops_star_Pattern.java ├── p2_Inverted_Strar_Patter.java ├── p3_Half_Pyramid_Pattern.java └── p4_Character_pattern.java ├── 11. Functions & Methods ├── Functions Questions.pdf ├── Functions Solutions.pdf ├── p10_a_number_is_prime_or_not.java ├── p11_a_number_is_prime_or_not_Optimized.java ├── p12_Primes_in_Range.java ├── p13_Binary_to_Decimal.java ├── p14_Decimal_to_Binary.java ├── p1_Syntax_of_Void_function_in_java.java ├── p2_Syntax_of_int_function_in_java.java ├── p3_Parameters_and_Arguments.java ├── p4_Call_by_value_and_Reference.java ├── p5_product_of_a_and_b.java ├── p6_Factorial_in_java.java ├── p7_Binomial_Coefficient.java ├── p8_overloading_using_parameters.java └── p9_overloading_using_Data_Types.java ├── 12. Patterns(Part-2)- Advanced ├── p10_Number_Pyramid.java ├── p11_Palindromic_Pattern_with_Numbers.java ├── p1_Hollow_Rectangle_Pattern.java ├── p2_Inverted_and_Rotated_Half_pyramid.java ├── p3_Inverted_Half_Pyramid_Pattern_with_Numbers.java ├── p4_Floyds_Triangle_Pattern.java ├── p5_0_1_Triangle_Pattern.java ├── p6_Butterfly_Pattern.java ├── p7_Solid_Rhombus_Pattern.java ├── p8_Hollow_Rhombus_Pattern.java └── p9_Diamod_Pattern.java ├── 13. ARRAYS ├── Arrays - Assignment_Type.pdf ├── Arrays - Solution (1).pdf ├── Space & Time Complexity (1) (1).pdf ├── p10_Print_Subarrays.java ├── p11_Sum_of_Subarray.java ├── p12_Max_Subarray_Sum_1_Brute_Force.java ├── p13_Max_Subarray_Sum_2_Prefix_Sum.java ├── p14_Max_Subarray_Sum_3_Kadane_s_Algorithm.java ├── p15_Trapping_Rainwater_Auxiliary_Array_IMP.java ├── p16_Best_Time_to_Buy_And_Sell_Stock.java ├── p17_Array_appears_at_least_twice.java ├── p18_Array_appears_at_least_twice_Approch2.java ├── p19_Sorted_Rotated_Array_using_Binary_Search.java ├── p1_Arrays_creation.java ├── p2_Array_input_output_update.java ├── p3_Arrays_as_Function_Arguments.java ├── p4_Linear_search.java ├── p5_Largest_in_Array.java ├── p6_Smallest_in_Array.java ├── p7_Binary_Search.java ├── p8_Reverse_an_Array.java └── p9_Pairs_in_Array.java ├── 14. Basic Sorting Algorithms ├── Basic Sorting Questions (1).pdf ├── Basic Sorting Solutions.pdf ├── p1_Bubble_Sort_Code.java ├── p2_Selection_Sort_Code.java ├── p3_Insertion_Sort_code.java ├── p4_Inbuilt_Sort_code.java ├── p5_Counting_Sort_Code.java ├── p6_Descending_Bubble_Sort_Code.java ├── p7_Descending_Selection_Sort_code.java ├── p8_Descending_Insertion_Sort_Code.java └── p9_Descending_Counting_Sort_Code.java ├── 15. 2-D Arrays ├── 2D Arrays Questions.pdf ├── 2D Arrays Solutions.pdf ├── p10_Transpose_of_Matrix.java ├── p11_CountNegativeNumbersInSorted2DArray.java ├── p12_MaxElementIn2DArray.java ├── p13_2DArray.java ├── p1_2D_Array_Declearation_Code.java ├── p2_Search_key_in_2D_Array_Code.java ├── p3_Spiral_Matrix_Code.java ├── p4_Diagonal_Sum_Brute_force.java ├── p5_Diagonal_Sum_Best_force.java ├── p6_Search_in_Sorted_Matrix_Brute_force.java ├── p7_Search_in_Sorted_Matrix_Best_force.java ├── p8_num_how_many_time_present_in_array.java └── p9_Sum_of_nth_row_in_2DArray.java ├── 16. Strings ├── StringQuestion.pdf ├── StringSolution.pdf ├── p10_largest_String_using_lexicographic.java ├── p11_StringBuilder_mutable_String.java ├── p12_first_letter_uppercase.java ├── p13_String_Compression.java ├── p14_Count_lowercased_vowels.java ├── p15_String_Anagrams.java ├── p16_firstNonRepeatedChar.java ├── p1_Introduction_String.java ├── p2_String_Input_Output.java ├── p3_String_length_Method.java ├── p4_String_Concatenation.java ├── p5_String_charAt.java ├── p6_Palindrome_String.java ├── p7_Shortest_path_to_reach_destination.java ├── p8_String_Compare_Methods.java └── p9_Substring_method.java ├── 17. Bit Manipulation ├── Question.pdf ├── Solution.pdf ├── p10_Set_Ith_Bit.java ├── p11_Clear_Ith_Bit.java ├── p12_Update_Ith_Bit.java ├── p13_Clear_Last_I_Bits.java ├── p14_Clear_Range_Of_Bits.java ├── p15_Check_If_A_Number_is_Power.java ├── p16_Count_Set_Bits_In_Number.java ├── p17_Fast_Exponentiation_bitManipulation.java ├── p18_SwapBits.java ├── p19_ReverseBits.java ├── p1_Introduction_to_Bitwise_Operator.java ├── p2_Binary_AND.java ├── p3_Binary_OR.java ├── p4_Binary_XOR.java ├── p5_Binary_Ones_Complement.java ├── p6_Binary_Left_Shift.java ├── p7_Binary_Right_Shift.java ├── p8_Odd_or_Even_using_Bitwise.java └── p9_Get_ith_Bit.java └── 18. Oops ├── p1_What_is_Class.java ├── p2_Access_Modifiers.java ├── p3_Getters_setters.java ├── p4_Encapsulation.java ├── p5_Constructors.java ├── p6_Type_of_Constructors.java ├── p7_Copy_Constructor.java └── p8_Destructors.java /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.project.sourcePaths": [ 3 | "6. Variables & DataTypes", 4 | "7. Operators", 5 | "8. Conditional Statements", 6 | "9. Loops(Flow Control)", 7 | "10. Patterns(Part-1)", 8 | "11. Functions & Methods", 9 | "." 10 | ], 11 | "python.formatting.provider": "autopep8", 12 | "java.debug.settings.onBuildFailureProceed": true 13 | } -------------------------------------------------------------------------------- /01. Wecome to Alpha/Telegram Channel.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/01. Wecome to Alpha/Telegram Channel.pdf -------------------------------------------------------------------------------- /01. Wecome to Alpha/website.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/01. Wecome to Alpha/website.pdf -------------------------------------------------------------------------------- /03. Prerequisites - Installation Resources/Installation Resources.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/03. Prerequisites - Installation Resources/Installation Resources.pdf -------------------------------------------------------------------------------- /06. Variables & DataTypes/Variables Questions (1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/06. Variables & DataTypes/Variables Questions (1).pdf -------------------------------------------------------------------------------- /06. Variables & DataTypes/Variables Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/06. Variables & DataTypes/Variables Solutions.pdf -------------------------------------------------------------------------------- /06. Variables & DataTypes/p10_Product_of_two_number.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public class p10_Product_of_two_number { 6 | public static void main(String args[]){ 7 | 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int number1 = sc.nextInt(); 11 | int number2 = sc.nextInt(); 12 | 13 | int product = number1*number2; 14 | 15 | System.out.println(product); 16 | 17 | 18 | sc.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /06. Variables & DataTypes/p11_Area_of_Circle.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public class p11_Area_of_Circle { 6 | public static void main(String args[]){ 7 | 8 | Scanner sc = new Scanner(System.in); 9 | 10 | float radius = sc.nextFloat(); 11 | 12 | float area = (float) (3.14 * radius * radius) ; 13 | 14 | System.out.println(area); 15 | 16 | 17 | sc.close(); 18 | } 19 | } -------------------------------------------------------------------------------- /06. Variables & DataTypes/p12_Type_Conversion.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public class p12_Type_Conversion { 6 | public static void main(String args[]){ 7 | 8 | /* 9 | * Conversion happens when: 10 | * a. type compatible 11 | * b. detination type > source type. 12 | * byte -> short -> int -> float -> long -> double 13 | *and also type conversion is low to up .. 14 | */ 15 | 16 | Scanner sc = new Scanner(System.in); 17 | 18 | float num = sc.nextInt(); // this is type conversion / widening / implicit 19 | 20 | System.out.println(num); 21 | 22 | sc.close(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /06. Variables & DataTypes/p13_Type_Casting.java: -------------------------------------------------------------------------------- 1 | public class p13_Type_Casting { 2 | 3 | 4 | // This Code is Written By Priyanshu Gour 5 | 6 | public static void main(String args[]){ 7 | 8 | /* 9 | * in type costing mean the conversion is forcefully ... 10 | * large to low & low to large all is allowed in type casting ... 11 | * such different name of type casting / narrowing / explicit... 12 | */ 13 | 14 | float num = (float) 12.424 ; 15 | 16 | int num1 = (int) num ; 17 | 18 | System.out.println(num1); 19 | 20 | 21 | char a = 'P'; 22 | int chartype = (int) a ; 23 | 24 | System.out.println(chartype); 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /06. Variables & DataTypes/p14_Type_promotion.java: -------------------------------------------------------------------------------- 1 | public class p14_Type_promotion { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | 6 | public static void main(String args[]){ 7 | 8 | /* 9 | * 1. Javaa automatically promotes each byte, short, or char operand to int when evaluting an expression. 10 | * 2. If one operand is long, float or double the whole expression is promoted to long, float, or double respectively... 11 | */ 12 | 13 | char a = 'A'; 14 | char b = 'B'; 15 | 16 | System.out.println(a+b); 17 | 18 | int w = 10 ; 19 | float x = (float)20.25; 20 | double y = 25 ; 21 | long z = 23 ; 22 | 23 | //int ans = w + x + y + z ; // not promotion because int is lower then uses datatypes......... 24 | 25 | int ans = (int) (w + x + y + z) ; 26 | 27 | System.out.println(ans); 28 | 29 | 30 | 31 | /* 32 | 33 | 34 | //wrong 35 | byte h = 5 ; 36 | h = h*2; 37 | 38 | 39 | // right 40 | byte i = 5 ; 41 | i = (byte) (b*2); 42 | 43 | 44 | */ 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /06. Variables & DataTypes/p1_BoilerplateCode.java: -------------------------------------------------------------------------------- 1 | public class p1_BoilerplateCode { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | 8 | // Write Java Code... 9 | 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /06. Variables & DataTypes/p2_Output.java: -------------------------------------------------------------------------------- 1 | public class p2_Output { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | 8 | System.out.println("Hello World...\nI am Priyanshu Gour....!"); 9 | 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /06. Variables & DataTypes/p3_Print_a_Pattern.java: -------------------------------------------------------------------------------- 1 | public class p3_Print_a_Pattern { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | 8 | System.out.println("****"); 9 | System.out.println("***"); 10 | System.out.println("**"); 11 | System.out.println("*"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /06. Variables & DataTypes/p4_Variables.java: -------------------------------------------------------------------------------- 1 | 2 | // This Code is Written By Priyanshu Gour 3 | 4 | public class p4_Variables { 5 | public static void main(String args[]){ 6 | 7 | int a = 10 ; // a is variable .. 8 | int b = 15 ; // b is also variable .. 9 | String name = "Priyanshu Gour"; 10 | 11 | int c = a + b ; 12 | 13 | System.out.println(c); 14 | System.out.println(name); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /06. Variables & DataTypes/p5_Data_Types.java: -------------------------------------------------------------------------------- 1 | public class p5_Data_Types { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | byte b = 8 ; // -128 to 127 8 | System.out.println(b); 9 | 10 | char ch = 'P'; // 2byte 11 | System.out.println(ch); 12 | 13 | boolean var = false ; //1byte 14 | System.out.println(var); 15 | 16 | int num = 923384 ; // 4bytes 17 | System.out.println(num); 18 | 19 | long num1 = 905239489; //8bytes 20 | System.out.println(num1); 21 | 22 | double num2 = 90384958.523452 ; //8bytes 23 | System.out.println(num2); 24 | 25 | float num3 = 345 ; //4bytes 26 | System.out.println(num3); 27 | 28 | short num4 = 234 ; // 2 bytes 29 | System.out.println(num4); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /06. Variables & DataTypes/p6_sum_of_two_numbers.java: -------------------------------------------------------------------------------- 1 | public class p6_sum_of_two_numbers { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | int number1 = 10 ; 8 | int number2 = 20 ; 9 | 10 | int sum_of_num1_and_num2 = number1 + number2 ; 11 | 12 | System.out.println("Sum of number1 and number2 is : "+sum_of_num1_and_num2); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /06. Variables & DataTypes/p7_Comments.java: -------------------------------------------------------------------------------- 1 | public class p7_Comments { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | 8 | // this is comment does not execute in code . and compiler ignore it 9 | 10 | // this is single line comment ..... 11 | 12 | 13 | /* 14 | hy buddy 15 | i am priyanshu Gour 16 | 17 | And this is also comment but this comment is multi line comment ... 18 | 19 | */ 20 | 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /06. Variables & DataTypes/p8_input_in_java.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | 4 | // This Code is Written By Priyanshu Gour 5 | 6 | public class p8_input_in_java { 7 | public static void main(String args[]){ 8 | Scanner sc = new Scanner(System.in); 9 | // String input = sc.next(); 10 | // String input = sc.nextLine(); 11 | // int input = sc.nextInt(); 12 | float input = sc.nextFloat(); 13 | System.out.println(input); 14 | 15 | 16 | 17 | /* 18 | Type of Input in JAVA 19 | 20 | next() 21 | nextLine() 22 | nextInt() 23 | nextByte() 24 | nextFloat() 25 | nextDouble() 26 | nextBoolean() 27 | nextShort() 28 | nextlong() 29 | */ 30 | 31 | 32 | sc.close(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /06. Variables & DataTypes/p9_Sum_of_two_input_value.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public class p9_Sum_of_two_input_value { 6 | public static void main(String args[]){ 7 | 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int number1 = sc.nextInt(); 11 | int number2 = sc.nextInt(); 12 | 13 | int sum = number1 + number2 ; 14 | 15 | System.out.println("Sum of Numberi and number2 is : "+ sum); 16 | 17 | 18 | sc.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /07. Operators/Operator Precedence_V1.1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/07. Operators/Operator Precedence_V1.1.pdf -------------------------------------------------------------------------------- /07. Operators/Operators Questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/07. Operators/Operators Questions.pdf -------------------------------------------------------------------------------- /07. Operators/Operators Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/07. Operators/Operators Solutions.pdf -------------------------------------------------------------------------------- /07. Operators/p1_types_of_operators.java: -------------------------------------------------------------------------------- 1 | public class p1_types_of_operators { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | /* 8 | * Arithmetic Operators (Binary / Unary / Ternary) 9 | * Relational Operators 10 | * Logical Operators 11 | * Bitwise Operators(Bit Manipulation..) 12 | * Assignment Operators 13 | */ 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /07. Operators/p2_Arithmetic_Operators.java: -------------------------------------------------------------------------------- 1 | public class p2_Arithmetic_Operators { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | 8 | // ARithmetic Operators... 9 | 10 | int a = 10 ; 11 | int b = 5 ; 12 | 13 | // Binary Arithmetic Operators 14 | int sum = a + b ; // sum Arithmetic Operators. 15 | int sub = a - b ; // sub Arithmetic Operators. 16 | int mul = a * b ; // mul Arithmetic Operators. 17 | int div = a / b ; // divided Arithmetic Operators. 18 | int mod = a % b ; // modulo This is showes the Remiender. 19 | 20 | System.out.println(sum); 21 | System.out.println(sub); 22 | System.out.println(mul); 23 | System.out.println(div); 24 | System.out.println(mod); 25 | 26 | //Unary Arithmetic Operators.. 27 | 28 | int incpost = a++ ; // post increment 29 | int incpre = ++a ; // pre icrement 30 | 31 | int decpost = a-- ; // post decrement 32 | int decpre = --a ; // pre decrement 33 | 34 | System.out.println(incpost); 35 | System.out.println(incpre); 36 | 37 | System.out.println(decpost); 38 | System.out.println(decpre); 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /07. Operators/p3_Relational_Operators.java: -------------------------------------------------------------------------------- 1 | public class p3_Relational_Operators { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | // Relational Operators :- it shows the output in the form of boolen . 8 | 9 | int a = 10 ; 10 | int b = 20 ; 11 | 12 | System.out.println(a==b); // == check value is equal or not.. 13 | System.out.println(a!=b); // != check value is not equal .. 14 | System.out.println(a>b); // > a is greater or not .. 15 | System.out.println(a=b); // a is greater equal or not .. 17 | System.out.println(a<=b); // b is greater equal or not .. 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /07. Operators/p4_Logical_Operators.java: -------------------------------------------------------------------------------- 1 | public class p4_Logical_Operators { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | 8 | // Logical Operators... 9 | 10 | int a = 10 ; 11 | int b = 20 ; 12 | int c = 30 ; 13 | 14 | System.out.println(b > a && c > b); 15 | System.out.println(b > a || a > b); 16 | System.out.println(!(b > a && c > b)); 17 | 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /07. Operators/p5_ASsignment_Operators.java: -------------------------------------------------------------------------------- 1 | public class p5_ASsignment_Operators { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | 8 | // Assignment Operators.. 9 | 10 | int a = 10 ; 11 | int b = 5 ; 12 | 13 | b = a ; 14 | System.out.println(b); 15 | 16 | b += a ; 17 | System.out.println(b); 18 | 19 | b -= a ; 20 | System.out.println(b); 21 | 22 | b *= a ; 23 | System.out.println(b); 24 | 25 | b /= a ; 26 | System.out.println(b); 27 | 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /07. Operators/p6_StringConcatination.java: -------------------------------------------------------------------------------- 1 | public class p6_StringConcatination { 2 | public static void main(String[] args) { 3 | String a = "sachin"; 4 | int b = 10, c = 20, d = 30; 5 | System.out.println(a + b + c + d);// sachin102030 6 | System.out.println(b + c + d + a);// 60sachin 7 | System.out.println(b + c + a + d);// 30sachin30 8 | System.out.println(b + a + c + d);// 10sachin2030 9 | } 10 | } -------------------------------------------------------------------------------- /07. Operators/p7_OperatorExample.java: -------------------------------------------------------------------------------- 1 | public class p7_OperatorExample { 2 | public static void main(String[] args) { 3 | // Arithmetic Operators 4 | int a = 10; 5 | int b = 3; 6 | 7 | int addition = a + b; 8 | int subtraction = a - b; 9 | int multiplication = a * b; 10 | int division = a / b; 11 | int modulus = a % b; 12 | 13 | // Logical Operators 14 | boolean x = true; 15 | boolean y = false; 16 | 17 | boolean andResult = x && y; 18 | boolean orResult = x || y; 19 | boolean notX = !x; 20 | 21 | // Relational Operators 22 | int p = 5; 23 | int q = 7; 24 | 25 | boolean isEqual = p == q; 26 | boolean isNotEqual = p != q; 27 | boolean isGreater = p > q; 28 | boolean isLess = p < q; 29 | boolean isGreaterOrEqual = p >= q; 30 | boolean isLessOrEqual = p <= q; 31 | 32 | // Bitwise Operators 33 | int m = 5; 34 | int n = 3; 35 | 36 | int bitwiseAnd = m & n; 37 | int bitwiseOr = m | n; 38 | int bitwiseXor = m ^ n; 39 | int bitwiseComplementM = ~m; 40 | int leftShift = m << 2; 41 | int rightShift = m >> 1; 42 | 43 | // Assignment Operators 44 | int xValue = 10; 45 | int yValue = 5; 46 | 47 | xValue += yValue; 48 | xValue -= yValue; 49 | xValue *= yValue; 50 | xValue /= yValue; 51 | xValue %= yValue; 52 | 53 | // Conditional (Ternary) Operator 54 | int number = 15; 55 | String result = (number > 10) ? "Greater than 10" : "Less than or equal to 10"; 56 | 57 | // Printing the results 58 | System.out.println("Arithmetic Operators:"); 59 | System.out.println("Addition: " + addition); 60 | System.out.println("Subtraction: " + subtraction); 61 | System.out.println("Multiplication: " + multiplication); 62 | System.out.println("Division: " + division); 63 | System.out.println("Modulus: " + modulus); 64 | 65 | System.out.println("\nLogical Operators:"); 66 | System.out.println("AND Result: " + andResult); 67 | System.out.println("OR Result: " + orResult); 68 | System.out.println("NOT X: " + notX); 69 | 70 | System.out.println("\nRelational Operators:"); 71 | System.out.println("Is Equal: " + isEqual); 72 | System.out.println("Is Not Equal: " + isNotEqual); 73 | System.out.println("Is Greater: " + isGreater); 74 | System.out.println("Is Less: " + isLess); 75 | System.out.println("Is Greater or Equal: " + isGreaterOrEqual); 76 | System.out.println("Is Less or Equal: " + isLessOrEqual); 77 | 78 | System.out.println("\nBitwise Operators:"); 79 | System.out.println("Bitwise AND: " + bitwiseAnd); 80 | System.out.println("Bitwise OR: " + bitwiseOr); 81 | System.out.println("Bitwise XOR: " + bitwiseXor); 82 | System.out.println("Bitwise Complement of M: " + bitwiseComplementM); 83 | System.out.println("Left Shift of M: " + leftShift); 84 | System.out.println("Right Shift of M: " + rightShift); 85 | 86 | System.out.println("\nAssignment Operators:"); 87 | System.out.println("xValue: " + xValue); 88 | 89 | System.out.println("\nConditional Operator:"); 90 | System.out.println("Result: " + result); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /08. Conditional Statements/Conditional Questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/08. Conditional Statements/Conditional Questions.pdf -------------------------------------------------------------------------------- /08. Conditional Statements/Conditional Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/08. Conditional Statements/Conditional Solutions.pdf -------------------------------------------------------------------------------- /08. Conditional Statements/p10_GradeCalculator.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class p10_GradeCalculator { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | 7 | System.out.print("Enter the total marks: "); 8 | int totalMarks = scanner.nextInt(); 9 | 10 | if (totalMarks < 0 || totalMarks > 100) { 11 | System.out.println("Invalid input! Total marks should be between 0 and 100."); 12 | } else { 13 | char finalGrade; 14 | 15 | if (totalMarks >= 90) { 16 | finalGrade = 'A'; 17 | } else if (totalMarks >= 80) { 18 | finalGrade = 'B'; 19 | } else if (totalMarks >= 70) { 20 | finalGrade = 'C'; 21 | } else if (totalMarks >= 50) { 22 | finalGrade = 'D'; 23 | } else { 24 | finalGrade = 'F'; 25 | } 26 | 27 | System.out.println("Final Grade: " + finalGrade); 28 | } 29 | 30 | scanner.close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /08. Conditional Statements/p1_if_else.java: -------------------------------------------------------------------------------- 1 | public class p1_if_else { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | int age = 23 ; 8 | 9 | if(age > 18){ 10 | System.out.println("You are adult ...and you can vote.."); 11 | }else{ 12 | System.out.println("Your cannot adult .. and your cannot vote..."); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /08. Conditional Statements/p2_Print_the_largest_of_2_numbers.java: -------------------------------------------------------------------------------- 1 | public class p2_Print_the_largest_of_2_numbers { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | int a = 10 ; 8 | int b = 20 ; 9 | 10 | if(a>b){ 11 | System.out.println("A is greater then B ..."); 12 | }else{ 13 | System.out.println("B is greater then A.."); 14 | } 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /08. Conditional Statements/p3_Print_if_number_is_odd_or_Even.java: -------------------------------------------------------------------------------- 1 | public class p3_Print_if_number_is_odd_or_Even { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | 8 | int a = 10 ; 9 | 10 | if(a % 2 == 0 ){ 11 | System.out.println(a+" : is Even Number.. "); 12 | }else{ 13 | System.out.println(a+" : is Odd Nubmer.."); 14 | } 15 | 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /08. Conditional Statements/p4_using_else_if_Tax_calculator.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public class p4_using_else_if_Tax_calculator { 6 | public static void main(String args[]){ 7 | 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int income = sc.nextInt(); 11 | 12 | if(income < 500000){ 13 | System.out.println("You will give 0% tax.. : 0"); 14 | }else if(income>500000 && 1000000 > income){ 15 | System.out.println("You will give 20% tax... : "+(income*0.2)); 16 | }else{ 17 | System.out.println("You will give 30% tax.. : "+(income*0.3)); 18 | } 19 | 20 | 21 | sc.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /08. Conditional Statements/p5_Print_the_largest_of_three_number.java: -------------------------------------------------------------------------------- 1 | public class p5_Print_the_largest_of_three_number { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | int a = 10 ; 8 | int b = 20 ; 9 | int c = 30 ; 10 | 11 | if(a > b && a > c){ 12 | System.out.println("A is greater then B & C"); 13 | }else if(b > a && b > c){ 14 | System.out.println("B is greater then A & C"); 15 | }else{ 16 | System.out.println("C is greater then A & B"); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /08. Conditional Statements/p6_ternary_operator.java: -------------------------------------------------------------------------------- 1 | public class p6_ternary_operator { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | // ternary 8 | // variable = condition? statement1:statement3; 9 | 10 | int num = 13 ; 11 | 12 | String check = (num%2==0)?"Even":"Odd"; 13 | 14 | System.out.println(check); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /08. Conditional Statements/p7_check_if_a_student_will_pass_or_fail.java: -------------------------------------------------------------------------------- 1 | public class p7_check_if_a_student_will_pass_or_fail { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | int marks = 23 ; 8 | 9 | String check = (marks >= 33 ) ? "Pass":"Fail"; 10 | 11 | System.out.println(check); 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /08. Conditional Statements/p8_switch_statement.java: -------------------------------------------------------------------------------- 1 | public class p8_switch_statement { 2 | 3 | // This Code is Written By Priyanshu Gour 4 | public static void main(String args[]){ 5 | 6 | 7 | int num = 3 ; 8 | 9 | 10 | switch(num){ 11 | case 1 : { 12 | System.out.println("Monday"); 13 | break ; 14 | } 15 | case 2 : { 16 | System.out.println("Tuesday"); 17 | break ; 18 | } 19 | case 3 : { 20 | System.out.println("Wednesday"); 21 | break ; 22 | } 23 | case 4 : { 24 | System.out.println("Thrusday"); 25 | break ; 26 | } 27 | case 5 : { 28 | System.out.println("Friday"); 29 | break ; 30 | } 31 | case 6 : { 32 | System.out.println("Saturday"); 33 | break ; 34 | } 35 | case 7 : { 36 | System.out.println("Sunday"); 37 | break ; 38 | } 39 | default : { 40 | System.out.println("Please Enter Right Choice ..."); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /08. Conditional Statements/p9_using_switch_Calculator.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // This Code is Written By Priyanshu Gour 4 | 5 | public class p9_using_switch_Calculator { 6 | public static void main(String args[]) { 7 | 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int a = sc.nextInt(); 11 | int b = sc.nextInt(); 12 | 13 | char c = sc.next().charAt(0); 14 | 15 | switch(c){ 16 | case '-' : { 17 | System.out.println(a-b); 18 | break ; 19 | } 20 | case '+' : { 21 | System.out.println(a+b); 22 | break ; 23 | } 24 | case '/' : { 25 | System.out.println(a/b); 26 | break ; 27 | } 28 | case '*' : { 29 | System.out.println(a*b); 30 | break ; 31 | } 32 | case '%' : { 33 | System.out.println(a%b); 34 | break ; 35 | } 36 | default : { 37 | System.out.println("Please Enter valid Operator.."); 38 | } 39 | } 40 | 41 | sc.close(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/Loops Questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/09. Loops(Flow Control)/Loops Questions.pdf -------------------------------------------------------------------------------- /09. Loops(Flow Control)/Loops Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/09. Loops(Flow Control)/Loops Solutions.pdf -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p10_input_multiple_of_10.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p10_input_multiple_of_10 { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void main(String args[]){ 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int input; 11 | 12 | while(true){ 13 | 14 | input = sc.nextInt(); 15 | 16 | if(input % 10 == 0){ 17 | break; 18 | } 19 | System.out.println(input); 20 | } 21 | 22 | sc.close(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p11_Continue_Statement.java: -------------------------------------------------------------------------------- 1 | public class p11_Continue_Statement { 2 | public static void main(String args[]){ 3 | 4 | // this code is writen by Priyanshu Gour 5 | 6 | for(int i = 1 ; 10 >= i ; i++){ 7 | if(i % 2 == 0){ 8 | continue; 9 | } 10 | System.out.println(i); 11 | } 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p12_print_number_except_multiples_of_10.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p12_print_number_except_multiples_of_10 { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void main(String args[]){ 8 | 9 | try (Scanner sc = new Scanner(System.in)) { 10 | 11 | int checknumber; 12 | System.out.println("if you want to exit the loop then you will click '0'"); 13 | while(true){ 14 | 15 | checknumber = sc.nextInt(); 16 | if(checknumber == 0){ 17 | break; 18 | } 19 | else if(checknumber%10 == 0 ){ 20 | continue; 21 | } 22 | System.out.println(checknumber); 23 | } 24 | } 25 | 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p13_Number_is_prime_or_not.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p13_Number_is_prime_or_not { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void main(String args[]){ 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int num = sc.nextInt(); 11 | 12 | boolean isprime = true ; 13 | 14 | if(num == 2){ 15 | System.out.println(num + " is prime Number.."); 16 | }else{ 17 | for(int i = 2 ; num-1 >= i ; i++){ 18 | if(num % i==0){ 19 | isprime = false; 20 | } 21 | } 22 | } 23 | 24 | if(isprime){ 25 | System.out.println(num+" is prime..."); 26 | }else{ 27 | System.out.println(num+" is not prime"); 28 | } 29 | 30 | sc.close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p14_Optmize_way_to_prime.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p14_Optmize_way_to_prime { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void main(String args[]){ 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int num = sc.nextInt(); 11 | 12 | boolean isprime = true ; 13 | 14 | if(num == 2){ 15 | System.out.println(num + " is prime Number.."); 16 | }else{ 17 | for(int i = 2 ; Math.sqrt(num) >= i ; i++){ 18 | if(num % i==0){ 19 | isprime = false; 20 | } 21 | } 22 | } 23 | 24 | if(isprime){ 25 | System.out.println(num+" is prime..."); 26 | }else{ 27 | System.out.println(num+" is not prime"); 28 | } 29 | 30 | sc.close(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p1_while_Loop.java: -------------------------------------------------------------------------------- 1 | public class p1_while_Loop { 2 | public static void main(String args[]){ 3 | 4 | // this code is writen by Priyanshu Gour 5 | 6 | int a = 1 ; 7 | 8 | while(10 >= a){ 9 | System.out.println(a); 10 | a++; 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p2_print_numbers_to_n_numbers.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p2_print_numbers_to_n_numbers { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void main(String args[]) { 8 | 9 | Scanner sc = new Scanner(System.in); 10 | 11 | int i = 1, n = sc.nextInt(); 12 | 13 | while (i <= n) { 14 | System.out.println(i); 15 | i++; 16 | } 17 | 18 | sc.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p3_sum_of_first_n_natural_number.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p3_sum_of_first_n_natural_number { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void main(String args[]) { 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int sum = 0, i = 1 , n = sc.nextInt(); 11 | 12 | while(i <= n){ 13 | sum += i; 14 | i++; 15 | } 16 | 17 | System.out.println(sum); 18 | 19 | sc.close(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p4_for_Loop.java: -------------------------------------------------------------------------------- 1 | public class p4_for_Loop { 2 | public static void main(String args[]){ 3 | 4 | // this code is writen by Priyanshu Gour 5 | 6 | 7 | 8 | for(int i = 1 ; 10 >= i ; i++){ 9 | System.out.println(i); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p5_print_square.java: -------------------------------------------------------------------------------- 1 | public class p5_print_square { 2 | public static void main(String args[]){ 3 | 4 | // this code is writen by Priyanshu Gour 5 | 6 | 7 | for(int i = 1 ; 5 >= i ; i++){ 8 | System.out.println("* * * * *"); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p6_print_reverse_of_a_number.java: -------------------------------------------------------------------------------- 1 | public class p6_print_reverse_of_a_number { 2 | public static void main(String args[]){ 3 | 4 | // this code is writen by Priyanshu Gour 5 | 6 | int dob = 40804 ; 7 | 8 | while(dob > 0){ 9 | int lastdigit = dob % 10 ; 10 | System.out.print(lastdigit); 11 | dob = dob/10 ; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p7_reverse_the_given_nubmer.java: -------------------------------------------------------------------------------- 1 | public class p7_reverse_the_given_nubmer { 2 | public static void main(String args[]){ 3 | 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | int n = 12345 ; 8 | int temp = 0; 9 | 10 | while(n > 0){ 11 | int lastdigit = n % 10 ; 12 | temp = (temp * 10) + lastdigit ; 13 | n = n/10; 14 | } 15 | n = temp; 16 | System.out.println(n); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p8_do_while_loop.java: -------------------------------------------------------------------------------- 1 | public class p8_do_while_loop { 2 | public static void main(String args[]){ 3 | 4 | // this code is writen by Priyanshu Gour 5 | 6 | 7 | int counter = 1 ; 8 | 9 | do{ 10 | System.out.println("Hello Priyanshu .. "+ counter); 11 | counter++; 12 | }while(10 >= counter); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /09. Loops(Flow Control)/p9_break_statement.java: -------------------------------------------------------------------------------- 1 | public class p9_break_statement { 2 | public static void main(String args[]){ 3 | 4 | // this code is writen by Priyanshu Gour 5 | 6 | 7 | // break :- to exit the loop .. 8 | 9 | for(int i = 1 ; 10 >= i ; i++){ 10 | if(i == 5){ 11 | break; // to exit the loop. 12 | } 13 | System.out.println(i); 14 | } 15 | 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /10. Patterns(Part-1)/2-Advanced Patterns Note.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/10. Patterns(Part-1)/2-Advanced Patterns Note.pdf -------------------------------------------------------------------------------- /10. Patterns(Part-1)/p1_intro_to_Nested_Loops_star_Pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p1_intro_to_Nested_Loops_star_Pattern { 4 | 5 | // this code is writen by Priyanshu Gour 6 | public static void main(String args[]){ 7 | 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n = sc.nextInt(); 11 | 12 | for(int i = 1 ; n >= i ; i++){ 13 | 14 | for(int j = 1 ; i >= j ; j++){ 15 | System.out.print("* "); 16 | } 17 | System.out.println(); 18 | } 19 | 20 | sc.close(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /10. Patterns(Part-1)/p2_Inverted_Strar_Patter.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p2_Inverted_Strar_Patter { 4 | 5 | // this code is writen by Priyanshu Gour 6 | public static void main(String args[]){ 7 | 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n = sc.nextInt(); 11 | 12 | for(int i = 1; n >= i ; n--){ 13 | 14 | for(int j = 1 ; n >= j ; j++){ 15 | System.out.print("* "); 16 | } 17 | System.out.println(); 18 | } 19 | 20 | sc.close(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /10. Patterns(Part-1)/p3_Half_Pyramid_Pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p3_Half_Pyramid_Pattern { 4 | 5 | // this code is writen by Priyanshu Gour 6 | public static void main(String args[]){ 7 | 8 | Scanner sc = new Scanner(System.in); 9 | 10 | int n = sc.nextInt(); 11 | 12 | for(int i = 1 ; n >= i ; i++){ 13 | 14 | for(int j = 1 ; i >= j ; j++){ 15 | System.out.print(j+" "); 16 | } 17 | 18 | System.out.println(); 19 | } 20 | 21 | sc.close(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /10. Patterns(Part-1)/p4_Character_pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p4_Character_pattern { 4 | 5 | // this code is writen by Priyanshu Gour 6 | public static void main(String args[]){ 7 | Scanner sc = new Scanner(System.in); 8 | 9 | char ch = 'A'; 10 | int n = sc.nextInt(); 11 | 12 | for(int i = 1 ; n >= i ; i++ ){ 13 | 14 | for(int j = 1 ; i >= j ; j++){ 15 | System.out.print(ch+" "); 16 | ch++; 17 | } 18 | 19 | System.out.println(); 20 | } 21 | 22 | sc.close(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /11. Functions & Methods/Functions Questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/11. Functions & Methods/Functions Questions.pdf -------------------------------------------------------------------------------- /11. Functions & Methods/Functions Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/11. Functions & Methods/Functions Solutions.pdf -------------------------------------------------------------------------------- /11. Functions & Methods/p10_a_number_is_prime_or_not.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p10_a_number_is_prime_or_not { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static boolean checkprime(int a){ 8 | 9 | boolean isprime = true ; 10 | 11 | for(int i = 2 ; a-1 >= i ; i++ ){ 12 | if(a%i==0){ 13 | isprime = false; 14 | } 15 | } 16 | 17 | return isprime ; 18 | } 19 | 20 | 21 | public static void main(String args[]){ 22 | Scanner sc = new Scanner(System.in); 23 | 24 | int primeornot = sc.nextInt(); 25 | 26 | boolean primeis = checkprime(primeornot); 27 | 28 | if(primeis){ 29 | System.out.println("Your No. is prime..."); 30 | }else{ 31 | System.out.println("Your No. is Not prime..."); 32 | } 33 | 34 | 35 | sc.close(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /11. Functions & Methods/p11_a_number_is_prime_or_not_Optimized.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p11_a_number_is_prime_or_not_Optimized { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static boolean optimized_prime(int a) { 8 | 9 | boolean isprime = true; 10 | 11 | for(int i = 1 ; Math.sqrt(a) >= i ; i++){ 12 | if(a % i == 0){ 13 | isprime = false; 14 | } 15 | } 16 | 17 | return isprime ; 18 | 19 | } 20 | 21 | public static void main(String args[]) { 22 | Scanner sc = new Scanner(System.in); 23 | 24 | int n = sc.nextInt(); 25 | 26 | boolean primeis = optimized_prime(n); 27 | 28 | if (primeis) { 29 | System.out.println("Your No. is Prime.."); 30 | } else { 31 | System.out.println("Your No. id NOT Prime.."); 32 | } 33 | 34 | sc.close(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /11. Functions & Methods/p12_Primes_in_Range.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p12_Primes_in_Range { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static boolean rangeprime(int n){ 8 | 9 | if(n == 2 ){ 10 | return true; 11 | } 12 | for(int i = 2 ; Math.sqrt(n) >= i ; i++){ 13 | if(n % i == 0){ 14 | return false; 15 | } 16 | } 17 | return true ; 18 | } 19 | 20 | 21 | public static void main(String args[]){ 22 | Scanner sc = new Scanner(System.in); 23 | 24 | int n = sc.nextInt(); 25 | 26 | for(int i = 2 ; n >= i ; i++){ 27 | if(rangeprime(i)){ 28 | System.out.println(i+"is Prime Number.......!"); 29 | } 30 | } 31 | 32 | 33 | sc.close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /11. Functions & Methods/p13_Binary_to_Decimal.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p13_Binary_to_Decimal { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void Binary_to_Decimal(int b) { 8 | 9 | int myNum = b ; 10 | int pow = 0 ; 11 | int decNum = 0 ; 12 | 13 | while(b > 0){ 14 | int lastDigit = b % 10 ; 15 | decNum = decNum + (lastDigit * (int)Math.pow(2,pow)); 16 | 17 | pow++; 18 | b = b/10 ; 19 | } 20 | 21 | System.out.println("Binary number of "+myNum+" is "+decNum); 22 | 23 | } 24 | 25 | public static void main(String args[]) { 26 | Scanner sc = new Scanner(System.in); 27 | 28 | int binary = sc.nextInt(); 29 | 30 | Binary_to_Decimal(binary); 31 | 32 | sc.close(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /11. Functions & Methods/p14_Decimal_to_Binary.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p14_Decimal_to_Binary { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static int Decimal_to_Binary(int Dec){ 8 | 9 | int binNum = 0 ; 10 | int rem = 0 ; 11 | int pow = 0 ; 12 | while(Dec > 0){ 13 | rem = Dec % 2 ; 14 | binNum += (rem * (int)(Math.pow(10,pow))); 15 | 16 | pow++; 17 | Dec /= 2; 18 | 19 | } 20 | 21 | return binNum; 22 | } 23 | 24 | 25 | public static void main(String args[]){ 26 | Scanner sc = new Scanner(System.in); 27 | 28 | int Dec = sc.nextInt(); 29 | 30 | int bin = Decimal_to_Binary(Dec); 31 | 32 | System.out.println("Binary Number of "+Dec+" is "+bin); 33 | 34 | sc.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /11. Functions & Methods/p1_Syntax_of_Void_function_in_java.java: -------------------------------------------------------------------------------- 1 | public class p1_Syntax_of_Void_function_in_java { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void helloworld(){ 6 | 7 | System.err.println("Hello World...!"); 8 | System.err.println("Hello World...!"); 9 | System.err.println("Hello World...!"); 10 | System.err.println("Hello World...!"); 11 | System.err.println("Hello World...!"); 12 | } 13 | 14 | public static void main(String args[]){ 15 | 16 | helloworld(); 17 | 18 | } 19 | } -------------------------------------------------------------------------------- /11. Functions & Methods/p2_Syntax_of_int_function_in_java.java: -------------------------------------------------------------------------------- 1 | public class p2_Syntax_of_int_function_in_java { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static int sumoftwonum(int x,int y){ 6 | 7 | int sum = x + y ; 8 | 9 | return sum ; 10 | 11 | } 12 | 13 | 14 | public static void main(String args[]){ 15 | 16 | int a = 10 , b = 20 ; 17 | int fun = sumoftwonum(a,b); 18 | 19 | System.out.println(fun); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /11. Functions & Methods/p3_Parameters_and_Arguments.java: -------------------------------------------------------------------------------- 1 | public class p3_Parameters_and_Arguments { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static int calsum(int a , int b){ // Parameters or Formal parameters.. 6 | 7 | int sum = a + b ; 8 | return sum ; 9 | } 10 | 11 | public static void main(String args[]){ 12 | 13 | int x = 32 ; 14 | int y = 43 ; 15 | 16 | int addtion = calsum(x,y); // arguments or actual parameters.. 17 | 18 | System.out.println(addtion); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /11. Functions & Methods/p4_Call_by_value_and_Reference.java: -------------------------------------------------------------------------------- 1 | public class p4_Call_by_value_and_Reference { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void swap(int a , int b){ // this is the call by reference because does not change the actual value.. 6 | 7 | int temp = a ; // this is the call by reference .. because does not effect on the actual value... 8 | a = b ; // this is the call by reference .. because does not effect on the actual value... 9 | b = temp ; // this is the call by reference .. because does not effect on the actual value... 10 | 11 | System.out.println(a); 12 | System.out.println(b); 13 | } 14 | 15 | public static void main(String args[]){ // ** in java always pass the value in function will pass as a copy .. 16 | 17 | int a = 34 ; // this is the actual value .. because changes are actual .. 18 | int b = 98 ; // this is the actual value .. because changes are actual .. 19 | 20 | swap(a, b); 21 | 22 | System.out.println("After the execution of function the value is : "); 23 | System.out.println(a); 24 | System.out.println(b); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /11. Functions & Methods/p5_product_of_a_and_b.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p5_product_of_a_and_b { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static int productoftwono(int a , int b){ 8 | 9 | int productis = a * b ; 10 | 11 | return productis ; 12 | } 13 | 14 | public static void main(String args[]){ 15 | Scanner sc = new Scanner(System.in); 16 | 17 | int a = sc.nextInt(); 18 | int b = sc.nextInt(); 19 | 20 | int mulofab = productoftwono(a, b); 21 | 22 | System.out.println(mulofab); 23 | 24 | 25 | sc.close(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /11. Functions & Methods/p6_Factorial_in_java.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | 4 | public class p6_Factorial_in_java { 5 | 6 | // this code is writen by Priyanshu Gour 7 | 8 | public static int factorial(int a){ 9 | int fact = 1 ; 10 | 11 | for(int i = 1 ; a >= i ; i++){ 12 | fact *= i ; 13 | } 14 | 15 | return fact ; 16 | } 17 | 18 | public static void main(String args[]){ 19 | Scanner sc = new Scanner(System.in); 20 | 21 | 22 | int fct = sc.nextInt(); 23 | 24 | System.out.println(factorial(fct)); 25 | 26 | sc.close(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /11. Functions & Methods/p7_Binomial_Coefficient.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p7_Binomial_Coefficient { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static int factorial(int f) { 8 | int fact = 1; 9 | 10 | for (int i = 1; i <= f; i++) { 11 | fact *= i; 12 | } 13 | 14 | return fact; 15 | } 16 | 17 | public static int Binomial_Coefficient(int n, int r) { 18 | 19 | int fn = factorial(n); 20 | int fr = factorial(r); 21 | int fnr = factorial(n - r); 22 | 23 | int binocoeff = fn / (fr * fnr); 24 | 25 | return binocoeff; 26 | } 27 | 28 | public static void main(String args[]) { 29 | Scanner sc = new Scanner(System.in); 30 | 31 | int n = sc.nextInt(); 32 | int r = sc.nextInt(); 33 | 34 | System.out.println(Binomial_Coefficient(n, r)); 35 | 36 | sc.close(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /11. Functions & Methods/p8_overloading_using_parameters.java: -------------------------------------------------------------------------------- 1 | public class p8_overloading_using_parameters { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static int sum(int a, int b, int c){ 6 | return a+b+c ; 7 | } 8 | // Function Overloading is know as the name of function is it same but its parameters are differents.. 9 | public static int sum(int a , int b){ 10 | return a+b ; 11 | } 12 | 13 | public static void main(String args[]){ 14 | System.out.println(sum(5,2)); 15 | System.out.println(sum(5,2,3)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /11. Functions & Methods/p9_overloading_using_Data_Types.java: -------------------------------------------------------------------------------- 1 | public class p9_overloading_using_Data_Types { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static float sum(double d, double e) { 6 | return (float) (d + e) ; 7 | } 8 | 9 | // Function Overloading using DataTypes... same as it is function only change the datatype of function... 10 | public static int sum(int a, int b) { 11 | return a + b; 12 | } 13 | 14 | public static void main(String args[]) { 15 | System.out.println(sum(5.32, 2.31)); 16 | System.out.println(sum(5, 2)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /12. Patterns(Part-2)- Advanced/p10_Number_Pyramid.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p10_Number_Pyramid { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void Number_Pyramid(int n){ 8 | 9 | int count = 1 ; 10 | 11 | for(int i = 1 ; n >= i ; i++){ 12 | 13 | for(int j = 1 ; n-i >= j ;j++){ 14 | System.out.print(" "); 15 | } 16 | for(int k = 1 ; i >= k ; k++){ 17 | System.out.print(count+" "); 18 | } 19 | 20 | System.out.println(); 21 | count++; 22 | } 23 | } 24 | 25 | 26 | public static void main(String args[]){ 27 | Scanner sc = new Scanner(System.in); 28 | 29 | int n = sc.nextInt(); 30 | 31 | Number_Pyramid(n); 32 | 33 | sc.close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /12. Patterns(Part-2)- Advanced/p11_Palindromic_Pattern_with_Numbers.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p11_Palindromic_Pattern_with_Numbers { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void Palindromic_Pattern(int n){ 8 | 9 | for(int i = 1 ; n >= i ; i++){ 10 | 11 | int x = i ; 12 | 13 | for(int j = 1 ; n-i >= j ; j++){ 14 | System.out.print(" "); 15 | } 16 | 17 | for(int k = 1 ; i >= k ; k++,x--){ 18 | System.out.print(x+" "); 19 | } 20 | 21 | for(int l = 1 ; i-1 >= l ; l++){ 22 | System.out.print(l+1+" "); 23 | } 24 | 25 | System.out.println(); 26 | } 27 | } 28 | 29 | public static void main(String args[]) { 30 | Scanner sc = new Scanner(System.in); 31 | 32 | int n = sc.nextInt(); 33 | 34 | Palindromic_Pattern(n); 35 | 36 | sc.close(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /12. Patterns(Part-2)- Advanced/p1_Hollow_Rectangle_Pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p1_Hollow_Rectangle_Pattern { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void Hollow_rectangle(int row , int col){ 8 | 9 | 10 | for(int i = 1 ; row >= i ; i++){ 11 | 12 | for(int j = 1 ; col >= j ; j++ ){ 13 | if(i == row || i == 1 || j == 1 || j == col){ 14 | System.out.print("* "); 15 | }else{ 16 | System.out.print(" "); 17 | } 18 | } 19 | 20 | System.out.println(); 21 | 22 | } 23 | } 24 | 25 | 26 | public static void main(String args[]){ 27 | Scanner sc = new Scanner(System.in); 28 | 29 | int row = sc.nextInt(); 30 | int col = sc.nextInt(); 31 | 32 | Hollow_rectangle(row , col); 33 | 34 | 35 | sc.close(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /12. Patterns(Part-2)- Advanced/p2_Inverted_and_Rotated_Half_pyramid.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p2_Inverted_and_Rotated_Half_pyramid { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void Inverted_Rotated_Half_Pyramid(int n ){ 8 | 9 | for(int i = 1 ; n >= i ; i++){ 10 | 11 | for(int j = 1 ; n-i >= j ; j++){ 12 | System.out.print(" "); 13 | } 14 | for(int k = 1 ; i >= k ; k++){ 15 | System.out.print("* "); 16 | } 17 | 18 | System.out.println(); 19 | } 20 | } 21 | 22 | public static void main(String args[]){ 23 | Scanner sc = new Scanner(System.in); 24 | 25 | int n = sc.nextInt(); 26 | 27 | Inverted_Rotated_Half_Pyramid(n); 28 | 29 | sc.close(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /12. Patterns(Part-2)- Advanced/p3_Inverted_Half_Pyramid_Pattern_with_Numbers.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p3_Inverted_Half_Pyramid_Pattern_with_Numbers { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void Inverted_Half_Pyramid_Pattern_with_Numbers(int n ){ 8 | int k = n ; 9 | for(int i = 1 ; n >= i ; i++ ){ 10 | 11 | for(int j = 1 ; k >= j ; j++){ 12 | System.out.print(j+" "); 13 | } 14 | k--; 15 | System.out.println(); 16 | } 17 | } 18 | 19 | 20 | public static void main(String args[]){ 21 | Scanner sc = new Scanner(System.in); 22 | 23 | int n = sc.nextInt(); 24 | 25 | Inverted_Half_Pyramid_Pattern_with_Numbers(n); 26 | 27 | sc.close(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /12. Patterns(Part-2)- Advanced/p4_Floyds_Triangle_Pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p4_Floyds_Triangle_Pattern { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void Floyds_Triangle_Pattern(int n) { 8 | int count = 1; 9 | 10 | for (int i = 1; n >= i; i++) { 11 | 12 | for (int j = 1; i >= j; j++) { 13 | System.out.print(count + " "); 14 | count++; 15 | } 16 | 17 | System.out.println(); 18 | } 19 | } 20 | 21 | public static void main(String args[]) { 22 | Scanner sc = new Scanner(System.in); 23 | 24 | int n = sc.nextInt(); 25 | 26 | Floyds_Triangle_Pattern(n); 27 | 28 | sc.close(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /12. Patterns(Part-2)- Advanced/p5_0_1_Triangle_Pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p5_0_1_Triangle_Pattern { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void _0_1_Triangle_Pattern(int n){ 8 | 9 | 10 | for(int i = 1 ; n >= i ; i++){ 11 | 12 | for(int j = 1 ; i >= j ; j++){ 13 | if((i+j)%2==0){ 14 | System.out.print("1 "); 15 | }else{ 16 | System.out.print("0 "); 17 | } 18 | } 19 | 20 | System.out.println(); 21 | } 22 | } 23 | 24 | public static void main(String args[]){ 25 | Scanner sc = new Scanner(System.in); 26 | 27 | int n = sc.nextInt(); 28 | 29 | _0_1_Triangle_Pattern(n); 30 | 31 | 32 | sc.close(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /12. Patterns(Part-2)- Advanced/p6_Butterfly_Pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p6_Butterfly_Pattern { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void Butterfly_Pattern(int n) { 8 | 9 | for (int i = 1; n >= i; i++) { 10 | 11 | for (int j = 1; i >= j; j++) { 12 | System.out.print("* "); 13 | } 14 | for (int k = 1; (n - i) * 2 >= k; k++) { 15 | System.out.print(" "); 16 | } 17 | for (int l = 1; i >= l; l++) { 18 | System.out.print("* "); 19 | } 20 | 21 | System.out.println(); 22 | } 23 | for (int i = 1; n >= i; i++) { 24 | 25 | for (int j = 1; n - i + 1 >= j; j++) { 26 | System.out.print("* "); 27 | } 28 | for (int k = 1; (i - 1) * 2 >= k; k++) { 29 | System.out.print(" "); 30 | } 31 | for (int l = 1; n - i + 1 >= l; l++) { 32 | System.out.print("* "); 33 | } 34 | 35 | System.out.println(); 36 | } 37 | } 38 | 39 | public static void main(String args[]) { 40 | Scanner sc = new Scanner(System.in); 41 | 42 | int n = sc.nextInt(); 43 | 44 | Butterfly_Pattern(n); 45 | 46 | sc.close(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /12. Patterns(Part-2)- Advanced/p7_Solid_Rhombus_Pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class p7_Solid_Rhombus_Pattern { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void Solid_Rhombus_Pattern(int n){ 8 | 9 | for(int i = 1 ; n >= i ; i++){ 10 | 11 | for(int j = 1 ; n-i >= j ; j++){ 12 | System.out.print(" "); 13 | } 14 | for(int k = 1 ; n >= k ; k++){ 15 | System.out.print("* "); 16 | } 17 | 18 | System.out.println(); 19 | } 20 | } 21 | 22 | public static void main(String args[]){ 23 | Scanner sc = new Scanner(System.in); 24 | 25 | int n = sc.nextInt(); 26 | 27 | Solid_Rhombus_Pattern(n); 28 | 29 | sc.close(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /12. Patterns(Part-2)- Advanced/p8_Hollow_Rhombus_Pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p8_Hollow_Rhombus_Pattern { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void Hollow_Rhombus_Pattern(int n) { 8 | 9 | for (int i = 1; n >= i; i++) { 10 | 11 | for (int j = 1; n - i >= j; j++) { 12 | System.out.print(" "); 13 | } 14 | for (int k = 1; n >= k; k++) { 15 | if (i == 1 || i == n || k == 1 || k == n) { 16 | System.out.print("* "); 17 | } else { 18 | System.out.print(" "); 19 | } 20 | } 21 | 22 | System.out.println(); 23 | } 24 | 25 | } 26 | 27 | public static void main(String args[]) { 28 | Scanner sc = new Scanner(System.in); 29 | 30 | int n = sc.nextInt(); 31 | 32 | Hollow_Rhombus_Pattern(n); 33 | 34 | sc.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /12. Patterns(Part-2)- Advanced/p9_Diamod_Pattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p9_Diamod_Pattern { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void Diamod_Pattern(int n){ 8 | 9 | 10 | for(int i = 1 ; n >= i ; i++){ 11 | 12 | for(int j = 1 ; n-i >= j ; j++){ 13 | System.out.print(" "); 14 | } 15 | for(int k = 1 ; (2*i)-1 >= k ; k++){ 16 | System.out.print("* "); 17 | } 18 | 19 | System.out.println(); 20 | } 21 | 22 | for(int i = n ; i >= 1 ; i--){ 23 | 24 | for(int k = 1 ; (n-i) >= k ; k++){ 25 | System.out.print(" "); 26 | } 27 | 28 | for(int j = 1 ; (2*i)-1 >= j ; j++ ){ 29 | System.out.print("* "); 30 | } 31 | 32 | System.out.println(); 33 | } 34 | } 35 | 36 | 37 | public static void main(String args[]){ 38 | Scanner sc = new Scanner(System.in); 39 | 40 | int n = sc.nextInt(); 41 | 42 | Diamod_Pattern(n); 43 | 44 | sc.close(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /13. ARRAYS/Arrays - Assignment_Type.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/13. ARRAYS/Arrays - Assignment_Type.pdf -------------------------------------------------------------------------------- /13. ARRAYS/Arrays - Solution (1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/13. ARRAYS/Arrays - Solution (1).pdf -------------------------------------------------------------------------------- /13. ARRAYS/Space & Time Complexity (1) (1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/13. ARRAYS/Space & Time Complexity (1) (1).pdf -------------------------------------------------------------------------------- /13. ARRAYS/p10_Print_Subarrays.java: -------------------------------------------------------------------------------- 1 | public class p10_Print_Subarrays { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void Print_Subarrays(int arr[]){ 6 | 7 | int total = 0 ; 8 | for(int i = 0 ; arr.length >= i ; i++){ 9 | 10 | for(int j = i ; arr.length > j ; j++){ 11 | 12 | System.out.print("("); 13 | for(int k = i ; j >= k ; k++){ 14 | System.out.print(arr[k]+","); 15 | } 16 | total++; 17 | System.err.print(")"); 18 | } 19 | 20 | System.out.println(); 21 | } 22 | 23 | System.out.println("Total SubArray is : "+total); 24 | } 25 | 26 | public static void main(String args[]){ 27 | 28 | int arr[] = {2,4,6,8,10}; 29 | 30 | Print_Subarrays(arr); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /13. ARRAYS/p11_Sum_of_Subarray.java: -------------------------------------------------------------------------------- 1 | public class p11_Sum_of_Subarray { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void Sum_of_Subarray(int arr[]){ 6 | 7 | for(int i = 0 ; arr.length > i ; i++){ 8 | int sum = 0; 9 | 10 | for(int j = i ; arr.length > j ; j++){ 11 | System.out.print("("); 12 | for(int k = i ; j >= k ; k++){ 13 | sum+= arr[k]; 14 | } 15 | System.out.print(sum); 16 | System.out.print(")"); 17 | } 18 | 19 | System.out.println(); 20 | } 21 | } 22 | 23 | public static void main(String args[]){ 24 | int arr[] = {2,4,6,8,10}; 25 | 26 | Sum_of_Subarray(arr); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /13. ARRAYS/p12_Max_Subarray_Sum_1_Brute_Force.java: -------------------------------------------------------------------------------- 1 | public class p12_Max_Subarray_Sum_1_Brute_Force { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void Max_of_Subarray(int arr[]) { 6 | 7 | int sum = 0; 8 | int maxSum = Integer.MIN_VALUE ; 9 | 10 | for (int i = 0; arr.length > i; i++) { 11 | 12 | for (int j = i; arr.length > j; j++) { 13 | sum = 0; 14 | System.out.print("("); 15 | for (int k = i; j >= k; k++) { 16 | sum += arr[k]; 17 | } 18 | System.out.print(sum); 19 | System.out.print(")"); 20 | if(sum > maxSum){ 21 | maxSum = sum ; 22 | } 23 | } 24 | 25 | System.out.println(); 26 | } 27 | System.out.println("Max of the arre is : "+maxSum); 28 | } 29 | 30 | public static void main(String args[]) { 31 | int arr[] = { 1,-2,6,-1,3}; 32 | 33 | Max_of_Subarray(arr); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /13. ARRAYS/p13_Max_Subarray_Sum_2_Prefix_Sum.java: -------------------------------------------------------------------------------- 1 | public class p13_Max_Subarray_Sum_2_Prefix_Sum { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void Max_of_Subarray(int arr[]) { 6 | 7 | int sum = 0; 8 | int maxSum = Integer.MIN_VALUE ; 9 | int prefix[] = new int[arr.length]; 10 | 11 | 12 | prefix[0] = arr[0]; 13 | 14 | for(int i = 1 ; i < prefix.length ; i++){ 15 | prefix[i] = prefix[i-1] + arr[i]; 16 | } 17 | 18 | for (int i = 0; arr.length > i; i++) { 19 | 20 | for (int j = i; arr.length > j; j++) { 21 | 22 | sum = i == 0 ? prefix[j] : prefix[j] - prefix[i-1]; 23 | 24 | if(sum > maxSum){ 25 | maxSum = sum ; 26 | } 27 | } 28 | 29 | System.out.println(); 30 | } 31 | System.out.println("Max of the arre is : "+maxSum); 32 | } 33 | 34 | public static void main(String args[]) { 35 | int arr[] = { 1,-2,6,-1,3}; 36 | 37 | Max_of_Subarray(arr); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /13. ARRAYS/p14_Max_Subarray_Sum_3_Kadane_s_Algorithm.java: -------------------------------------------------------------------------------- 1 | public class p14_Max_Subarray_Sum_3_Kadane_s_Algorithm { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void Kadanes_Algorithm(int arr[]){ 6 | 7 | int ms = Integer.MIN_VALUE ; 8 | int cs = 0 ; 9 | 10 | for(int i = 0 ; i < arr.length ; i++){ 11 | 12 | cs = cs + arr[i]; 13 | 14 | if(cs < 0){ 15 | cs = 0 ; 16 | } 17 | 18 | ms = Math.max(cs, ms); 19 | } 20 | 21 | System.out.println("Maximum SubArray is(Using Kadane's Algorithm ) : "+ms); 22 | } 23 | 24 | public static void main(String args[]){ 25 | 26 | int arr[] = {-2,-3,4,-1,-2,1,5,-3}; 27 | 28 | Kadanes_Algorithm(arr); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /13. ARRAYS/p15_Trapping_Rainwater_Auxiliary_Array_IMP.java: -------------------------------------------------------------------------------- 1 | public class p15_Trapping_Rainwater_Auxiliary_Array_IMP { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | 6 | public static int TrappedRainwater(int height[]){ 7 | 8 | //Question. :- Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining... 9 | 10 | // Ans:- 11 | 12 | int n = height.length; 13 | //Calculate left max boundary -Auxiliary/Helper Array 14 | int leftmax[] = new int[n]; 15 | 16 | leftmax[0] = height[0]; 17 | 18 | for(int i = 1; n > i ; i++){ 19 | leftmax[i] = Math.max(height[i] , leftmax[i-1]); 20 | } 21 | 22 | //Calculate right max boundary -Auxiliary/Helper Array 23 | 24 | int rightmax[] = new int[n]; 25 | 26 | rightmax[n-1] = height[n-1]; 27 | 28 | for(int i = n-2 ; i >=0 ; i--){ 29 | rightmax[i] = Math.max(height[i],rightmax[i+1]); 30 | } 31 | 32 | // loop 33 | int trappedWater = 0 ; 34 | 35 | for(int i = 0 ; n > i; i++){ 36 | 37 | //WaterLevel = min (leftmax bound, rightmax bound) 38 | int waterLevel = Math.min(rightmax[i],leftmax[i]); 39 | 40 | //Trapped water = waterLevel - height[i] 41 | trappedWater += waterLevel - height[i] ; 42 | } 43 | 44 | return trappedWater ; 45 | 46 | } 47 | 48 | 49 | public static void main(String args[]){ 50 | 51 | int height[] = {4,2,0,6,3,2,5}; 52 | 53 | int trapWaterStore = TrappedRainwater(height); 54 | 55 | System.out.println("Total Water are Store in : "+trapWaterStore); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /13. ARRAYS/p16_Best_Time_to_Buy_And_Sell_Stock.java: -------------------------------------------------------------------------------- 1 | public class p16_Best_Time_to_Buy_And_Sell_Stock { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static int buyOrSellStock(int arr[]){ 6 | 7 | int buyPrice = Integer.MAX_VALUE ; 8 | int maxProfit = 0 ; 9 | 10 | for(int i = 0 ; arr.length > i ; i++){ 11 | 12 | if(buyPrice < arr[i]){ 13 | int profit = arr[i] - buyPrice ; 14 | maxProfit = Math.max(profit, maxProfit); 15 | }else{ 16 | buyPrice = arr[i]; 17 | } 18 | } 19 | return maxProfit ; 20 | } 21 | 22 | public static void main(String args []){ 23 | 24 | int price[] = {7,1,5,3,6,4}; 25 | 26 | int Stock = buyOrSellStock(price); 27 | 28 | System.out.println(Stock); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /13. ARRAYS/p17_Array_appears_at_least_twice.java: -------------------------------------------------------------------------------- 1 | public class p17_Array_appears_at_least_twice { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static boolean arrayAppearsAtLeastTwice(int arr[]){ 6 | 7 | for(int i = 0 ; arr.length > i ; i++){ 8 | 9 | for(int j = i+1 ; arr.length > j ; j++){ 10 | if(arr[i] == arr[j]){ 11 | return true ; 12 | } 13 | } 14 | } 15 | return false ; 16 | } 17 | 18 | public static void main(String args[]){ 19 | int arr[] = {1,2,3,4,5,6,7,8,9,10,10}; 20 | 21 | boolean appearence = arrayAppearsAtLeastTwice(arr); 22 | 23 | System.out.println(appearence); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /13. ARRAYS/p18_Array_appears_at_least_twice_Approch2.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | 3 | public class p18_Array_appears_at_least_twice_Approch2 { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static boolean arrayAppearsAtLeastTwice(int nums[]) { 8 | 9 | HashSet set = new HashSet<>(); 10 | 11 | for (int i = 0; i < nums.length; i++) { 12 | if (set.contains(nums[i])) { 13 | return true; 14 | } else { 15 | set.add(nums[i]); 16 | } 17 | } 18 | return false; 19 | } 20 | 21 | public static void main(String args[]) { 22 | int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10 }; 23 | 24 | boolean appearence = arrayAppearsAtLeastTwice(arr); 25 | 26 | System.out.println(appearence); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /13. ARRAYS/p19_Sorted_Rotated_Array_using_Binary_Search.java: -------------------------------------------------------------------------------- 1 | public class p19_Sorted_Rotated_Array_using_Binary_Search { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | 6 | public static int search(int nums[], int target){ 7 | // min will have index of minimum element of nums 8 | int min = minSearch(nums); 9 | 10 | //find in sorted left 11 | 12 | if(nums[min] <= target && target <= nums[nums.length-1]){ 13 | return search(nums,min,nums.length-1, target); 14 | } 15 | 16 | //find in sorted right 17 | 18 | else{ 19 | return search(nums,0,min,target); 20 | } 21 | } 22 | 23 | 24 | // binary search to find target in left to right boundary.. 25 | public static int search(int nums[], int left, int right, int target){ 26 | int l = left ; 27 | int r = right ; 28 | 29 | // System.out.println(left+" "+right); 30 | 31 | while(l <= r){ 32 | int mid = l + (r-l)/2 ; 33 | 34 | if(nums[mid] == target){ 35 | return mid; 36 | } 37 | else if(nums[mid] > target){ 38 | r = mid-1; 39 | } 40 | else{ 41 | l = mid+1; 42 | } 43 | } 44 | return -1 ; 45 | } 46 | 47 | 48 | // smallest element index ; 49 | public static int minSearch(int nums[]){ 50 | int left = 0 ; 51 | int right = nums.length-1 ; 52 | 53 | while(left < right){ 54 | int mid = left + (right - left)/2 ; 55 | if(mid > 0 && nums[mid-1] > nums[mid]){ 56 | return mid; 57 | } 58 | else if(nums[left] <= nums[mid] && nums[mid] > nums[right]){ 59 | left = mid + 1 ; 60 | } 61 | else{ 62 | right = mid-1 ; 63 | } 64 | } 65 | return left ; 66 | } 67 | public static void main(String args []){ 68 | 69 | int arr[] = {5,6,7,8,9,1,2,3,4}; 70 | int target = 2 ; 71 | 72 | int indexNo = search(arr,target); 73 | 74 | System.out.println(indexNo); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /13. ARRAYS/p1_Arrays_creation.java: -------------------------------------------------------------------------------- 1 | public class p1_Arrays_creation { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void main(String args[]){ 6 | 7 | int marks[] = new int[10]; // this is known as array decleartaion... 8 | 9 | marks[0] = 10 ; 10 | 11 | int number[] = {1,2,3,4,5}; 12 | String name [] = {"Priyansu", "Devanshu", "Harshita", "Suhani"}; 13 | 14 | System.out.println(number[0]); 15 | System.out.println(name[0]); 16 | System.out.println(marks[0]); 17 | } 18 | } -------------------------------------------------------------------------------- /13. ARRAYS/p2_Array_input_output_update.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public class p2_Array_input_output_update { 6 | public static void main(String args[]){ 7 | Scanner sc = new Scanner(System.in); 8 | 9 | System.out.print("What is the size of Your array ? : "); 10 | int size = sc.nextInt(); 11 | 12 | int Numbers[] = new int[size]; 13 | 14 | for(int i = 0 ; size > i ; i++){ 15 | System.out.print("Enter "+i+1+" of value : "); 16 | Numbers[i] = sc.nextInt(); 17 | } 18 | 19 | for(int i = 0 ; size > i ; i++){ 20 | System.out.println(Numbers[i]); 21 | } 22 | 23 | System.out.println("Length of an array is : "+ Numbers.length); 24 | sc.close(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /13. ARRAYS/p3_Arrays_as_Function_Arguments.java: -------------------------------------------------------------------------------- 1 | public class p3_Arrays_as_Function_Arguments { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void update(int array[]) { 6 | 7 | for (int i = 0; array.length > i; i++) { 8 | array[i] = array[i] + 1; 9 | } 10 | System.out.println(" In Function.. "); 11 | for (int i = 0; array.length > i; i++) { 12 | System.out.println(array[i]); 13 | } 14 | 15 | } 16 | 17 | public static void main(String args[]) { 18 | 19 | int array[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }; 20 | 21 | update(array); 22 | 23 | System.out.println(" In Main.. "); 24 | for (int i = 0; array.length > i; i++) { 25 | System.out.println(array[i]); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /13. ARRAYS/p4_Linear_search.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public class p4_Linear_search { 6 | 7 | public static void Linear_search(int arr[] ,int key){ 8 | 9 | 10 | boolean issearch = false ; 11 | int value = 0 ; 12 | for(int i = 0 ; arr.length > i ; i++){ 13 | if(key == arr[i]){ 14 | issearch = true ; 15 | value = i ; 16 | } 17 | } 18 | 19 | if(issearch){ 20 | System.out.print("This present in the array.. Index value is : " ); 21 | System.out.println(value); 22 | } 23 | else{ 24 | System.out.println("did't present in the array .."); 25 | } 26 | } 27 | 28 | public static void main(String args[]){ 29 | Scanner sc = new Scanner(System.in); 30 | 31 | int arr[] = {1,2,3,4,5,6,7,8,9,10}; 32 | 33 | Linear_search(arr, 4); 34 | sc.close(); 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /13. ARRAYS/p5_Largest_in_Array.java: -------------------------------------------------------------------------------- 1 | public class p5_Largest_in_Array { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | 6 | public static void Largest_in_Array(int arr[]){ 7 | 8 | int temp = Integer.MIN_VALUE ; 9 | 10 | for(int i = 0 ; arr.length > i ; i++){ 11 | if(arr[i]>=temp){ 12 | temp = arr[i] ; 13 | } 14 | } 15 | 16 | 17 | System.out.println("Largest Number in Arrray is : "+temp); 18 | } 19 | public static void main(String args[]){ 20 | int arr[] = {30,34,24,65,34,89,62,67,32,68,46,34}; 21 | 22 | Largest_in_Array(arr); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /13. ARRAYS/p6_Smallest_in_Array.java: -------------------------------------------------------------------------------- 1 | public class p6_Smallest_in_Array { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void Smallest_in_Array(int arr[]){ 6 | 7 | int temp = Integer.MAX_VALUE ; 8 | 9 | for(int i = 0 ; arr.length > i ; i++){ 10 | if(arr[i] i ; i++){ 19 | System.out.print(arr[i]+", "); 20 | } 21 | 22 | 23 | 24 | } 25 | 26 | public static void main(String args[]){ 27 | int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12}; 28 | 29 | for(int i = 0 ; arr.length > i ; i++){ 30 | System.out.print(arr[i]+", "); 31 | } 32 | System.out.println("\n"); 33 | Reverse_an_Array(arr); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /13. ARRAYS/p9_Pairs_in_Array.java: -------------------------------------------------------------------------------- 1 | public class p9_Pairs_in_Array { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void Pairs_in_Array(int arr[]){ 6 | 7 | int total_pairs = 0 ; 8 | for(int i = 0 ; arr.length > i ; i++){ 9 | 10 | for(int j = i+1 ; arr.length > j ; j++ ){ 11 | total_pairs++; 12 | System.out.print("("+arr[i]+","+arr[j]+")"); 13 | } 14 | 15 | System.out.println(); 16 | } 17 | 18 | System.out.println("Total Parirs is : "+total_pairs); 19 | } 20 | 21 | public static void main(String args[]){ 22 | int arr[] = {1,2,3,4,5,6,7}; 23 | 24 | Pairs_in_Array(arr); 25 | 26 | for(int i = 0 ; arr.length > i ; i++){ 27 | System.out.print(arr[i]+", "); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /14. Basic Sorting Algorithms/Basic Sorting Questions (1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/14. Basic Sorting Algorithms/Basic Sorting Questions (1).pdf -------------------------------------------------------------------------------- /14. Basic Sorting Algorithms/Basic Sorting Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/14. Basic Sorting Algorithms/Basic Sorting Solutions.pdf -------------------------------------------------------------------------------- /14. Basic Sorting Algorithms/p1_Bubble_Sort_Code.java: -------------------------------------------------------------------------------- 1 | public class p1_Bubble_Sort_Code { 2 | 3 | 4 | // this code is writen by Priyanshu Gour 5 | 6 | public static void BubbleSort(int arr[]){ 7 | 8 | for(int turn = 0 ; turn < arr.length-1 ; turn++){ 9 | for(int j = 0 ; j < arr.length-1-turn ; j++){ 10 | if(arr[j] > arr[j+1]){ 11 | // swap 12 | int temp = arr[j]; 13 | arr[j] = arr[j+1]; 14 | arr[j+1] = temp ; 15 | } 16 | } 17 | } 18 | } 19 | 20 | public static void printArray(int arr[]){ 21 | for(int i = 0 ; arr.length > i ; i++){ 22 | System.out.print(arr[i]+" "); 23 | } 24 | System.out.println(); 25 | } 26 | public static void main(String args[]){ 27 | 28 | int bubble[] = { 5,9,4,1,6,8,3,7,2}; 29 | 30 | BubbleSort(bubble); 31 | printArray(bubble); 32 | 33 | } 34 | } -------------------------------------------------------------------------------- /14. Basic Sorting Algorithms/p2_Selection_Sort_Code.java: -------------------------------------------------------------------------------- 1 | public class p2_Selection_Sort_Code { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void selectionSort(int arr[]){ 6 | 7 | int n = arr.length ; 8 | 9 | 10 | for(int i = 0 ; n > i ; i++ ){ 11 | 12 | int min = i ; 13 | for(int j = i+1 ; n > j ; j++){ 14 | if(arr[min] > arr[j]){ 15 | min = j ; 16 | } 17 | } 18 | 19 | int temp = arr[i]; 20 | arr[i] = arr[min]; 21 | arr[min] = temp; 22 | } 23 | } 24 | 25 | public static void printArray(int arr[]){ 26 | for(int i = 0 ; arr.length > i ; i++){ 27 | System.out.print(arr[i]+" "); 28 | } 29 | System.out.println(); 30 | } 31 | public static void main(String args[]){ 32 | 33 | int arr[] = { 5,9,4,1,6,8,3,7,2}; 34 | 35 | selectionSort(arr); 36 | printArray(arr); 37 | 38 | } 39 | } -------------------------------------------------------------------------------- /14. Basic Sorting Algorithms/p3_Insertion_Sort_code.java: -------------------------------------------------------------------------------- 1 | public class p3_Insertion_Sort_code { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void InsertionSort(int arr[]){ 6 | 7 | int n = arr.length ; 8 | 9 | for(int i = 1 ; n > i ; i++){ 10 | int temp = arr[i]; 11 | int j = i-1 ; 12 | 13 | while(j >= 0 && arr[j] > temp){ 14 | arr[j+1] = arr[j]; 15 | j--; 16 | } 17 | arr[j+1] = temp ; 18 | } 19 | } 20 | 21 | public static void printArray(int arr[]){ 22 | 23 | for(int i = 0 ; arr.length > i ; i++){ 24 | System.out.print(arr[i]+ " "); 25 | } 26 | System.out.println(); 27 | } 28 | 29 | public static void main(String args[]){ 30 | int arr[] = {3,2,5,4,1}; 31 | 32 | InsertionSort(arr); 33 | printArray(arr); 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /14. Basic Sorting Algorithms/p4_Inbuilt_Sort_code.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p4_Inbuilt_Sort_code { 4 | 5 | // this code is writen by Priyanshu Gour 6 | 7 | public static void printArray(Integer arr[]){ 8 | 9 | for(Integer i = 0 ; arr.length > i ; i++){ 10 | System.out.print(arr[i]+ " "); 11 | } 12 | System.out.println(); 13 | } 14 | 15 | public static void main(String args[]){ 16 | Integer arr[] = {3,2,5,4,1,6,7}; // we will using premitive Datatypes because Collections.reverseOrder() Was working on premitive Datatypes... 17 | 18 | // both twos are Sorting in accending Order.. 19 | Arrays.sort(arr); 20 | printArray(arr); 21 | Arrays.sort(arr,1,5); // this was not proper working because in runing type array was sorted you will run then first sort is comment then run ... 22 | printArray(arr); 23 | 24 | 25 | // both twos are Sorting in Decending Order.. 26 | Arrays.sort(arr,Collections.reverseOrder()); 27 | printArray(arr); 28 | Arrays.sort(arr,1,5,Collections.reverseOrder()); // this was not proper working because in runing type array was sorted you will run then first sort is comment then run ... 29 | printArray(arr); 30 | 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /14. Basic Sorting Algorithms/p5_Counting_Sort_Code.java: -------------------------------------------------------------------------------- 1 | public class p5_Counting_Sort_Code { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void countSort(int arr[]){ 6 | int max = Integer.MIN_VALUE ; 7 | 8 | // for find max of value in arr.. 9 | for(int i = 0 ; arr.length > i ; i++){ 10 | max = Math.max(max, arr[i]); 11 | } 12 | 13 | // for initilization Array.. 14 | int count[] = new int[max + 1]; 15 | 16 | // for giving Zero value for each.. 17 | for(int i = 0 ; count.length > i ; i++){ 18 | count[i] = 0 ; 19 | } 20 | 21 | // put value on count array.. 22 | for(int i = 0 ; arr.length > i ; i++){ 23 | count[arr[i]]++; 24 | } 25 | 26 | int j = 0 ; 27 | for(int i = 0 ; count.length > i ; i++){ 28 | while(count[i] > 0){ 29 | arr[j] = i ; 30 | j++; 31 | count[i]--; 32 | } 33 | } 34 | 35 | } 36 | 37 | 38 | 39 | public static void printArray(int arr[]){ 40 | for(int i = 0 ; arr.length > i ; i++){ 41 | System.out.print(arr[i]+" "); 42 | } 43 | System.out.println(); 44 | } 45 | 46 | 47 | 48 | public static void main(String args[]){ 49 | 50 | int arr[] = {5,3,7,6,3,5,4,6,3,6,8,2}; 51 | 52 | countSort(arr); 53 | printArray(arr); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /14. Basic Sorting Algorithms/p6_Descending_Bubble_Sort_Code.java: -------------------------------------------------------------------------------- 1 | public class p6_Descending_Bubble_Sort_Code { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void incBubbleSort(int arr[]){ 6 | int n = arr.length ; 7 | 8 | for(int i = 0 ; n-1 > i ; i++){ 9 | for(int j = 0 ; n-1-i > j ; j++){ 10 | if(arr[j+1] < arr[j]){ 11 | 12 | // Swap.. 13 | 14 | int temp = arr[j]; 15 | arr[j] = arr[j+1]; 16 | arr[j+1] = temp; 17 | } 18 | } 19 | } 20 | printArray(arr); 21 | } 22 | 23 | public static void decBubbleSort(int arr[]){ 24 | 25 | int n = arr.length ; 26 | 27 | for(int i = 0 ; n-1 > i ; i++){ 28 | for(int j = 0 ; n-1-i > j ; j++){ 29 | 30 | if(arr[j] < arr[j+1]){ 31 | // Swap ; 32 | 33 | int temp = arr[j]; 34 | arr[j] = arr[j+1]; 35 | arr[j+1] = temp ; 36 | } 37 | } 38 | } 39 | printArray(arr); 40 | } 41 | 42 | public static void printArray(int arr[]){ 43 | for(int i = 0 ; arr.length > i ; i++){ 44 | System.out.print(arr[i]+" "); 45 | } 46 | System.out.println(); 47 | } 48 | 49 | public static void main(String args[]){ 50 | int arr[] = {3,6,2,1,8,7,4,5,3,1}; 51 | 52 | incBubbleSort(arr); 53 | decBubbleSort(arr); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /14. Basic Sorting Algorithms/p7_Descending_Selection_Sort_code.java: -------------------------------------------------------------------------------- 1 | public class p7_Descending_Selection_Sort_code { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void SelectionSort(int arr[]){ 6 | for(int i = 0 ; arr.length > i ; i++){ 7 | 8 | int min = i ; 9 | 10 | for(int j = i+1 ; arr.length > j ; j++){ 11 | if(arr[min] > arr[j]){ 12 | min = j ; 13 | } 14 | } 15 | 16 | // Swap ... 17 | int temp = arr[i]; 18 | arr[i] = arr[min]; 19 | arr[min] = temp ; 20 | } 21 | printArray(arr); 22 | } 23 | 24 | public static void decSelectionSort(int arr[]){ 25 | for(int i = 0 ; arr.length > i ; i++){ 26 | 27 | int max = i ; 28 | 29 | for(int j = i+1 ; arr.length > j ; j++){ 30 | if(arr[max] < arr[j]){ 31 | max = j ; 32 | } 33 | } 34 | 35 | // Swap ..... 36 | int temp = arr[i]; 37 | arr[i] = arr[max]; 38 | arr[max] = temp ; 39 | } 40 | printArray(arr); 41 | } 42 | 43 | 44 | public static void printArray(int arr[]){ 45 | for(int i = 0 ; arr.length > i ; i++){ 46 | System.out.print(arr[i]+" "); 47 | } 48 | System.out.println(); 49 | } 50 | 51 | public static void main(String[] args) { 52 | int arr[] = {3,6,2,1,8,7,4,5,3,1}; 53 | 54 | 55 | SelectionSort(arr); 56 | decSelectionSort(arr); 57 | } 58 | } -------------------------------------------------------------------------------- /14. Basic Sorting Algorithms/p8_Descending_Insertion_Sort_Code.java: -------------------------------------------------------------------------------- 1 | public class p8_Descending_Insertion_Sort_Code { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void insertionSort(int arr[]){ 6 | 7 | for(int i = 1 ; arr.length > i ; i++){ 8 | int temp = arr[i]; 9 | int j = i -1 ; 10 | 11 | while(j >= 0 && arr[j] > temp){ 12 | arr[j+1] = arr[j]; 13 | j--; 14 | } 15 | arr[j+1] = temp; 16 | } 17 | printArray(arr); 18 | } 19 | 20 | 21 | public static void decInsertionSort(int arr[]){ 22 | 23 | for(int i = 1 ; arr.length > i ; i++){ 24 | int temp = arr[i]; 25 | int j = i - 1 ; 26 | 27 | while(j >= 0 && arr[j] < temp){ 28 | arr[j + 1] = arr[j]; 29 | j-- ; 30 | } 31 | arr[j+1] = temp ; 32 | } 33 | printArray(arr); 34 | } 35 | 36 | 37 | public static void printArray(int arr[]){ 38 | for(int i = 0 ; arr.length > i ; i++){ 39 | System.out.print(arr[i] + " "); 40 | } 41 | System.out.println(); 42 | } 43 | 44 | 45 | public static void main(String args[]){ 46 | 47 | int arr[] = {3,6,2,1,8,7,4,5,3,1}; 48 | 49 | insertionSort(arr); 50 | decInsertionSort(arr); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /14. Basic Sorting Algorithms/p9_Descending_Counting_Sort_Code.java: -------------------------------------------------------------------------------- 1 | public class p9_Descending_Counting_Sort_Code { 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public static void countingSort(int arr[]){ 6 | 7 | int largest = Integer.MIN_VALUE ; 8 | 9 | for(int i = 0 ; arr.length > i ; i++){ 10 | largest = Math.max(largest , arr[i]); 11 | } 12 | 13 | int Aux[] = new int[largest + 1]; 14 | 15 | for(int i = 0 ; Aux.length > i ; i++){ 16 | Aux[i] = 0 ; 17 | } 18 | 19 | for(int i = 0 ; arr.length > i ; i++){ 20 | Aux[arr[i]]++; 21 | } 22 | 23 | int j = 0 ; 24 | for(int i = 0 ; Aux.length > i ; i++){ 25 | 26 | while(Aux[i] > 0){ 27 | arr[j] = i ; 28 | j++; 29 | Aux[i]--; 30 | } 31 | } 32 | printArray(arr); 33 | } 34 | 35 | 36 | public static void decCountingSort(int arr[]){ 37 | 38 | int largest = Integer.MIN_VALUE ; 39 | 40 | for(int i = 0 ; arr.length > i ; i++){ 41 | largest = Math.max(largest, arr[i]); 42 | } 43 | 44 | int Aux[] = new int[largest + 1]; 45 | 46 | for(int i = 0 ; Aux.length > i ; i++){ 47 | Aux[i] = 0; 48 | } 49 | 50 | for(int i = 0 ; arr.length > i ; i++){ 51 | Aux[arr[i]]++; 52 | } 53 | 54 | int j = arr.length-1 ; 55 | 56 | for(int i = 0 ; Aux.length > i ; i++){ 57 | 58 | while(Aux[i] > 0 ) { 59 | arr[j] = i ; 60 | j--; 61 | Aux[i]--; 62 | } 63 | } 64 | 65 | printArray(arr); 66 | } 67 | 68 | 69 | public static void printArray(int [] arr){ 70 | for(int i = 0 ; arr.length > i ; i++){ 71 | System.out.print(arr[i] + " "); 72 | } 73 | System.out.println(); 74 | } 75 | 76 | 77 | public static void main(String args[]){ 78 | 79 | int arr[] = {3,6,2,1,8,7,4,5,3,1}; 80 | 81 | countingSort(arr); 82 | decCountingSort(arr); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /15. 2-D Arrays/2D Arrays Questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/15. 2-D Arrays/2D Arrays Questions.pdf -------------------------------------------------------------------------------- /15. 2-D Arrays/2D Arrays Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/15. 2-D Arrays/2D Arrays Solutions.pdf -------------------------------------------------------------------------------- /15. 2-D Arrays/p10_Transpose_of_Matrix.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p10_Transpose_of_Matrix { 4 | 5 | public static void matrixTranspose(int matrix[][]){ 6 | 7 | int arr[][] = new int [matrix.length][matrix[0].length] ; 8 | 9 | for(int i = 0 ; matrix.length > i ; i++){ 10 | for(int j = 0 ; matrix[i].length > j ; j++){ 11 | 12 | arr [j][i] = matrix[i][j]; 13 | 14 | } 15 | } 16 | 17 | printMatrix(arr); 18 | } 19 | 20 | public static void printMatrix(int arr[][]){ 21 | for(int i = 0 ; arr.length > i ; i++){ 22 | for(int j = 0 ; arr[i].length > j ; j++){ 23 | System.out.print(arr[i][j] + " "); 24 | } 25 | System.out.println(); 26 | } 27 | } 28 | 29 | public static void main(String[] args) { 30 | Scanner sc = new Scanner(System.in); 31 | 32 | int matrix[][] = { 33 | {10,20,30,40}, 34 | {15,10,20,40}, 35 | {10,40,15,30}, 36 | {30,10,20,15}, 37 | }; 38 | 39 | matrixTranspose(matrix); 40 | 41 | sc.close(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /15. 2-D Arrays/p11_CountNegativeNumbersInSorted2DArray.java: -------------------------------------------------------------------------------- 1 | 2 | public class p11_CountNegativeNumbersInSorted2DArray { 3 | public static int countNegatives(int[][] grid) { 4 | int rows = grid.length; 5 | int cols = grid[0].length; 6 | int count = 0; 7 | 8 | int row = rows - 1; // Start from the last row 9 | int col = 0; 10 | 11 | while (row >= 0 && col < cols) { 12 | if (grid[row][col] < 0) { 13 | // All elements to the right of the current element in this row are also 14 | // negative 15 | count += cols - col; 16 | row--; // Move to the previous row 17 | } else { 18 | col++; // Move to the next column 19 | } 20 | } 21 | 22 | return count; 23 | } 24 | 25 | public static void main(String[] args) { 26 | int[][] grid = { 27 | { -4, -3, -1, 1 }, 28 | { -2, -2, 1, 2 }, 29 | { -1, 0, 2, 3 }, 30 | { 1, 2, 4, 5 } 31 | }; 32 | 33 | int count = countNegatives(grid); 34 | System.out.println("Number of negative numbers in the sorted 2D array: " + count); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /15. 2-D Arrays/p12_MaxElementIn2DArray.java: -------------------------------------------------------------------------------- 1 | 2 | public class p12_MaxElementIn2DArray { 3 | public static void main(String[] args) { 4 | int[][] arr = { 5 | { 10, 5, 9 }, 6 | { 8, 3, 7 }, 7 | { 2, 6, 4 } 8 | }; 9 | 10 | int maxElement = findMaxElement(arr); 11 | 12 | System.out.println("The maximum element in the 2D array is: " + maxElement); 13 | } 14 | 15 | public static int findMaxElement(int[][] arr) { 16 | int maxElement = arr[0][0]; // Assuming the array is not empty 17 | 18 | for (int i = 0; i < arr.length; i++) { 19 | for (int j = 0; j < arr[i].length; j++) { 20 | if (arr[i][j] > maxElement) { 21 | maxElement = arr[i][j]; 22 | } 23 | } 24 | } 25 | 26 | return maxElement; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /15. 2-D Arrays/p13_2DArray.java: -------------------------------------------------------------------------------- 1 | public class p13_2DArray { 2 | public static void main(String[] args) { 3 | int[][] a = new int[3][4]; 4 | System.out.println(a);// [[I@... 5 | System.out.println(a[0]);// [I@.. 6 | System.out.println(a[0][0]);// 0 7 | } 8 | } -------------------------------------------------------------------------------- /15. 2-D Arrays/p1_2D_Array_Declearation_Code.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // Starting of 2D- Array Let's Start 4 | // this code is writen by Priyanshu Gour 5 | 6 | public class p1_2D_Array_Declearation_Code { 7 | 8 | 9 | public static void inputMatrix(int matrix [] []){ 10 | 11 | int n = matrix.length ; 12 | int m = matrix[0].length ; 13 | 14 | Scanner sc = new Scanner(System.in); 15 | 16 | for(int i = 0 ; n > i ; i++){ 17 | 18 | for(int j = 0 ; m > j ; j++){ 19 | 20 | matrix[i][j] = sc.nextInt(); 21 | 22 | } 23 | } 24 | 25 | sc.close(); 26 | } 27 | 28 | 29 | public static void printMatrix(int matrix [][]){ 30 | 31 | int n = matrix.length ; 32 | int m = matrix[0].length ; 33 | 34 | for(int i = 0 ; n > i ; i++ ){ 35 | 36 | for(int j = 0 ; m > j ; j++ ){ 37 | System.out.print(matrix[i][j]+ " "); 38 | } 39 | System.out.println(); 40 | } 41 | } 42 | 43 | 44 | public static void main(String args[]){ 45 | int matrix [][] = new int [2][5]; 46 | 47 | inputMatrix(matrix); 48 | printMatrix(matrix); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /15. 2-D Arrays/p2_Search_key_in_2D_Array_Code.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // this code is writen by Priyanshu Gour 4 | 5 | public class p2_Search_key_in_2D_Array_Code { 6 | 7 | public static void searchMatrix(int arr[][] , int key ) { 8 | 9 | boolean find = false ; 10 | int n = arr.length ; 11 | int m = arr[0].length ; 12 | 13 | for(int i = 0 ; n > i ; i++){ 14 | for(int j = 0 ; m > j ; j++){ 15 | if(key == arr[i][j]){ 16 | find = true ; 17 | } 18 | } 19 | } 20 | 21 | if(find){ 22 | System.out.println("Element Finded .."); 23 | }else{ 24 | System.out.println("Element Not Founded..."); 25 | } 26 | } 27 | 28 | 29 | public static void inputMatrix(int matrix [] []){ 30 | 31 | int n = matrix.length ; 32 | int m = matrix[0].length ; 33 | 34 | Scanner sc = new Scanner(System.in); 35 | 36 | for(int i = 0 ; n > i ; i++){ 37 | 38 | for(int j = 0 ; m > j ; j++){ 39 | 40 | matrix[i][j] = sc.nextInt(); 41 | 42 | } 43 | } 44 | 45 | sc.close(); 46 | } 47 | 48 | 49 | public static void main(String args[]){ 50 | 51 | int arr[][] = new int [3][3]; 52 | 53 | int key = 5 ; 54 | 55 | inputMatrix(arr); 56 | searchMatrix(arr,key); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /15. 2-D Arrays/p3_Spiral_Matrix_Code.java: -------------------------------------------------------------------------------- 1 | public class p3_Spiral_Matrix_Code { 2 | 3 | // this code is written by Priyanshu Gour 4 | 5 | public static void spiralMatrix(int matrix [][]){ 6 | 7 | int startRow = 0 ; 8 | int startCol = 0 ; 9 | int endRow = matrix.length-1 ; 10 | int endCol = matrix.length-1 ; 11 | 12 | 13 | while(startRow <= endRow && startCol <= endCol){ 14 | 15 | // Top 16 | for(int j = startCol ; j <= endCol ;j++){ 17 | System.out.print(matrix[startRow][j] + " "); 18 | } 19 | 20 | // Right 21 | for(int i = startRow + 1 ; i <= endRow ; i++){ 22 | System.out.print(matrix[i][endCol] + " "); 23 | } 24 | 25 | // Bottom 26 | for(int j = endCol -1 ; j >= startCol ; j--){ 27 | System.out.print(matrix[endRow][j] + " "); 28 | } 29 | 30 | // Left 31 | for(int i = endRow-1 ; i >= startRow +1 ;i--){ 32 | System.out.print(matrix[i][startCol] + " "); 33 | } 34 | 35 | startCol++ ; 36 | startRow++ ; 37 | endCol-- ; 38 | endRow-- ; 39 | } 40 | } 41 | 42 | 43 | public static void main(String args[]){ 44 | int matrix[][] = {{1,2,3,4}, 45 | {5,6,7,8}, 46 | {9,10,11,12}, 47 | {13,14,15,16}}; 48 | 49 | spiralMatrix(matrix); 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /15. 2-D Arrays/p4_Diagonal_Sum_Brute_force.java: -------------------------------------------------------------------------------- 1 | public class p4_Diagonal_Sum_Brute_force { 2 | 3 | // this code is written by Priyanshu Gour 4 | 5 | public static void diagonalSum(int arr[][]){ 6 | 7 | int sumRight = 0 , sumLeft = 0 ; 8 | 9 | for(int i = 0 ; arr.length > i ; i++){ 10 | 11 | for(int j = 0 ; arr[i].length > j ; j++ ){ 12 | 13 | if(i == j ){ 14 | sumRight += arr[i][j] ; 15 | } 16 | if(i + j == arr.length-1 && i != j){ 17 | sumLeft += arr[i][j] ; 18 | } 19 | 20 | } 21 | } 22 | 23 | System.out.println("Right Sum is :- "+sumRight); 24 | System.out.println("Left Sum is :- "+ sumLeft); 25 | } 26 | 27 | 28 | public static void main(String[] args) { 29 | 30 | int matrix[][] = {{1,2,3,4}, 31 | {5,6,7,8}, 32 | {9,10,11,12}, 33 | {13,14,15,16}}; 34 | 35 | diagonalSum(matrix); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /15. 2-D Arrays/p5_Diagonal_Sum_Best_force.java: -------------------------------------------------------------------------------- 1 | public class p5_Diagonal_Sum_Best_force { 2 | 3 | // this code is written by Priyanshu Gour 4 | 5 | public static void diagonalSum(int arr[][]){ 6 | 7 | int sumRight = 0 , sumLeft = 0 ; 8 | 9 | for(int i = 0 ; i < arr.length ; i++){ 10 | sumRight += arr[i][i] ; 11 | 12 | if(i != arr.length - 1 - i){ 13 | sumLeft += arr[i][arr.length - i - 1] ; 14 | } 15 | } 16 | 17 | System.out.println(sumRight + sumLeft); 18 | } 19 | 20 | 21 | public static void main(String[] args) { 22 | 23 | int matrix[][] = {{1,2,3,4}, 24 | {5,6,7,8}, 25 | {9,10,11,12}, 26 | {13,14,15,16}}; 27 | 28 | diagonalSum(matrix); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /15. 2-D Arrays/p6_Search_in_Sorted_Matrix_Brute_force.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p6_Search_in_Sorted_Matrix_Brute_force { 4 | 5 | // this code is written by Priyanshu Gour 6 | 7 | public static void searchInSortedMatrix(int arr[][], int key){ 8 | 9 | for(int i = 0 ; arr.length > i ; i++){ 10 | for(int j = 0 ; arr[i].length > j ; j++){ 11 | 12 | if(key == arr[i][j]){ 13 | System.out.println("(" + i + "," + j + ")"); 14 | } 15 | 16 | } 17 | } 18 | 19 | } 20 | 21 | public static void main(String[] args) { 22 | Scanner sc = new Scanner(System.in); 23 | 24 | int matrix[][] = { 25 | {10,20,30,40}, 26 | {15,25,35,45}, 27 | {27,29,37,48}, 28 | {32,33,39,50}, 29 | }; 30 | System.out.print( "Enter the Key to Search in Sorted Matrix :- "); 31 | int key = sc.nextInt(); 32 | 33 | searchInSortedMatrix(matrix, key); 34 | 35 | 36 | sc.close(); 37 | } 38 | } -------------------------------------------------------------------------------- /15. 2-D Arrays/p7_Search_in_Sorted_Matrix_Best_force.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p7_Search_in_Sorted_Matrix_Best_force { 4 | 5 | // this code is written by Priyanshu Gour 6 | 7 | public static boolean searchInSortedMatrix(int matrix[][], int key){ 8 | 9 | int row = 0 ; 10 | int col = matrix[0].length - 1 ; 11 | 12 | while(row < matrix.length && col >= 0){ 13 | if(matrix[row][col] == key){ 14 | System.out.println("Found Key at :- (" + row + "," + col + ")"); 15 | return true ; 16 | } 17 | else if(key < matrix[row][col]) { 18 | col-- ; 19 | } 20 | else{ 21 | row++ ; 22 | } 23 | } 24 | 25 | System.out.println("key not Founded ... ! "); 26 | return false ; 27 | 28 | 29 | } 30 | 31 | public static void main(String[] args) { 32 | Scanner sc = new Scanner(System.in); 33 | 34 | int matrix[][] = { 35 | {10,20,30,40}, 36 | {15,25,35,45}, 37 | {27,29,37,48}, 38 | {32,33,39,50}, 39 | }; 40 | System.out.print( "Enter the Key to Search in Sorted Matrix :- "); 41 | int key = sc.nextInt(); 42 | 43 | searchInSortedMatrix(matrix, key); 44 | 45 | 46 | sc.close(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /15. 2-D Arrays/p8_num_how_many_time_present_in_array.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p8_num_how_many_time_present_in_array { 4 | 5 | public static void checkHowManyTime(int arr[][], int num){ 6 | 7 | int check = 0 ; 8 | 9 | for(int i = 0 ; arr.length > i ; i++){ 10 | for(int j = 0 ; arr.length > j ; j++){ 11 | if(arr[i][j] == num ){ 12 | check ++ ; 13 | } 14 | } 15 | } 16 | 17 | System.out.println(num + " Presten in "+ check + " Time's .. !"); 18 | 19 | } 20 | 21 | public static void main(String[] args) { 22 | Scanner sc = new Scanner(System.in); 23 | 24 | int matrix[][] = { 25 | {10,20,30,40}, 26 | {15,10,20,40}, 27 | {10,40,15,30}, 28 | {30,10,20,15}, 29 | }; 30 | System.out.print( "Enter the Number to Check how many time in the Matrix :- "); 31 | int key = sc.nextInt(); 32 | 33 | checkHowManyTime(matrix, key); 34 | 35 | sc.close(); 36 | } 37 | } -------------------------------------------------------------------------------- /15. 2-D Arrays/p9_Sum_of_nth_row_in_2DArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p9_Sum_of_nth_row_in_2DArray { 4 | 5 | public static void sumOfNthRow(int arr[][], int row){ 6 | 7 | int sum = 0 ; 8 | 9 | for(int i = 0 ; arr.length > i ; i++){ 10 | for(int j = 0 ; arr.length > j ; j++){ 11 | 12 | if(i+1 == row ){ 13 | sum += arr[i][j]; 14 | } 15 | 16 | } 17 | } 18 | 19 | System.out.println("Sum of " +row+ "th Row is "+ sum ); 20 | 21 | } 22 | 23 | public static void main(String[] args) { 24 | Scanner sc = new Scanner(System.in); 25 | 26 | int matrix[][] = { 27 | {10,20,30,40}, 28 | {15,10,20,40}, 29 | {10,40,15,30}, 30 | {30,10,20,15}, 31 | }; 32 | 33 | System.out.print( "Enter the Row Number to check Sum of Nth Row :- "); 34 | int key = sc.nextInt(); 35 | 36 | sumOfNthRow(matrix, key); 37 | 38 | sc.close(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /16. Strings/StringQuestion.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/16. Strings/StringQuestion.pdf -------------------------------------------------------------------------------- /16. Strings/StringSolution.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/16. Strings/StringSolution.pdf -------------------------------------------------------------------------------- /16. Strings/p10_largest_String_using_lexicographic.java: -------------------------------------------------------------------------------- 1 | public class p10_largest_String_using_lexicographic { 2 | 3 | public static void largestString(String str []){ 4 | 5 | String largest = str[0]; 6 | 7 | for(int i = 0 ; i < str.length ; i++){ 8 | if(largest.compareToIgnoreCase(str[i]) < 0){ 9 | largest = str[i] ; 10 | } 11 | } 12 | 13 | System.out.println(largest); 14 | } 15 | 16 | 17 | public static void main(String[] args) { 18 | 19 | String str1 = "Priyanshu" ; 20 | String str2 = "PriyanshU" ; 21 | 22 | // str1.compareTo(str2) ; 23 | // 'A' & 'a' both are different 24 | // 0 : equal 25 | // <0 : -ve str1 < str2 26 | // >0 : -ve str1 > str2 27 | 28 | 29 | 30 | // str1.compareToIgnoreCase(str2) ; 31 | // 'A' & 'a' both are Same 32 | // 0 : equal 33 | // <0 : -ve str1 < str2 34 | // >0 : -ve str1 > str2 35 | 36 | System.out.println(str1.compareTo(str2)); 37 | System.out.println(str1.compareToIgnoreCase(str2)); 38 | 39 | String fruits [] = {"Apple", "Banana", "Mango", "Gauva"}; 40 | 41 | largestString(fruits); 42 | 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /16. Strings/p11_StringBuilder_mutable_String.java: -------------------------------------------------------------------------------- 1 | public class p11_StringBuilder_mutable_String { 2 | 3 | public static void main(String[] args) { 4 | 5 | StringBuilder sb = new StringBuilder("hey buddy "); 6 | 7 | sb.append("Priyanshu ") ; 8 | sb.append("Gour"); 9 | 10 | System.out.println(sb); 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /16. Strings/p12_first_letter_uppercase.java: -------------------------------------------------------------------------------- 1 | public class p12_first_letter_uppercase { 2 | 3 | public static String toUpperCase(String str){ 4 | 5 | StringBuilder sb = new StringBuilder(""); 6 | 7 | char ch = Character.toUpperCase(str.charAt(0)) ; 8 | sb.append(ch); 9 | 10 | for(int i = 1 ; i < str.length(); i++){ 11 | if(str.charAt(i) == ' ' && i < str.length()-1){ 12 | sb.append(str.charAt(i)); 13 | i++ ; 14 | sb.append(Character.toUpperCase(str.charAt(i))); 15 | }else{ 16 | sb.append(str.charAt(i)); 17 | } 18 | } 19 | 20 | return sb.toString() ; 21 | } 22 | 23 | public static void main(String[] args) { 24 | 25 | String str = "hey i am devil .. "; 26 | 27 | str = toUpperCase(str); 28 | 29 | System.out.println(str); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /16. Strings/p13_String_Compression.java: -------------------------------------------------------------------------------- 1 | public class p13_String_Compression { 2 | 3 | public static String strCompression(String str) { 4 | if (str == null || str.isEmpty()) { 5 | return str; 6 | } 7 | 8 | StringBuilder newStr = new StringBuilder(); 9 | 10 | for (int i = 0; i < str.length(); i++) { 11 | char currentChar = str.charAt(i); 12 | int count = 1; 13 | 14 | while (i < str.length() - 1 && str.charAt(i) == str.charAt(i + 1)) { 15 | count++; 16 | i++; 17 | } 18 | 19 | newStr.append(currentChar); 20 | 21 | if (count > 1) { 22 | newStr.append(count); 23 | } 24 | } 25 | 26 | return newStr.toString(); 27 | } 28 | 29 | public static void main(String[] args) { 30 | String str = "aaabbbcccdde"; 31 | String strComp = strCompression(str); 32 | System.out.println(strComp); // Output: a3b3c3de2 33 | } 34 | } -------------------------------------------------------------------------------- /16. Strings/p14_Count_lowercased_vowels.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class p14_Count_lowercased_vowels { 4 | 5 | public static int countLowercaseVowels(String str) { 6 | int count = 0 ; 7 | for(int i = 0 ; str.length() > i ; i++){ 8 | if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u'){ 9 | count++ ; 10 | } 11 | } 12 | 13 | return count ; 14 | } 15 | 16 | public static void main(String[] args) { 17 | Scanner sc = new Scanner(System.in) ; 18 | 19 | String str = sc.nextLine() ; 20 | 21 | int vowels = countLowercaseVowels(str); 22 | 23 | System.out.println(vowels); 24 | 25 | sc.close(); 26 | } 27 | } -------------------------------------------------------------------------------- /16. Strings/p15_String_Anagrams.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class p15_String_Anagrams { 4 | 5 | public static boolean checkAnagrams(String str1 , String str2) { 6 | 7 | str1 = str1.toLowerCase() ; 8 | str2 = str2.toLowerCase() ; 9 | 10 | str1 = str1.replaceAll(" ", "") ; 11 | str2 = str2.replaceAll(" ", "") ; 12 | 13 | char arrstr1 [] = str1.toCharArray() ; 14 | char arrstr2 [] = str2.toCharArray() ; 15 | 16 | Arrays.sort(arrstr1); 17 | Arrays.sort(arrstr2); 18 | 19 | if(arrstr1.length != arrstr2.length){ 20 | return false ; 21 | } 22 | for(int i = 0 ; arrstr1.length > i ; i++){ 23 | 24 | if(arrstr1[i] != arrstr2[i]){ 25 | return false ; 26 | } 27 | } 28 | 29 | 30 | return true; 31 | } 32 | 33 | public static void main(String[] args) { 34 | 35 | String str1 = "Priyanshu Gour" ; 36 | String str2 = "uh sy a ni rP oU rg" ; 37 | 38 | if(checkAnagrams(str1, str2)){ 39 | System.out.println("String is Anagram ..!"); 40 | }else{ 41 | System.out.println("String is Not Anagram ..!"); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /16. Strings/p16_firstNonRepeatedChar.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedHashMap; 2 | import java.util.Map; 3 | 4 | public class p16_firstNonRepeatedChar { 5 | public static void main(String[] args) { 6 | String str = "hello world"; 7 | char firstNonRepeatedChar = findFirstNonRepeatedChar(str); 8 | System.out.println("The first non-repeated character in \"" + str + "\" is: " + firstNonRepeatedChar); 9 | } 10 | 11 | public static char findFirstNonRepeatedChar(String str) { 12 | Map charCount = new LinkedHashMap<>(); // Using LinkedHashMap to maintain insertion order 13 | for (int i = 0; i < str.length(); i++) { 14 | char c = str.charAt(i); 15 | charCount.put(c, charCount.getOrDefault(c, 0) + 1); // Increment character count in the map 16 | } 17 | for (Map.Entry entry : charCount.entrySet()) { 18 | if (entry.getValue() == 1) { // Check for non-repeated character 19 | return entry.getKey(); 20 | } 21 | } 22 | return '\0'; // Return null character if no non-repeated character is found 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /16. Strings/p1_Introduction_String.java: -------------------------------------------------------------------------------- 1 | public class p1_Introduction_String { 2 | 3 | public static void main(String[] args) { 4 | 5 | char arr[] = {'a', 'b', 'c', 'd'}; 6 | String str = "Priyanshu Gour" ; 7 | String str2 = new String("Priyanshu Gour"); 8 | 9 | System.out.println(arr[0]); 10 | System.out.println(str); 11 | System.out.println(str2); 12 | } 13 | } -------------------------------------------------------------------------------- /16. Strings/p2_String_Input_Output.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p2_String_Input_Output { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | System.out.print("Enter your Name :- "); 9 | String name = sc.next(); 10 | System.out.println(name); 11 | 12 | System.out.print("Enter Your Full Name :- "); 13 | String fullName = sc.nextLine(); 14 | System.out.println(fullName); 15 | 16 | 17 | 18 | sc.close(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /16. Strings/p3_String_length_Method.java: -------------------------------------------------------------------------------- 1 | public class p3_String_length_Method { 2 | 3 | public static void main(String[] args) { 4 | 5 | String fullName = "Priyanshu Gour" ; 6 | 7 | int len = fullName.length(); 8 | 9 | System.out.println(len); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /16. Strings/p4_String_Concatenation.java: -------------------------------------------------------------------------------- 1 | public class p4_String_Concatenation { 2 | 3 | public static void main(String[] args) { 4 | String firstName = "Priyanshu" ; 5 | String lastName = "Gour" ; 6 | String fullName = firstName +" "+ lastName ; 7 | 8 | System.out.println(fullName); 9 | 10 | String fullName1 = firstName.concat(lastName); 11 | 12 | System.out.println(fullName1); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /16. Strings/p5_String_charAt.java: -------------------------------------------------------------------------------- 1 | public class p5_String_charAt { 2 | 3 | public static void printLetters(String str){ 4 | 5 | for(int i = 0 ; str.length() > i ; i++){ 6 | System.out.print(str.charAt(i) + " "); 7 | } 8 | System.out.println(); 9 | } 10 | 11 | public static void main(String[] args) { 12 | 13 | String name = "Priyanshu Gour" ; 14 | System.out.println(name.charAt(10)); 15 | 16 | printLetters(name); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /16. Strings/p6_Palindrome_String.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p6_Palindrome_String { 4 | 5 | public static boolean checkPalindrome(String str){ 6 | 7 | for(int i = 0 ; str.length()/2 > i ; i++){ 8 | if(str.charAt(i) != str.charAt(str.length()-1-i)){ 9 | return false ; 10 | } 11 | } 12 | 13 | return true ; 14 | } 15 | 16 | public static void main(String[] args) { 17 | Scanner sc = new Scanner(System.in); 18 | 19 | String str = sc.nextLine(); 20 | 21 | if(checkPalindrome(str)){ 22 | System.out.println("String is Palindrome .."); 23 | }else{ 24 | System.out.println("String is Not Palindrome .."); 25 | } 26 | 27 | sc.close(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /16. Strings/p7_Shortest_path_to_reach_destination.java: -------------------------------------------------------------------------------- 1 | public class p7_Shortest_path_to_reach_destination { 2 | 3 | public static float sortestPath(String str){ 4 | int x = 0 , y = 0 ; 5 | 6 | for(int i = 0 ; i < str.length() ; i++){ 7 | if(str.charAt(i) == 'W'){ 8 | x-- ; 9 | }else if(str.charAt(i) == 'N') { 10 | y++ ; 11 | }else if(str.charAt(i) == 'S'){ 12 | y-- ; 13 | }else{ 14 | x++ ; 15 | } 16 | } 17 | 18 | int x2 = x*x ; 19 | int y2 = y*y ; 20 | 21 | return (float)Math.sqrt(x2 + y2) ; 22 | } 23 | 24 | public static void main(String[] args) { 25 | 26 | String path = "WNEENESENNN" ; 27 | 28 | float dist = sortestPath(path); 29 | 30 | System.out.println(dist); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /16. Strings/p8_String_Compare_Methods.java: -------------------------------------------------------------------------------- 1 | public class p8_String_Compare_Methods { 2 | 3 | public static void main(String[] args) { 4 | 5 | String s1 = "Hey" ; 6 | String s2 = "Hey" ; 7 | String s3 = new String("Hey"); 8 | 9 | if(s1 == s2){ 10 | System.out.println("s1 and s2 are equal"); 11 | }else{ 12 | System.out.println("String are not equal.."); 13 | } 14 | 15 | if(s1 == s3){ 16 | System.out.println("s1 and s3 are equal"); 17 | }else{ 18 | System.out.println("String are not equal.."); 19 | } 20 | 21 | if(s1.equals(s2)){ 22 | System.out.println("s1 and s2 are equal"); 23 | }else{ 24 | System.out.println("String are not equal.."); 25 | } 26 | 27 | if(s1.equals(s3)){ 28 | System.out.println("s1 and s2 are equal"); 29 | }else{ 30 | System.out.println("String are not equal.."); 31 | } 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /16. Strings/p9_Substring_method.java: -------------------------------------------------------------------------------- 1 | public class p9_Substring_method { 2 | 3 | 4 | public static String subString(String str, int m , int n){ 5 | 6 | String strcpy = ""; 7 | 8 | for(int i = 0 ; str.length() > i ; i++){ 9 | if(i >= m && n > i){ 10 | strcpy += str.charAt(i); 11 | } 12 | } 13 | 14 | return strcpy ; 15 | } 16 | public static void main(String[] args) { 17 | 18 | String hey = "priyanshu Gour" ; 19 | 20 | String sub = subString(hey,1,5); 21 | 22 | System.out.println(sub); 23 | System.out.println(hey.substring(5)); 24 | System.out.println(hey.substring(1,5)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /17. Bit Manipulation/Question.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/17. Bit Manipulation/Question.pdf -------------------------------------------------------------------------------- /17. Bit Manipulation/Solution.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/priyanshugour70/Data-Structures-and-Algorithms-in-Java/74fd76afe3cf758863c718d112162434ef1a8b9f/17. Bit Manipulation/Solution.pdf -------------------------------------------------------------------------------- /17. Bit Manipulation/p10_Set_Ith_Bit.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class p10_Set_Ith_Bit { 4 | 5 | public static void main(String[] args) { 6 | 7 | Scanner sc = new Scanner(System.in); 8 | 9 | System.out.print("Enter number to find length ith :- "); 10 | int num = sc.nextInt(); 11 | 12 | System.out.print("Enter get ith bit :- "); 13 | int ith = sc.nextInt(); 14 | 15 | int ithBit = num << ith ; 16 | 17 | int last = num | ithBit ; 18 | 19 | System.out.println(last) ; 20 | sc.close(); 21 | } 22 | } -------------------------------------------------------------------------------- /17. Bit Manipulation/p11_Clear_Ith_Bit.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class p11_Clear_Ith_Bit { 4 | 5 | public static void main(String[] args) { 6 | 7 | Scanner sc = new Scanner(System.in) ; 8 | 9 | System.out.print("Enter Number :- "); 10 | int num = sc.nextInt(); 11 | 12 | System.out.print("Enter which Bit to Clear :- "); 13 | int ith = sc.nextInt(); 14 | 15 | int clrBit = num << ith ; 16 | 17 | clrBit = ~(clrBit) ; 18 | 19 | int finlOutput = num & clrBit ; 20 | 21 | System.out.println(finlOutput); 22 | 23 | 24 | sc.close(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /17. Bit Manipulation/p12_Update_Ith_Bit.java: -------------------------------------------------------------------------------- 1 | public class p12_Update_Ith_Bit { 2 | 3 | public static int setIthBit(int num , int ith){ 4 | 5 | int ithBit = num << ith ; 6 | 7 | return num | ithBit ; 8 | 9 | } 10 | 11 | 12 | public static int clearIthBit(int num , int ith){ 13 | 14 | int clrBit = num << ith ; 15 | 16 | clrBit = ~(clrBit) ; 17 | 18 | return num & clrBit ; 19 | } 20 | 21 | 22 | public static int updateIthBitSimpleMethod(int num , int ith, int newBit){ 23 | 24 | if(newBit == 0){ 25 | return clearIthBit(num, ith); 26 | }else{ 27 | return setIthBit(num, ith); 28 | } 29 | } 30 | 31 | 32 | public static int updateIthBitComplexMethod(int num , int ith, int newBit){ 33 | 34 | num = clearIthBit(num, ith) ; 35 | 36 | int BitMask = newBit << ith ; 37 | 38 | return num | BitMask ; 39 | 40 | } 41 | 42 | public static void main(String[] args) { 43 | 44 | System.out.println(updateIthBitComplexMethod(10, 2, 1)); 45 | System.out.println(updateIthBitSimpleMethod(10, 2, 1)); 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /17. Bit Manipulation/p13_Clear_Last_I_Bits.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class p13_Clear_Last_I_Bits { 4 | 5 | public static void main(String[] args) { 6 | 7 | Scanner sc = new Scanner(System.in) ; 8 | 9 | System.out.print("Enter Number : "); 10 | int num = sc.nextInt(); 11 | 12 | System.out.print("Enter Ith Number : "); 13 | int ith = sc.nextInt(); 14 | 15 | int bitMask = (~0)<> i) & 1; 5 | int bitJ = (num >> j) & 1; 6 | 7 | if (bitI != bitJ) { 8 | // Create masks to isolate the 'i-th' and 'j-th' bits 9 | int maskI = 1 << i; 10 | int maskJ = 1 << j; 11 | 12 | // Use XOR to flip the 'i-th' and 'j-th' bits 13 | num ^= maskI; // Flips the 'i-th' bit (0 to 1 or 1 to 0) 14 | num ^= maskJ; // Flips the 'j-th' bit (0 to 1 or 1 to 0) 15 | } 16 | 17 | return num; 18 | } 19 | 20 | public static void main(String[] args) { 21 | int num = 73; // Example input integer 22 | int i = 1; // Example 'i-th' bit position 23 | int j = 6; // Example 'j-th' bit position 24 | 25 | int result = swapBits(num, i, j); 26 | System.out.println("Result: " + result); // Output: 11 (binary representation 1011) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /17. Bit Manipulation/p19_ReverseBits.java: -------------------------------------------------------------------------------- 1 | public class p19_ReverseBits { 2 | public static int reverseBits(int num) { 3 | int result = 0; 4 | for (int i = 0; i < 32; i++) { 5 | result <<= 1; // Left shift the result to make space for the next bit 6 | result |= num & 1; // Get the least significant bit of num and set it in result 7 | num >>= 1; // Right shift num to get the next bit 8 | } 9 | return result; 10 | } 11 | 12 | public static void main(String[] args) { 13 | int num = 43261596; // Example input, the binary representation is 00000010100101000001111010011100 14 | int reversed = reverseBits(num); 15 | System.out.println("Reversed: " + reversed); // Output: 964176192 (binary representation 16 | // 00111001011110000010100101000000) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /17. Bit Manipulation/p1_Introduction_to_Bitwise_Operator.java: -------------------------------------------------------------------------------- 1 | public class p1_Introduction_to_Bitwise_Operator { 2 | 3 | public static void main(String[] args) { 4 | 5 | // Bit manipulation is the process of manipulating individual bits of binary numbers to achieve some specific result. Java provides several operators and methods that enable programmers to perform bit manipulation easily. 6 | 7 | 8 | // Bitwise AND (&): performs a bitwise AND operation on each bit of two integers, resulting in a new integer where each bit is set to 1 only if the corresponding bits of both integers are also 1. 9 | // Bitwise OR (|): performs a bitwise OR operation on each bit of two integers, resulting in a new integer where each bit is set to 1 if the corresponding bit of either or both integers is 1. 10 | // Bitwise XOR (^): performs a bitwise exclusive OR (XOR) operation on each bit of two integers, resulting in a new integer where each bit is set to 1 only if the corresponding bits of the two integers are different. 11 | // Bitwise Complement (~): performs a bitwise complement operation on an integer, resulting in a new integer with all its bits inverted (0s become 1s and 1s become 0s). 12 | // Signed Left Shift (<<): shifts the bits of an integer to the left by a specified number of positions. The leftmost bits are filled with 0s, and the rightmost bits are discarded. 13 | // Signed Right Shift (>>): shifts the bits of an integer to the right by a specified number of positions. The rightmost bits are filled with the sign bit (the most significant bit), and the leftmost bits are filled with 0s. 14 | 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /17. Bit Manipulation/p2_Binary_AND.java: -------------------------------------------------------------------------------- 1 | public class p2_Binary_AND { 2 | 3 | public static void main(String[] args) { 4 | 5 | int A = 5 ; // 0101 6 | int B = 6 ; // 0110 7 | 8 | // 0 & 0 : 0 9 | // 0 & 1 : 0 10 | // 1 & 0 : 0 11 | // 1 & 1 : 1 12 | 13 | int And = A & B ; // 0101 & 0110 = 0100(4) 14 | 15 | System.out.println(And); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /17. Bit Manipulation/p3_Binary_OR.java: -------------------------------------------------------------------------------- 1 | public class p3_Binary_OR { 2 | 3 | public static void main(String[] args) { 4 | 5 | int A = 5 ; // 0101 6 | int B = 6 ; // 0110 7 | 8 | // 0 | 0 : 0 9 | // 0 | 1 : 1 10 | // 1 | 0 : 1 11 | // 1 | 1 : 1 12 | 13 | int Or = A | B ; // 0101 | 0110 = 0111(7) 14 | 15 | System.out.println(Or); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /17. Bit Manipulation/p4_Binary_XOR.java: -------------------------------------------------------------------------------- 1 | public class p4_Binary_XOR { 2 | 3 | public static void main(String[] args) { 4 | 5 | int A = 5 ; // 0101 6 | int B = 6 ; // 0110 7 | 8 | // 0 ^ 0 : 0 9 | // 0 ^ 1 : 1 10 | // 1 ^ 0 : 1 11 | // 1 ^ 1 : 0 12 | 13 | int XOR = A ^ B ; // 0101 ^ 0110 = 0011(3) 14 | 15 | System.out.println(XOR); 16 | } 17 | } -------------------------------------------------------------------------------- /17. Bit Manipulation/p5_Binary_Ones_Complement.java: -------------------------------------------------------------------------------- 1 | public class p5_Binary_Ones_Complement { 2 | 3 | public static void main(String[] args) { 4 | 5 | int A = 5 ; // 0101 6 | 7 | // ~0 = 1 8 | // ~1 = 0 9 | // This operator work on single variable and it is also known as NOT oprator 10 | 11 | int NOT = ~A ; // ~0101 = 010(2) 12 | 13 | // 5 = 00000101 => 11111010 => (11111010(one complement) = 00000101) => (00000101 +1 = 00000110)(it is known as 6 but we find using twos complement thats way the nubmer is negitive) => -6 14 | 15 | System.out.println(NOT); 16 | } 17 | } -------------------------------------------------------------------------------- /17. Bit Manipulation/p6_Binary_Left_Shift.java: -------------------------------------------------------------------------------- 1 | public class p6_Binary_Left_Shift { 2 | 3 | public static void main(String[] args) { 4 | 5 | int A = 5 ; // 000101 6 | int b = 2 ; 7 | 8 | // A << b ; (5 << 2 = 000101 << 2(All values are shifted left with 2 time and the last bits fill by zeros) => 010100 ) 9 | 10 | int LeftShift = A << b ; // 00000101 = 00010100(20) 11 | // A << b ; // A << b = A * (2^b) this is formula to find leftshift 12 | 13 | System.out.println(LeftShift); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /17. Bit Manipulation/p7_Binary_Right_Shift.java: -------------------------------------------------------------------------------- 1 | public class p7_Binary_Right_Shift { 2 | 3 | public static void main(String[] args) { 4 | 5 | int A = 6 ; // 000110 6 | int b = 1 ; 7 | 8 | // A >> b ; (6 >> 1 = 000110 >> 1(All values are shifted right with one time and the starting bits fill by zeros) => 000011 ) 9 | 10 | int RightShift = A >> b ; // 000101 = 000011(3) 11 | // A >> b ; // A >> b = A / (2^b) this is formula to find Rightshift 12 | 13 | System.out.println(RightShift); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /17. Bit Manipulation/p8_Odd_or_Even_using_Bitwise.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class p8_Odd_or_Even_using_Bitwise { 4 | 5 | public static void main(String[] args) { 6 | 7 | // Number Lastbit 0 then even or 1 then odd 8 | // 0 = 000 0 9 | // 1 = 001 1 10 | // 2 = 010 0 11 | // 3 = 011 1 12 | // 4 = 100 0 13 | // 5 = 101 1 14 | // 6 = 110 0 15 | // 7 = 111 1 16 | 17 | // (any number) & 0 = 00000 18 | // (any nubmer) & 1 = one is not one is look like (0000001 ) thats why all bits zero but last bit if (zero) then one else (one)then zero 19 | 20 | Scanner sc = new Scanner(System.in); 21 | 22 | int num = sc.nextInt(); 23 | 24 | if((num & 1) == 0){ 25 | System.out.println(num + " is Even Nubmer.."); 26 | }else{ 27 | System.out.println(num + " is Odd Nubmer .. "); 28 | } 29 | 30 | 31 | sc.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /17. Bit Manipulation/p9_Get_ith_Bit.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class p9_Get_ith_Bit { 4 | 5 | public static void main(String[] args) { 6 | 7 | Scanner sc = new Scanner(System.in); 8 | 9 | System.out.print("Enter number to find length ith :- "); 10 | int num = sc.nextInt(); 11 | 12 | System.out.print("Enter get ith bit :- "); 13 | int ithBit = sc.nextInt(); 14 | 15 | if((num & ithBit) == 0){ 16 | System.out.println(" 0 th Nubmer.."); 17 | }else{ 18 | System.out.println(" 1 th Nubmer .. "); 19 | } 20 | 21 | sc.close(); 22 | } 23 | } -------------------------------------------------------------------------------- /18. Oops/p1_What_is_Class.java: -------------------------------------------------------------------------------- 1 | public class p1_What_is_Class { 2 | 3 | public static void main(String[] args) { 4 | 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /18. Oops/p2_Access_Modifiers.java: -------------------------------------------------------------------------------- 1 | public class p2_Access_Modifiers { 2 | public static void main(String[] args) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /18. Oops/p3_Getters_setters.java: -------------------------------------------------------------------------------- 1 | public class p3_Getters_setters { 2 | public static void main(String[] args) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /18. Oops/p4_Encapsulation.java: -------------------------------------------------------------------------------- 1 | public class p4_Encapsulation { 2 | 3 | public static void main(String[] args) { 4 | 5 | } 6 | } -------------------------------------------------------------------------------- /18. Oops/p5_Constructors.java: -------------------------------------------------------------------------------- 1 | public class p5_Constructors { 2 | 3 | public static void main(String[] args) { 4 | 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /18. Oops/p6_Type_of_Constructors.java: -------------------------------------------------------------------------------- 1 | public class p6_Type_of_Constructors { 2 | 3 | public static void main(String[] args) { 4 | 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /18. Oops/p7_Copy_Constructor.java: -------------------------------------------------------------------------------- 1 | public class p7_Copy_Constructor { 2 | 3 | public static void main(String[] args) { 4 | 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /18. Oops/p8_Destructors.java: -------------------------------------------------------------------------------- 1 | public class p8_Destructors { 2 | 3 | public static void main(String[] args) { 4 | 5 | } 6 | } 7 | --------------------------------------------------------------------------------