├── Arrays - 1 ├── Arrange_Numbers_In_Array.java ├── Array_Intersection.java ├── Find_Duplicate.java ├── Find_Unique.java ├── Linear_Search.java ├── Pair_Sum.java ├── Return_Array_Sum.java ├── Sort_0_1.java ├── Swap_Alternate.java └── Triplet_Sum.java ├── Arrays 2 ├── Binary_Search.java ├── Bubble_Sort.java ├── Check_Array_Rotation.java ├── Insertion_Sort.java ├── Merge_Two_Sorted_Arrays.java ├── Push_Zeros_to_end.java ├── Rotate_array.java ├── Second_Largest_in_array.java ├── Selection_Sort.java ├── Sort_0_1_2.java └── Sum_of_Two_Arrays.java ├── Conditionals and Loops ├── Factors.java ├── Fahrenheit_to_Celsius_Table.java ├── Find_Character_Case.java ├── Find_power_of_a_number.java ├── Multiplication_Table.java ├── Sum_of_even_and_odd.java └── Total_Salary.java ├── Functions & Scope ├── Fahrenheit_to_Celsius_Table.java └── Fibonacci_Number.java ├── Getting Started with Java └── Average_Marks.java ├── Operators & For Loop ├── All_Prime_Numbers.java ├── Binary_to_decimal.java ├── Check_Number_sequence.java ├── Decimal_to_Binary.java ├── Nth_Fibonacci_Number.java ├── Reverse_of_a_number.java ├── Square_Root.java ├── Sum_or_Product.java └── Terms_of_AP.java ├── Pattern-1 ├── Alpha_Pattern.java ├── Character_Pattern.java ├── Interesting_Alphabets.java ├── Reverse_Number_Pattern.java ├── Squre_Pattern.java ├── Triangle_Number_Pattern.java └── Triangular_Star_Pattern.java ├── Pattern-2 ├── Diamond_of_stars.java ├── Half_Diamond_Pattern.java ├── Inverted_Number_Pattern.java ├── Mirror_Image_Number_Pattern.java ├── Odd_Square.java ├── Parallelogram_Pattern.java ├── Star_Pattern.java ├── Sum_Pattern.java └── Triangle_of_Numbers.java ├── README.md ├── Strings ├── All_substrings.java ├── Check_Permutation.java ├── Compress_the_String.java ├── Count_Words.java ├── Highest_Occuring_Character.java ├── Remove_Consecutive_Duplicates.java ├── Remove_character.java ├── Reverse_Each_Word.java ├── Reverse_String_Wordwise.java └── String_Palindrome.java └── Two Dimensional Arrays ├── Boundaries_and_Diagonals_total_sum.java ├── Largest_Row_or_Column.java ├── Print_Like_a_Wave.java ├── Print_Spiral.java └── Row_Wise_Sum.java /Arrays - 1/Arrange_Numbers_In_Array.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an empty array(ARR) and its size N. 3 | The only input taken from the user will be N and you need not worry about the array. 4 | 5 | Your task is to populate the array using the integer values 6 | in the range 1 to N(both inclusive) in the order - 1,3,.......4,2 7 | */ 8 | 9 | public class Arrange_Numbers_In_Array { 10 | public static void arrange(int[] arr, int n) { 11 | //Your code goes here 12 | if (n%2 != 0) 13 | { 14 | arr[n/2]=n; 15 | } 16 | int even = 2; int odd = 1; int i = 0; 17 | while (i < n/2) { 18 | arr[i] = odd; 19 | odd += 2; 20 | arr[n - i-1] = even; 21 | even += 2; 22 | i++; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Arrays - 1/Array_Intersection.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given two integer arrays/list(ARR1 and ARR2) of size N and M, respectively. 3 | You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists. 4 | */ 5 | 6 | public class Array_Intersection { 7 | public static void intersections(int arr1[], int arr2[]) { 8 | //Your code goes here 9 | for (int i = 0; i < arr1.length; i++) 10 | { 11 | 12 | for (int j = 0; j < arr2.length; j++) 13 | { 14 | if (arr1[i]==arr2[j]) 15 | { 16 | System.out.print(arr1[i]+" "); 17 | arr2[j]=-9999999; 18 | break; 19 | } 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Arrays - 1/Find_Duplicate.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an integer array/list(ARR) of size N which contains numbers from 0 to (N - 2). 3 | Each number is present at least once. That is, if N = 5, the array/list constitutes values ranging from 0 to 3 and among these, 4 | there is a single integer value that is present twice. You need to find and return that duplicate number present in the array. 5 | */ 6 | 7 | public class Find_Duplicate { 8 | public static int duplicateNumber(int arr[]) { 9 | //Your code goes here 10 | for (int i = 0; i < arr.length; i++) 11 | { 12 | int jg=-1; 13 | for (int j = 0; j < arr.length; j++) 14 | { 15 | if (arr[i]==arr[j]) 16 | { 17 | jg++; 18 | } 19 | } 20 | if (jg>0) 21 | return arr[i]; 22 | } 23 | return 0; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Arrays - 1/Find_Unique.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an integer array/list(ARR) of size N. Where N is equal to [2M + 1].\ 3 | 4 | Now, in the given array/list, 'M' numbers are present twice and one number is present only once. 5 | 6 | You need to find and return that number which is unique in the array/list. 7 | */ 8 | 9 | public class Find_Unique { 10 | public static int findUnique(int[] arr){ 11 | //Your code goes here 12 | for (int i = 0; i < arr.length; i++) 13 | { 14 | int jg=-1; 15 | for (int j = 0; j < arr.length; j++) 16 | { 17 | if (arr[i]==arr[j]) 18 | { 19 | jg++; 20 | } 21 | } 22 | if (jg==0) 23 | { 24 | return arr[i]; 25 | } 26 | 27 | 28 | } 29 | return arr[0]; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Arrays - 1/Linear_Search.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given a random integer array/list(ARR) of size N, and an integer X. 3 | You need to search for the integer X in the given array/list using 'Linear Search'. 4 | */ 5 | 6 | public class Linear_Search { 7 | public static int linearSearch(int arr[], int x) { 8 | //Your code goes here 9 | boolean find=false; 10 | int i; 11 | for (i =0;i< arr.length;i++){ 12 | if (arr[i]==x){ 13 | find=true; 14 | break; 15 | } 16 | } 17 | if (find==false){ 18 | return -1; 19 | } 20 | else { 21 | return i; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Arrays - 1/Pair_Sum.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an integer array/list(ARR) and a number X. 3 | Find and return the total number of pairs in the array/list which sum to X. 4 | */ 5 | 6 | public class Pair_Sum { 7 | public static int pairSum(int arr[], int x) { 8 | //Your code goes here 9 | int count=0; 10 | for (int i = 0; i < arr.length; i++) 11 | { 12 | 13 | for (int j = i; j < arr.length; j++) 14 | { 15 | if (i!=j && ((arr[i]+arr[j])==x)){ 16 | count++; 17 | } 18 | } 19 | } 20 | return count; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Arrays - 1/Return_Array_Sum.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array/list(ARR) of length N, you need to find and return the sum of all the elements in the array/list. 3 | */ 4 | 5 | import java.util.*; 6 | 7 | public class Return_Array_Sum{ 8 | 9 | public static int[] takeInput(){ 10 | Scanner s = new Scanner(System.in); 11 | int size = s.nextInt(); 12 | int[] input = new int[size]; 13 | for(int i=0;i x) 17 | { 18 | end = mid - 1; 19 | } 20 | else if 21 | (arr[mid] < x) 22 | { 23 | start = mid + 1; 24 | } 25 | else 26 | { 27 | return mid; 28 | 29 | } 30 | } 31 | return -1; 32 | } 33 | } -------------------------------------------------------------------------------- /Arrays 2/Bubble_Sort.java: -------------------------------------------------------------------------------- 1 | /* 2 | Provided with a random integer array/list(ARR) of size N, you have been required to sort this array using 'Bubble Sort'. 3 | Note: 4 | Change in the input array/list itself. You don't need to return or print the elements. 5 | */ 6 | 7 | public class Bubble_Sort { 8 | 9 | public static void bubbleSort(int[] arr){ 10 | //Your code goes here 11 | for (int i = 0; i < arr.length-1; i++) 12 | { 13 | 14 | for (int j = 0; j < arr.length - i-1; j++) 15 | { 16 | if (arr[j]>arr[j+1]) 17 | { 18 | int f1=arr[j+1]; 19 | arr[j+1]=arr[j]; 20 | arr[j]=f1; 21 | } 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Arrays 2/Check_Array_Rotation.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given an integer array/list(ARR) of size N. 3 | It has been sorted(in increasing order) and then rotated by some number 'K' in the right hand direction. 4 | 5 | Your task is to write a function that returns the value of 'K', that means, the index from which the array/list has been rotated. 6 | */ 7 | 8 | public class Check_Array_Rotation { 9 | 10 | public static int arrayRotateCheck(int[] arr){ 11 | int check=0; 12 | for (int i = 1; i < arr.length; i++) 13 | { 14 | if (arr[i-1]= 0) 18 | { 19 | if(arr[j] > temp) 20 | { 21 | arr[j + 1] = arr[j]; 22 | } 23 | else 24 | { 25 | break; 26 | } 27 | j -= 1; 28 | } 29 | arr[j + 1] = temp; i += 1; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Arrays 2/Merge_Two_Sorted_Arrays.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given two sorted arrays/lists(ARR1 and ARR2) of size N and M respectively, 3 | merge them into a third array/list such that the third array is also sorted. 4 | */ 5 | 6 | public class Merge_Two_Sorted_Arrays { 7 | 8 | public static int[] merge(int arr1[], int arr2[]) { 9 | //Your code goes here 10 | int[] ans = new int[arr1.length + arr2.length]; 11 | int i = 0, j = 0, k = 0; 12 | while(i < arr1.length && j < arr2.length) 13 | { 14 | if(arr1[i] < arr2[j]) 15 | { 16 | ans[k] = arr1[i]; k += 1; i += 1; 17 | } 18 | else 19 | { 20 | ans[k] = arr2[j]; k += 1; j += 1; 21 | } 22 | } 23 | while(i < arr1.length) 24 | { 25 | ans[k] = arr1[i]; k += 1; i += 1; 26 | } 27 | while(j < arr2.length) 28 | { 29 | ans[k] = arr2[j]; k += 1; j += 1; 30 | } 31 | return ans; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Arrays 2/Push_Zeros_to_end.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given a random integer array/list(ARR) of size N. 3 | You have been required to push all the zeros that are present in the array/list to the end of it. 4 | Also, make sure to maintain the relative order of the non-zero elements. 5 | */ 6 | 7 | public class Push_Zeros_to_end { 8 | 9 | public static int[] pushZerosAtEnd(int[] arr) { 10 | int[] arr2=new int[arr.length]; 11 | int p=0; 12 | for (int i = 0; i < arr2.length; i++) { 13 | if(arr[i]!=0){ 14 | arr2[p]=arr[i]; 15 | p++; 16 | } 17 | } 18 | System.arraycopy(arr2, 0, arr, 0, arr.length); 19 | return arr; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Arrays 2/Rotate_array.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have been given a random integer array/list(ARR) of size N. 3 | Write a function that rotates the given array/list by D elements(towards the left). 4 | */ 5 | 6 | public class Rotate_array { 7 | 8 | public static void rotate(int[] arr, int d) { 9 | int[] temp=new int[d]; 10 | for(int i=0;i b && SecL < b) 23 | { 24 | SecL = b; 25 | } 26 | } 27 | // System.out.print(SecL); 28 | return SecL; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Arrays 2/Selection_Sort.java: -------------------------------------------------------------------------------- 1 | /* 2 | Provided with a random integer array/list(ARR) of size N, you have been required to sort this array using 'Selection Sort'. 3 | Note: 4 | Change in the input array/list itself. You don't need to return or print the elements. 5 | */ 6 | 7 | public class Selection_Sort { 8 | 9 | public static void selectionSort(int[] arr) { 10 | //Your code goes here 11 | int index; 12 | for(int i=0;i= 0 && j >= 0) 18 | { 19 | int sum = arr1[i] + arr2[j] + carry; 20 | output[k] = sum % 10; 21 | carry = sum / 10; 22 | i -= 1; 23 | j -= 1; 24 | k -= 1; 25 | } 26 | while(i >= 0) 27 | { 28 | int sum = arr1[i] + carry; 29 | output[k] = sum % 10; 30 | carry = sum / 10; 31 | i -= 1; k -= 1; 32 | } 33 | while(j >= 0) 34 | { 35 | int sum = arr2[j] + carry; output[k] = sum % 10; 36 | carry = sum / 10; 37 | j -= 1; 38 | k -= 1; 39 | } 40 | output[0] = carry; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Conditionals and Loops/Factors.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to print all the factors of a number other than 1 and the number itself. 3 | */ 4 | 5 | import java.util.Scanner; 6 | public class Factors { 7 | public static void main(String[] args) { 8 | // Write your code here 9 | Scanner sc = new Scanner(System.in); 10 | int n = sc.nextInt(); 11 | int i; 12 | for (i = 2; i <= n; i++) { 13 | if (n % i == 0) { 14 | if (i != n) { 15 | System.out.print(i + " "); 16 | } 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Conditionals and Loops/Fahrenheit_to_Celsius_Table.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given three values - Start Fahrenheit Value (S), End Fahrenheit value (E) and Step Size (W), 3 | you need to convert all Fahrenheit values from Start to End at the gap of W, 4 | into their corresponding Celsius values and print the table. 5 | */ 6 | 7 | import java.util.Scanner; 8 | 9 | public class Fahrenheit_to_Celsius_Table { 10 | public static void main(String[] args) { 11 | 12 | /* 13 | * Your class should be named Solution. 14 | * Read input as specified in the question. 15 | * Print output as specified in the question. 16 | */ 17 | Scanner s = new Scanner(System.in); 18 | int S = s.nextInt(); 19 | int E = s.nextInt(); 20 | int W = s.nextInt(); 21 | int fah; 22 | while (S <= E) { 23 | fah = ((5 * (S - 32)) / 9); 24 | System.out.println(S + " " + fah); 25 | S += W; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Conditionals and Loops/Find_Character_Case.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that takes a character as input and prints either 1, 0 or -1 according to the following rules. 3 | 4 | 1, if the character is an uppercase alphabet (A - Z) 5 | 0, if the character is a lowercase alphabet (a - z) 6 | -1, if the character is not an alphabet 7 | 8 | */ 9 | 10 | import java.util.Scanner; 11 | 12 | public class Find_Character_Case { 13 | public static void main(String[] args) { 14 | Scanner scanner = new Scanner(System.in); 15 | char ch = scanner.next().charAt(0); 16 | if (ch >= 'A' && ch <= 'Z') 17 | System.out.println("1"); 18 | 19 | else if (ch >= 'a' && ch <= 'z') 20 | System.out.println("0"); 21 | 22 | else 23 | System.out.println("-1"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Conditionals and Loops/Find_power_of_a_number.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to find x to the power n (i.e. x^n). 3 | Take x and n from the user. You need to print the answer. 4 | 5 | Note : For this question, you can assume that 0 raised to the power of 0 is 1 6 | */ 7 | 8 | import java.util.Scanner; 9 | public class Find_power_of_a_number { 10 | public static void main(String[] args) { 11 | // Write your code here 12 | Scanner sc = new Scanner(System.in); 13 | int x = sc.nextInt(); 14 | int temp = 1; 15 | int n = sc.nextInt(); 16 | 17 | while (n != 0) { 18 | temp = temp * x; 19 | n--; 20 | } 21 | 22 | System.out.println(temp); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Conditionals and Loops/Multiplication_Table.java: -------------------------------------------------------------------------------- 1 | //Write a program to print multiplication table of n 2 | 3 | import java.util.Scanner; 4 | public class Multiplication_Table { 5 | public static void main(String[] args) { 6 | // Write your code here 7 | 8 | Scanner scanner = new Scanner(System.in); 9 | int n = scanner.nextInt(); 10 | int i; 11 | for (i = 1; i <= 10; i++) { 12 | System.out.println(i * n); 13 | } 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Conditionals and Loops/Sum_of_even_and_odd.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to input an integer N and print the sum of all its even digits and sum of all its odd digits separately. 3 | 4 | Digits mean numbers, not the places! That is, if the given integer is "13245", even digits are 2 & 4 and odd digits are 1, 3 & 5. 5 | Input format : 6 | Integer N 7 | */ 8 | 9 | import java.util.Scanner; 10 | public class Sum_of_even_and_odd { 11 | public static void main(String[] args) { 12 | // Write your code here 13 | Scanner sc = new Scanner(System.in); 14 | int n = sc.nextInt(); 15 | int evensum = 0, oddsum = 0; 16 | while (n != 0) { 17 | int temp = n % 10; 18 | if (temp % 2 == 0) { 19 | evensum += temp; 20 | } 21 | if (temp % 2 != 0) { 22 | oddsum += temp; 23 | } 24 | n = (int) (n / 10); 25 | } 26 | System.out.println(evensum + " " + oddsum); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Conditionals and Loops/Total_Salary.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to calculate the total salary of a person. 3 | The user has to enter the basic salary (an integer) and the grade (an uppercase character), 4 | and depending upon which the total salary is calculated as - 5 | 6 | totalSalary = basic + hra + da + allow – pf 7 | */ 8 | 9 | import java.util.*; 10 | public class Total_Salary { 11 | public static void main(String[] args) { 12 | // Write your code here 13 | Scanner sc = new Scanner(System.in); 14 | int basic = sc.nextInt(); 15 | 16 | char grade = sc.next().charAt(0); 17 | 18 | int a = basic; 19 | int allow = 1300; 20 | double hra = (0.2d * basic); 21 | double da = (0.5d * basic); 22 | if (grade == 'A') { 23 | allow = 1700; 24 | } else if (grade == 'B') { 25 | allow = 1500; 26 | } 27 | double pf = (0.11d * basic); 28 | double totalSalary = a + hra; 29 | totalSalary += da; 30 | totalSalary += allow; 31 | totalSalary -= pf; 32 | 33 | System.out.println(Math.round(totalSalary)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Functions & Scope/Fahrenheit_to_Celsius_Table.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given three values - Start Fahrenheit Value (S), End Fahrenheit value (E) and Step Size (W), 3 | you need to convert all Fahrenheit values from Start to End at the gap of W, 4 | into their corresponding Celsius values and print the table. 5 | */ 6 | 7 | import java.util.*; 8 | 9 | public class Fahrenheit_to_Celsius_Table { 10 | public static void printFahrenheitTable(int start, int end, int step) 11 | { 12 | for(int i=start;i<=end;i=i+step) 13 | { 14 | int cel=(int)((5.0/9)*(i-32)); 15 | System.out.println(i +"\t"+ cel); 16 | } 17 | } 18 | 19 | public static void main(String[] args) 20 | { 21 | Scanner sc=new Scanner(System.in); 22 | int start=sc.nextInt(); 23 | int end=sc.nextInt(); 24 | int step=sc.nextInt(); 25 | printFahrenheitTable(start,end,step); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Functions & Scope/Fibonacci_Number.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a number N, figure out if it is a member of fibonacci series or not. 3 | Return true if the number is member of fibonacci series else false. 4 | 5 | Fibonacci Series is defined by the recurrence 6 | F(n) = F(n-1) + F(n-2) 7 | where F(0) = 0 and F(1) = 1 8 | */ 9 | 10 | public class Fibonacci_Number { 11 | static boolean isPerfectSquare(int x) 12 | { 13 | int s = (int) Math.sqrt(x); 14 | return (s*s == x); 15 | } 16 | 17 | public static boolean checkMember(int n) 18 | { 19 | return isPerfectSquare(5*n*n + 4) || isPerfectSquare(5*n*n - 4); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Getting Started with Java/Average_Marks.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to input a name(as a single character) and marks of three tests as m1, m2, and m3 of a student 3 | considering all the three marks have been given in integer format. 4 | 5 | Now, you need to calculate the average of the given marks and print it along with the name as mentioned in the output format section. 6 | 7 | All the test marks are in integers and hence calculate the average in integer as well. 8 | That is, you need to print the integer part of the average only and neglect the decimal part. 9 | */ 10 | 11 | import java.util.*; 12 | 13 | public class Average_Marks { 14 | public static void main(String[] args) { 15 | 16 | Scanner sc = new Scanner(System.in); 17 | String str = sc.next(); 18 | int m1 = sc.nextInt(); 19 | int m2 = sc.nextInt(); 20 | int m3 = sc.nextInt(); 21 | int avg = (m1 + m2 + m3) / 3; 22 | System.out.println(str); 23 | System.out.print(avg); 24 | 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /Operators & For Loop/All_Prime_Numbers.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given an integer N, print all the prime numbers that lie in the range 2 to N (both inclusive). 3 | Print the prime numbers in different lines. 4 | */ 5 | 6 | import java.util.*; 7 | 8 | public class All_Prime_Numbers { 9 | public static void main(String[] args) { 10 | 11 | /* Your class should be named Solution. 12 | * Read input as specified in the question. 13 | * Print output as specified in the question. 14 | */ 15 | int i, number, count; 16 | Scanner sc = new Scanner(System.in); 17 | int n = sc.nextInt(); 18 | for(number = 1; number <= n; number++) 19 | { 20 | count = 0; 21 | for (i = 2; i <= number/2; i++) 22 | { 23 | if(number % i == 0) 24 | { 25 | count++; 26 | break; 27 | } 28 | } 29 | if(count == 0 && number != 1 ) 30 | { 31 | System.out.print(number+"\n"); 32 | } 33 | } 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Operators & For Loop/Binary_to_decimal.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary number as an integer N, convert it into decimal and print. 3 | */ 4 | 5 | import java.util.*; 6 | 7 | public class Binary_to_decimal { 8 | 9 | public static void main(String[] args) { 10 | // Write your code here 11 | Scanner sc = new Scanner(System.in); 12 | int num = sc.nextInt(); 13 | int dec_value = 0; 14 | int base = 1; 15 | int temp = num; 16 | 17 | while (temp > 0) 18 | { 19 | int last_digit = temp % 10; 20 | temp = temp / 10; 21 | dec_value += last_digit * base; 22 | base = base * 2; 23 | } 24 | 25 | System.out.println(dec_value); 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Operators & For Loop/Check_Number_sequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | You are given S, a sequence of n integers i.e. S = s1, s2, ..., sn. 3 | Compute if it is possible to split S into two parts : s1, s2, ..., si and si+1, si+2, ….., sn (0 <= i <= n) in such a way 4 | that the first part is strictly decreasing while the second is strictly increasing one. 5 | */ 6 | 7 | import java.util.*; 8 | 9 | public class Check_Number_sequence { 10 | 11 | public static void main(String[] args) { 12 | // Write your code here 13 | Scanner sc=new Scanner(System.in); 14 | int n=sc.nextInt();int f=0; 15 | int arr[]=new int[n]; 16 | 17 | for(int i=0;iarr[j+1]) 32 | { 33 | f=1; 34 | } 35 | } 36 | if(f==1) 37 | { 38 | System.out.println("false"); 39 | } 40 | else 41 | { 42 | System.out.println("true"); 43 | } 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Operators & For Loop/Decimal_to_Binary.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a decimal number (integer N), convert it into binary and print. 3 | The binary number should be in the form of an integer. 4 | */ 5 | 6 | import java.util.*; 7 | 8 | public class Decimal_to_Binary { 9 | 10 | public static void main(String[] args) { 11 | // Write your code here 12 | Scanner sc = new Scanner(System.in); 13 | int n = sc.nextInt(); 14 | int[] binaryNum = new int[32]; 15 | int i = 0; 16 | if(n==0) 17 | { 18 | System.out.println(0); 19 | } 20 | 21 | while (n > 0) 22 | { 23 | binaryNum[i] = n % 2; 24 | n = n / 2; 25 | i++; 26 | } 27 | 28 | for (int j = i - 1; j >= 0; j--) 29 | { 30 | System.out.print(binaryNum[j]); 31 | } 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Operators & For Loop/Nth_Fibonacci_Number.java: -------------------------------------------------------------------------------- 1 | /* 2 | Nth term of Fibonacci series F(n), where F(n) is a function, is calculated using the following formula - 3 | F(n) = F(n-1) + F(n-2), 4 | Where, F(1) = F(2) = 1 5 | Provided N you have to find out the Nth Fibonacci Number. 6 | */ 7 | 8 | import java.util.*; 9 | 10 | public class Nth_Fibonacci_Number { 11 | 12 | static int fib(int n) 13 | { 14 | if (n <= 1) 15 | return n; 16 | return fib(n - 1) + fib(n - 2); 17 | } 18 | 19 | public static void main(String[] args) { 20 | 21 | /* Your class should be named Solution. 22 | * Read input as specified in the question. 23 | * Print output as specified in the question. 24 | */ 25 | 26 | Scanner sc = new Scanner(System.in); 27 | int n = sc.nextInt(); 28 | System.out.println(fib(n)); 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Operators & For Loop/Reverse_of_a_number.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to generate the reverse of a given number N. 3 | Print the corresponding reverse number. 4 | 5 | Note : If a number has trailing zeros, then its reverse will not include them. 6 | For e.g., reverse of 10400 will be 401 instead of 00401. 7 | */ 8 | 9 | import java.util.*; 10 | 11 | public class Reverse_of_a_number { 12 | 13 | public static void main(String[] args) { 14 | // Write your code here 15 | int d, number,temp, revnum = 0; 16 | Scanner sc = new Scanner(System.in); 17 | number = sc.nextInt(); 18 | temp = number; 19 | 20 | while (temp >0) 21 | { 22 | d = temp %10; 23 | revnum = (revnum*10)+d; 24 | temp = temp/10; 25 | } 26 | 27 | System.out.println(revnum); 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Operators & For Loop/Square_Root.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a number N, find its square root. You need to find and print only the integral part of square root of N. 3 | For eg. if number given is 18, answer is 4. 4 | */ 5 | 6 | import java.util.*; 7 | 8 | public class Square_Root { 9 | 10 | public static void main(String[] args) { 11 | // Write your code here 12 | Scanner sc = new Scanner(System.in); 13 | int x = sc.nextInt(); 14 | 15 | if (x == 0 || x == 1) 16 | { 17 | System.out.println(x); 18 | } 19 | else 20 | { 21 | int i = 1, result = 1; 22 | 23 | while (result <= x) 24 | { 25 | i++; 26 | result = i * i; 27 | } 28 | 29 | System.out.println(i - 1); 30 | } 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Operators & For Loop/Sum_or_Product.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that asks the user for a number N and a choice C. 3 | And then give them the possibility to choose between computing the sum and computing the product of all integers in the range 1 to N (both inclusive). 4 | 5 | If C is equal to - 6 | 1, then print the sum 7 | 2, then print the product 8 | Any other number, then print '-1' (without the quotes) 9 | */ 10 | 11 | import java.util.*; 12 | 13 | public class Sum_or_Product { 14 | public static void main(String[] args) { 15 | // Write your code here 16 | Scanner sc = new Scanner(System.in); 17 | int N = sc.nextInt(); 18 | int C = sc.nextInt(); 19 | int i,sum = 0, pro = 1; 20 | 21 | switch(C) 22 | { 23 | case 1: 24 | for(i=0;i<=N;i++) 25 | { 26 | sum = sum + i; 27 | } 28 | System.out.println(sum); 29 | break; 30 | 31 | case 2: 32 | for(i=1;i<=N;i++) 33 | { 34 | pro = pro*i; 35 | } 36 | System.out.println(pro); 37 | break; 38 | 39 | default: 40 | System.out.println(-1); 41 | break; 42 | } 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Operators & For Loop/Terms_of_AP.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to print first x terms of the series 3N + 2 which are not multiples of 4. 3 | */ 4 | 5 | import java.util.*; 6 | 7 | public class Terms_of_AP { 8 | 9 | public static void main(String[] args) { 10 | // Write your code here 11 | Scanner sc = new Scanner(System.in); 12 | int n = sc.nextInt(); 13 | int count = 0; 14 | for(int i=1; count decider; space--) 33 | System.out.print(" "); 34 | for (j = (row * 2) - 1; j > (i * 2); j--) 35 | System.out.print("*"); 36 | System.out.print("\n"); 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Pattern-2/Half_Diamond_Pattern.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to print N number of rows for Half Diamond pattern using stars and numbers 3 | Note : There are no spaces between the characters in a single line. 4 | */ 5 | 6 | import java.util.Scanner; 7 | 8 | public class Half_Diamond_Pattern { 9 | public static void main(String[] args) { 10 | // Write your code here 11 | Scanner sc = new Scanner(System.in); 12 | int n = sc.nextInt(); 13 | System.out.println("*"); 14 | for (int i = 1; i <= n; i++) 15 | { 16 | int j = 1; 17 | System.out.print("*"); 18 | while (j <= i) 19 | { 20 | System.out.print(j); 21 | j++; 22 | } 23 | j--; 24 | while (--j >= 1) 25 | { 26 | System.out.print(j ); 27 | } 28 | System.out.println("*"); 29 | } 30 | for (int i = n - 1; i >= 1; i--) 31 | { 32 | int j = 1; 33 | System.out.print("*"); 34 | while (j <= i) { 35 | System.out.print(j); 36 | j++; 37 | } 38 | j--; 39 | while (--j >= 1) 40 | { 41 | System.out.print(j ); 42 | } 43 | System.out.println("*"); 44 | } 45 | System.out.println("*"); 46 | 47 | } 48 | } -------------------------------------------------------------------------------- /Pattern-2/Inverted_Number_Pattern.java: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | 4444 5 | 333 6 | 22 7 | 1 8 | */ 9 | 10 | import java.util.Scanner; 11 | 12 | public class Inverted_Number_Pattern { 13 | public static void main(String[] args) { 14 | 15 | /* 16 | * Your class should be named Solution. 17 | * Read input as specified in the question. 18 | * Print output as specified in the question. 19 | */ 20 | Scanner sc = new Scanner(System.in); 21 | int n = sc.nextInt(); 22 | for (int i = n; i >= 1; i--) { 23 | for (int j = 1; j <= i; j++) { 24 | System.out.print(i); 25 | } 26 | System.out.println(); 27 | } 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Pattern-2/Mirror_Image_Number_Pattern.java: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern for the given N number of rows. 3 | Pattern for N = 4 4 | 5 | 1 6 | 1 2 7 | 1 2 3 8 | 1 2 3 4 9 | */ 10 | 11 | import java.util.*; 12 | 13 | public class Mirror_Image_Number_Pattern { 14 | public static void main(String[] args) { 15 | 16 | /* 17 | * Your class should be named Solution. 18 | * Read input as specified in the question. 19 | * Print output as specified in the question. 20 | */ 21 | Scanner sc = new Scanner(System.in); 22 | int n = sc.nextInt(); 23 | int count = 1; 24 | for (int i = n; i > 0; i--) { 25 | for (int j = 1; j < i; j++) 26 | System.out.print(" "); 27 | for (int j = 1; j <= count; j++) { 28 | System.out.print(j); 29 | } 30 | System.out.println(); 31 | count++; 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Pattern-2/Odd_Square.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to print the pattern for the given N number of rows. 3 | For N = 4 4 | 1357 5 | 3571 6 | 5713 7 | 7135 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class Odd_Square { 13 | 14 | public static void main(String[] args) { 15 | 16 | // Write your code here 17 | 18 | Scanner sc = new Scanner(System.in); 19 | int n = sc.nextInt(); 20 | 21 | for (int i = 1; i <= n; i++) 22 | { 23 | for (int j = i - 1; j < n; j++ ) 24 | { 25 | System.out.print(j * 2 + 1 + ""); 26 | } 27 | for(int k = 0; k < i - 1; k++) 28 | { 29 | System.out.print(k * 2 + 1 + ""); 30 | } 31 | System.out.println(); 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Pattern-2/Parallelogram_Pattern.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to print parallelogram pattern for the given N number of rows. 3 | For N = 4 4 | **** 5 | **** 6 | **** 7 | **** 8 | */ 9 | 10 | import java.util.*; 11 | 12 | public class Parallelogram_Pattern { 13 | public static void main(String[] args) { 14 | // Write your code here 15 | Scanner sc = new Scanner(System.in); 16 | int row = sc.nextInt(); 17 | 18 | for (int i = 1; i <= row; i++) 19 | { 20 | for (int j = 2; j <= i; j++) 21 | { 22 | System.out.print(" "); 23 | } 24 | for (int j = 1; j <= row; j++) 25 | { 26 | System.out.print("*"); 27 | } 28 | System.out.println(); 29 | } 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Pattern-2/Star_Pattern.java: -------------------------------------------------------------------------------- 1 | /* 2 | Print the following pattern 3 | Pattern for N = 4 4 | 5 | * 6 | *** 7 | ***** 8 | ******* 9 | */ 10 | 11 | import java.util.Scanner; 12 | 13 | public class Star_Pattern { 14 | public static void main(String[] args) { 15 | 16 | /* 17 | * Your class should be named Solution. 18 | * Read input as specified in the question. 19 | * Print output as specified in the question. 20 | */ 21 | 22 | Scanner sc = new Scanner(System.in); 23 | int n = sc.nextInt(); 24 | int space = n - 1; 25 | int i = 1; 26 | while (i <= n) { 27 | int j = 1; 28 | space = n - i; 29 | while (space > 0) { 30 | System.out.print(" "); 31 | space--; 32 | } 33 | while (j <= ((2 * i) - 1)) { 34 | System.out.print("*"); 35 | j += 1; 36 | } 37 | System.out.println(""); 38 | i += 1; 39 | } 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Pattern-2/Sum_Pattern.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to print triangle of user defined integers sum. 3 | */ 4 | 5 | import java.util.*; 6 | 7 | public class Sum_Pattern { 8 | 9 | public static void main(String[] args) { 10 | // Write your code here 11 | Scanner sc = new Scanner(System.in); 12 | int n = sc.nextInt(); 13 | int i, j, num; 14 | 15 | for(i=1; i<=n; i++) 16 | { 17 | num=1; 18 | int sum=0; 19 | for(j=1; j<=i; j++) 20 | { 21 | System.out.print(num+ ""); 22 | sum=sum+num; 23 | if (num
3 | This repo includes:
4 |
    5 |
  1. Getting Started with Java
  2. 6 | 9 | 10 |
  3. Conditionals and Loops
  4. 11 | 20 | 21 |
  5. Patterns - 1
  6. 22 | 31 | 32 |
  7. Patterns - 2
  8. 33 | 44 | 45 |
  9. Operators & For Loop
  10. 46 | 57 | 58 |
  11. Functions & Scope
  12. 59 | 63 | 64 |
  13. Arrays - 1
  14. 65 | 77 | 78 |
  15. Arrays 2
  16. 79 | 92 | 93 |
  17. Strings
  18. 94 | 106 | 107 |
  19. Two Dimensional Arrays
  20. 108 | 115 |
116 | 117 |

Coding Ninja 118 |

Please rate my repo 119 | -------------------------------------------------------------------------------- /Strings/All_substrings.java: -------------------------------------------------------------------------------- 1 | /* 2 | For a given input string(str), write a function to print all the possible substrings. 3 | Substring 4 | A substring is a contiguous sequence of characters within a string. 5 | 6 | Example: "cod" is a substring of "coding". Whereas, "cdng" is not as the characters taken are not contiguous 7 | */ 8 | 9 | public class All_substrings { 10 | 11 | public static void printSubstrings(String str) { 12 | //Your code goes here 13 | for (int i = 0; i < str.length(); i++) 14 | { 15 | for (int j = i; j < str.length(); j++) 16 | { 17 | for (int k = i; k <=j; k++) 18 | { 19 | System.out.print(str.charAt(k)); 20 | } 21 | System.out.println(); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Strings/Check_Permutation.java: -------------------------------------------------------------------------------- 1 | /* 2 | For a given two strings, 'str1' and 'str2', check whether they are a permutation of each other or not. 3 | Permutations of each other 4 | Two strings are said to be a permutation of each other when either of the string's characters can be rearranged so that it becomes identical to the other one. 5 | 6 | Example: 7 | str1= "sinrtg" 8 | str2 = "string" 9 | 10 | The character of the first string(str1) can be rearranged to form str2 and hence we can say that the given strings are a permutation of each other. 11 | */ 12 | 13 | import java.util.*; 14 | 15 | public class Check_Permutation { 16 | 17 | public static boolean isPermutation(String str1, String str2) { 18 | //Your code goes here 19 | int n1 = str1.length(); 20 | int n2 = str2.length(); 21 | if (n1 != n2) 22 | return false; 23 | char ch1[] = str1.toCharArray(); 24 | char ch2[] = str2.toCharArray(); 25 | 26 | Arrays.sort(ch1); 27 | Arrays.sort(ch2); 28 | for (int i = 0; i < n1; i++) 29 | if (ch1[i] != ch2[i]) 30 | return false; 31 | return true; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Strings/Compress_the_String.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to do basic string compression. 3 | For a character which is consecutively repeated more than once, replace consecutive duplicate occurrences with the count of repetitions. 4 | 5 | Example: 6 | If a string has 'x' repeated 5 times, replace this "xxxxx" with "x5". 7 | 8 | The string is compressed only when the repeated character count is more than 1. 9 | */ 10 | 11 | public class Compress_the_String { 12 | 13 | public static String getCompressedString(String str) { 14 | // Write your code here. 15 | String s=new String(); 16 | int n = str.length(); 17 | for (int i = 0; i < n; i++) 18 | { 19 | 20 | int count = 1; 21 | while (i < n - 1 && str.charAt(i) == str.charAt(i + 1)) 22 | { 23 | count++; 24 | i++; 25 | } 26 | s+=str.charAt(i); 27 | if(count>1) 28 | { 29 | s+=count; 30 | } 31 | } 32 | return s; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Strings/Count_Words.java: -------------------------------------------------------------------------------- 1 | /* 2 | For a given input string(str), find and return the total number of words present in it. 3 | 4 | It is assumed that two words will have only a single space in between. 5 | Also, there wouldn't be any leading and trailing spaces in the given input string. 6 | */ 7 | 8 | public class Count_Words { 9 | 10 | public static int countWords(String str) { 11 | //Your code goes here 12 | if(str.length()==0) 13 | { 14 | return 0; 15 | } 16 | int spaces=0; 17 | for (int i = 0; i < str.length(); i++) 18 | { 19 | if (str.charAt(i)==' ') 20 | { 21 | spaces++; 22 | } 23 | } 24 | return spaces+1; 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Strings/Highest_Occuring_Character.java: -------------------------------------------------------------------------------- 1 | /* 2 | For a given a string(str), find and return the highest occurring character. 3 | 4 | Example: 5 | Input String: "abcdeapapqarr" 6 | Expected Output: 'a' 7 | Since 'a' has appeared four times in the string which happens to be the highest frequency character, the answer would be 'a'. 8 | */ 9 | 10 | public class Highest_Occuring_Character { 11 | 12 | public static char highestOccuringChar(String str) { 13 | //Your code goes here 14 | int count[] = new int[256]; 15 | 16 | int len = str.length(); 17 | for (int i=0; i=0 ; i--) 14 | { 15 | reverseWord+=arrOdStr[i]+" "; 16 | } 17 | return reverseWord; 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Strings/String_Palindrome.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a string, determine if it is a palindrome, considering only alphanumeric characters. 3 | */ 4 | 5 | public class String_Palindrome { 6 | 7 | public static boolean isPalindrome(String str) { 8 | //Your code goes here 9 | int len=str.length()-1; 10 | String a= ""; 11 | for (int i = len; i >=0 ; i--) 12 | { 13 | a+=(str.charAt(i)); 14 | } 15 | // System.out.println(a); 16 | return a.equals(str); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Two Dimensional Arrays/Boundaries_and_Diagonals_total_sum.java: -------------------------------------------------------------------------------- 1 | /* 2 | For a given two-dimensional square matrix of size (N x N). 3 | Find the total sum of elements on both the diagonals and at all the four boundaries. 4 | */ 5 | 6 | public class Boundaries_and_Diagonals_total_sum { 7 | 8 | public static void totalSum(int[][] mat) { 9 | //Your code goes here 10 | int horizontal=mat.length-1; 11 | if (horizontal==-1) 12 | { 13 | System.out.println(0); 14 | System.exit(0); 15 | } 16 | 17 | 18 | int vertically=mat[0].length-1; 19 | int sum=0; 20 | for (int i = 0; i < mat.length; i++) 21 | { 22 | for (int j = 0; j < mat[i].length; j++) 23 | { 24 | if (i==0 | j==0 | i==horizontal | j==vertically |i==j | j==horizontal-i) 25 | { 26 | sum+=mat[i][j]; 27 | } 28 | } 29 | } 30 | System.out.print(sum); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Two Dimensional Arrays/Largest_Row_or_Column.java: -------------------------------------------------------------------------------- 1 | /* 2 | For a given two-dimensional integer array/list of size (N x M), 3 | you need to find out which row or column has the largest sum(sum of all the elements in a row/column) amongst all the rows and columns. 4 | */ 5 | 6 | public class Largest_Row_or_Column { 7 | 8 | public static void findLargest(int mat[][]){ 9 | //Your code goes here 10 | int Lrow=Integer.MIN_VALUE; 11 | int Lcol=Integer.MIN_VALUE; 12 | int C_index=0; 13 | int R_index=0; 14 | 15 | if (mat.length>0) 16 | { 17 | for (int i = 0; i < mat.length; i++) 18 | { 19 | int sum=0; 20 | for(int ele:mat[i]) 21 | sum+=ele; 22 | if (Lrow=Lcol) 44 | System.out.println("row "+R_index+" "+Lrow); 45 | else 46 | System.out.println("column "+C_index+" "+Lcol); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Two Dimensional Arrays/Print_Like_a_Wave.java: -------------------------------------------------------------------------------- 1 | /* 2 | For a given two-dimensional integer array/list of size (N x M), print the array/list in a sine wave order, 3 | i.e, print the first column top to bottom, next column bottom to top and so on. 4 | */ 5 | 6 | public class Print_Like_a_Wave { 7 | 8 | public static void wavePrint(int mat[][]){ 9 | //Your code goes here 10 | if (mat.length==0) 11 | System.exit(0); 12 | int p=mat[0].length-1; 13 | for (int j = 0; j < mat[0].length ; j++) 14 | { 15 | for (int i = 0; i < mat.length; i++) 16 | { 17 | System.out.print(mat[i][j]+" "); 18 | } 19 | j++; 20 | if (j==mat[0].length) 21 | System.exit(0); 22 | for (int i = mat.length-1; i >= 0; i--) 23 | { 24 | System.out.print(mat[i][j]+" "); 25 | } 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Two Dimensional Arrays/Print_Spiral.java: -------------------------------------------------------------------------------- 1 | /* 2 | For a given two-dimensional integer array/list of size (N x M), print it in a spiral form. 3 | That is, you need to print in the order followed for every iteration: 4 | 5 | a. First row(left to right) 6 | b. Last column(top to bottom) 7 | c. Last row(right to left) 8 | d. First column(bottom to top) 9 | */ 10 | 11 | public class Print_Spiral { 12 | 13 | public static void spiralPrint(int matrix[][]){ 14 | //Your code goes here 15 | if (matrix.length!=0) 16 | { 17 | int x = matrix[0].length; 18 | int y = matrix.length; 19 | for (int i = 0; i < x / 2+1; i++) 20 | { 21 | for (int j = i; j < x - i; j++) 22 | { 23 | System.out.print(matrix[i][j] + " "); 24 | } 25 | for (int j = i + 1; j < y - i; j++) 26 | { 27 | System.out.print(matrix[j][x - 1 - i] + " "); 28 | } 29 | for (int j = x - 2 - i; j >= i; j--) 30 | { 31 | System.out.print(matrix[y - 1 - i][j] + " "); 32 | } 33 | for (int j = y - 2 - i; j >= i + 1; j--) 34 | { 35 | System.out.print(matrix[j][i] + " "); 36 | } 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Two Dimensional Arrays/Row_Wise_Sum.java: -------------------------------------------------------------------------------- 1 | /* 2 | For a given two-dimensional integer array/list of size (N x M), 3 | find and print the sum of each of the row elements in a single line, separated by a single space. 4 | */ 5 | 6 | import java.util.*; 7 | 8 | public class Row_Wise_Sum { 9 | 10 | public static void rowWiseSum(int[][] mat) { 11 | //Your code goes here 12 | int l= mat.length; 13 | for (int i = 0; i < l; i++) 14 | { 15 | int sum=0; 16 | for (int j = 0; j