Easy:
1)
Code : Reverse Number Pattern
Send Feedback
Print the following pattern for the given N number of rows.
Pattern for N = 4
1
21
321
4321
Input format :
Integer N (Total no. of rows)
Output format :
Pattern in N lines
Constraints
0 <= N <= 50
Sample Input 1:
5
Sample Output 1:
1
21
321
4321
54321
Sample Input 2:
6
Sample Output 2:
1
21
321
4321
54321
654321
Solution
import java.util.Scanner;
public class RevNum_pattern1{
public static void main(String args[]){
Scanner sc=new Scanner(System.in); //Scanner class in java
System.out.print("Enter the rows you want");
int rows=sc.nextInt();
System.out.println("");
for (int i=1; i<=rows; i++){
for (int j=i; j>=1; j--){
System.out.print(j);
}
System.out.println();
}
}
}
2)
Nth Fibonacci Number
Nth term of Fibonacci series F(n), where F(n) is a function, is calculated using the following formula -
F(n) = F(n-1) + F(n-2),
Where, F(1) = 1,
F(2) = 1
Provided N you have to find out the Nth Fibonacci Number.
Input Format :
The first line of each test case contains a real number ‘N’.
Output Format :
For each test case, return its equivalent Fibonacci number.
Constraints:
1 <= N <= 10000
Where ‘N’ represents the number for which we have to find its equivalent Fibonacci number.
Time Limit: 1 second
Sample Input 1:
Sample Output 1:
Explanation of Sample Input 1:
Now the number is ‘6’ so we have to find the “6th” Fibonacci number
So by using the property of the Fibonacci series i.e
[ 1, 1, 2, 3, 5, 8, 13, 21]
So the “6th” element is “8” hence we get the output.
Solution:
public class Demo{
static int fibo(int num){
int first = 0, second = 1, temp;
if (num == 0)
return first;
if (num == 1)
return second;
for (int i = 2; i <= num; i++){
temp = first + second;
first = second;
second = temp;
}
return second;
}
public static void main(String args[]){
int num = 7;
System.out.print("The 7th fibonacci number is : ");
System.out.println(fibo(num));
}
3)
Sum or Product
Write a program that asks the user for a number N and a choice C. 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).
If C is equal to -
1, then print the sum
2, then print the product
Any other number, then print '-1' (without the quotes)
Input format :
Line 1 : Integer N
Line 2 : Choice C
Output Format :
Sum or product according to user's choice
Constraints :
1 <= N <= 12
Sample Input 1 :
10
1
Sample Output 1 :
55
Sample Input 2 :
10
2
Sample Output 2 :
3628800
Sample Input 3 :
10
4
Sample Output 3 :
-1
public static void main(String[] args)
{ // Write your code here
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int c=sc.nextInt();
if(c==1)
{
int sum=0;
for(int i=1;i<=n;i++)
{
sum=sum+i;
}
System.out.println(sum);
}
else if(c==2)
{
int result=1;
for(int i=1;i<=n;i++)
{
result=result*i;
}
System.out.println(result);
}
else
{
System.out.println("-1");
}
}
medium
1)
Swap Alternate
You have been given an array/list(ARR) of size N. You need to swap every pair of alternate elements
in the array/list.
You don't need to print or return anything, just change in the input array itself.
Input Format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then
the test cases follow.
First line of each test case or query contains an integer 'N' representing the size of the array/list.
Second line contains 'N' single space separated integers representing the elements in the array/list.
Output Format :
For each test case, print the elements of the resulting array in a single row separated by a single
space.
Output for every test case will be printed in a separate line.
Constraints :
1 <= t <= 10^2
0 <= N <= 10^5
Time Limit: 1sec
Sample Input 1:
1
6
9 3 6 12 4 32
Sample Output 1 :
3 9 12 6 32 4
Sample Input 2:
2
9
9 3 6 12 4 32 5 11 19
4
1234
Sample Output 2 :
3 9 12 6 32 4 11 5 19
2143
Solution:
import java.util.Scanner;
class SwapArrayElements {
private static Scanner sc;
public static void main (String [] args) {
sc = new Scanner(System.in);
System.out.println("Input value of N should be even");
int N = sc.nextInt();
int [] a = new int [N];
System.out.print("Input elements in array- ");
for(int i= 0; i<N; i++)
a [i] = sc.nextInt();
System.out.print("Original Array Elements are - ");
for (int i = 0; i <N ; i++)
System.out.print(a[i] +" ");
for (int i = 0; i <N-1 ; i+=2)
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
System.out.println("n");
System.out.print("Swapped Array Result is- ");
for (int i = 0; i <N ; i++)
System.out.print(a[i] +" ");
2)
Find Unique
You have been given an integer array/list(ARR) of size N. Where N is equal to [2M + 1].
Now, in the given array/list, 'M' numbers are present twice and one number is present only once.
You need to find and return that number which is unique in the array/list.
Note:
Unique element is always present in the array/list according to the given condition.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then
the test cases follow.
First line of each test case or query contains an integer 'N' representing the size of the array/list.
Second line contains 'N' single space separated integers representing the elements in the array/list.
Output Format :
For each test case, print the unique element present in the array.
Output for every test case will be printed in a separate line.
Constraints :
1 <= t <= 10^2
0 <= N <= 10^3
Time Limit: 1 sec
Sample Input 1:
1
7
2316362
Sample Output 1:
1
Sample Input 2:
2
5
24727
9
1 3 1 3 6 6 7 10 7
Sample Output 2:
4
10
Import java.util.*;
Class main
{
Public static void main(String args[])
{
Scanner ob= new Scanner(System.in);
Int t=ob.nextInt();
While (t>0)
{
Int n=nextInt();
Int a[]=new int[n];
Int i;
for(i=0;i<n;i++)
{
a[i]=ob.nextInt();
}
for(i=0;i<n;i++)
{
Int ans=ans^a[i]
}
System.out.println(ans);
T–;
}
3)
Pair Sum
Send Feedback
You have been given an integer array/list(ARR) and a number X. Find and return the total number of
pairs in the array/list which sum to X.
Note:
Given array/list can contain duplicate elements.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then
the test cases follow.
First line of each test case or query contains an integer 'N' representing the size of the first array/list.
Second line contains 'N' single space separated integers representing the elements in the array/list.
Third line contains an integer 'X'.
Output format :
For each test case, print the total number of pairs present in the array/list.
Output for every test case will be printed in a separate line.
Constraints :
1 <= t <= 10^2
0 <= N <= 10^3
0 <= X <= 10^9
Time Limit: 1 sec
Sample Input 1:
1
9
136254324
7
Sample Output 1:
7
Sample Input 2:
2
9
136254324
12
6
2 8 10 5 -2 5
10
Sample Output 2:
0
2
Explanation for Input 2:
Since there doesn't exist any pair with sum equal to 12 for the first query, we print 0.
For the second query, we have 2 pairs in total that sum up to 10. They are, (2, 8) and (5, 5).
public class find {
public static void main(String args[])
{
int[] arr = { 1, 5, 7, -1, 5 };
int sum = 6;
getPairsCount(arr, sum);
}
// Prints number of pairs in arr[0..n-1] with sum equal
// to 'sum'
public static void getPairsCount(int[] arr, int sum)
{
int count = 0; // Initialize result
// Consider all possible pairs and check their sums
for (int i = 0; i < arr.length; i++)
for (int j = i + 1; j < arr.length; j++)
if ((arr[i] + arr[j]) == sum)
count++;
System.out.printf("Count of pairs is %d", count);
}
}
Difficult
1)
Spiral Print
Send Feedback
For a given two-dimensional integer array/list of size (N x M), print it in a spiral form. That is, you need
to print in the order followed for every iteration:
a. First row(left to right)
b. Last column(top to bottom)
c. Last row(right to left)
d. First column(bottom to top)
Mind that every element will be printed only once.
Refer to the Image:
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then
the test cases follow.
First line of each test case or query contains two integer values, 'N' and 'M', separated by a single
space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list.
Second line onwards, the next 'N' lines or rows represent the ith row values.
Each of the ith row constitutes 'M' column values separated by a single space.
Output format :
For each test case, print the elements of the two-dimensional array/list in the spiral form in a single
line, separated by a single space.
Output for every test case will be printed in a seperate line.
Constraints :
1 <= t <= 10^2
0 <= N <= 10^3
0 <= M <= 10^3
Time Limit: 1sec
Sample Input 1:
1
44
1234
5678
9 10 11 12
13 14 15 16
Sample Output 1:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Sample Input 2:
2
33
123
456
789
31
10
20
30
Sample Output 2:
123698745
10 20 30
public class PrintMatrixInSpiralForm {
public static void main(String args[]){
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int w = 0;
int x = a.length-1;
int y = 0;
int z = a[0].length-1;
while(w <= x && y <= z){
for (int i = w; i <= z; i++) {
System.out.print(a[w][i] + " ");
}
for (int i = w+1; i <= x; i++) {
System.out.print(a[i][z] + " ");
}
if(w+1 <= x){
for (int i = z-1; i >= y; i--) {
System.out.print(a[x][i] + " ");
}
}
if(y+1 <= z){
for (int i = x-1; i > w; i--) {
System.out.print(a[i][y] + " ");
}
}
w++;
x--;
y++;
z--;
}
}
}
2)
Row Wise Sum
For a given two-dimensional integer array/list of size (N x M), find and print the sum of each of the row
elements in a single line, separated by a single space.
Input Format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then
the test cases follow.
First line of each test case or query contains two integer values, 'N' and 'M', separated by a single
space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list.
Second line onwards, the next 'N' lines or rows represent the ith row values.
Each of the ith row constitutes 'M' column values separated by a single space.
Output Format :
For each test case, print the sum of every ith row elements in a single line separated by a single
space.
Output for every test case will be printed in a seperate line.
Constraints :
1 <= t <= 10^2
0 <= N <= 10^3
0 <= M <= 10^3
Time Limit: 1sec
Sample Input 1:
1
42
12
34
56
78
Sample Output 1:
3 7 11 15
Sample Input 2:
2
25
45326
75389
44
1234
9876
3456
-1 1 -10 5
Sample Output 2:
20 32
10 30 18 -5
Solution:
public class SumOfMatrixRows {
public static void main(String[] args) {
int i, j, sum;
int[][] SumOfRows_arr = {{15, 25, 35}, {45, 55, 65}, {75, 85, 95}};
for(i = 0; i < SumOfRows_arr.length; i++)
{
sum = 0;
for(j = 0; j < SumOfRows_arr[0].length; j++)
{
sum = sum + SumOfRows_arr[i][j];
}
System.out.println("\nThe Sum of Matrix Items in " + i + "
row = " + sum);
}
}
}
3)
Search a 2D Matrix
9877
308
Add to List
Share
Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This
matrix has the following properties:
● Integers in each row are sorted from left to right.
● The first integer of each row is greater than the last integer of the previous row.
Example 1:
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true
Example 2:
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false
Constraints:
● m == matrix.length
● n == matrix[i].length
● 1 <= m, n <= 100
● -104 <= matrix[i][j], target <= 104
import java.util.Arrays;
public class GFG {
public static void main(String[] args)
{
int arr[][] = { { 3, 12, 9 },
{ 5, 2, 89 },
{ 90, 45, 22 } };
int target = 89;
int ans[] = linearSearch(arr, target);
System.out.println("Element found at index: "
+ Arrays.toString(ans));
}
static int[] linearSearch(int[][] arr, int target)
{
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == target) {
return new int[] { i, j };
}
}
}
return new int[] { -1, -1 };
}
}