Strivers Problem Solving Java
Strivers Problem Solving Java
Strivers Problem Solving Java
import java.util.*;
public class Solution {
char ch = sc.next().charAt(0);
}
}
----------------------------
2.Data Types:
switch(ch) {
case 1 :
double r = a[0];
return Math.PI*r*r;
// break;
case 2 :
double l = a[0];
double b = a[1];
return l*b;
// break;
default:
return 0;
}
// return 0.0;
}
}
----------------
5. for loop ( Nth Fibonacci Number) :
import java.util.Scanner;
for(int i =2;i<=n;i++){
c=a+b;
a=b;
b=c;
}
return b;
}
int n = sc.nextInt();
System.out.println(result);
}
------------------
import java.util.Scanner;
int sumEven = 0;
int sumOdd =0;
while(n>0){
int digit = n%10;
if(digit%2 == 0){
sumEven+=digit;
}else {
sumOdd+=digit;
}
n=n/10;
}
System.out.println(sumEven+" "+sumOdd);
}
}
=--------------------
7.Count Digits :
public class Solution {
public static int countDigits(int n){
// Write your code here.
int count=0;
int original =n;
while(n>0){
int digit = n%10;
}
n/=10;
}
return count;
}
}
Example:
Input: ‘n’ = 336
Output: 3
Explanation:
336 is divisible by both ‘3’ and ‘6’. Since ‘3’ occurs twice it is counted two
times.
-----------------
8. Reverse a Number:
Example 1:
Input: x = 123
Output: 321
class Solution {
public int reverse(int x) {
int revN = 0;
while (x != 0) {
int digit = x % 10;
x /= 10;
if (revN > Integer.MAX_VALUE / 10 || (revN == Integer.MAX_VALUE / 10 &&
digit > 7)) return 0;
if (revN < Integer.MIN_VALUE / 10 || (revN == Integer.MIN_VALUE / 10 &&
digit < -8)) return 0;
revN = revN * 10 + digit;
}
return revN;
}
}
OR
Sample Input 1 :
2
0
12
Sample Output 1:
0
805306368
Explanation For Sample Input 1 :
For the first test case :
Since the given number N = 0 is represented as 00000000000000000000000000000000 in
its binary representation. So after reversing the bits, it will become
00000000000000000000000000000000 which is equal to 0 only. So the output is 0.
-----------------
9.Check Palindrome:
class Solution {
public boolean isPalindrome(int x) {
int ori = x;
int rev =0;
}
if(ori == rev ){
return true;
}
return false;
}
}
----------
10. GCD OR HCF :
public class Solution {
public static int calcGCD(int n, int m){
// Write your code here.
while(m >0 && n>0){
if(n > m){
n=n%m;
}else{
m = m%n;
}
if(n == 0){
return m;
}
else{
return n;
}
// return 0;
}
}
--------------------
11. Armstrong number :
import java.util.*;
public class Main {
}
return cnt;
System.out.println(result);
}
}
----------------
12. Print all divisors (Sum of all divisors):
public class Solution {
public static int sumOfAllDivisors(int n){
int totalSum = 0;
for (int i = 1; i <= n; i++) {
totalSum += sumOfDivisors(i);
}
return totalSum;
}
}
return sum;
}
}
------------------
13. check prime :
public class Solution {
public static String isPrime(int num) {
//Your code goes here
if(num<= 1){
return "NO";
}
for(int i=2;i<=(int) Math.sqrt(num);i++){
if(num%i == 0){
return "NO";
}
return "YES";
}
}
----------
14. Largest Element in the Array:
import java.util.* ;
import java.io.*;
}
}
return max;
}
}
------------
15. Second Largest Number:
Sample Input 1 :
4
3 4 5 2
Sample Output 1 :
4 3
Explanation For Sample Input 1 :
The second largest element after 5 is 4 only, and the second smallest element after
2 is 3.
}
--------------
16. Check if the array is sorted:
public class Solution {
public static int isSorted(int n, int []a) {
// Write your code here.
int res =0;
for(int i=0;i<n;i++){
if(a[i]< res){
return 0;
}
res = a[i];
}
return 1;
}
}
------------
17. Remove duplicates from Sorted array:
public class Solution {
public static int removeDuplicates(int[] arr,int n) {
// Write your code here.
int i=0;
for(int j=1;j<n;j++){
if(arr[i] != arr[j]){
arr[i+1] = arr[j];
i++;
}
}
return i+1;
}
}
---------
18. Left Rotate an Array by One :
import java.util.* ;
import java.io.*;
}
arr[n-1] = temp;
return arr;
}
}
-------------
19. Left rotate an array by D places:
import java.util.ArrayList;
reverse(arr, 0, k - 1);
reverse(arr, k, n - 1);
reverse(arr, 0, n - 1);
return arr;
for(int i=0;i<n;i++){
if(a[i] == 0){
j=i;
break;
}
if(j == -1){
return a;
}
for(int i=j+1;i<n;i++){
if(a[i] !=0){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
j++;
}
return a;
}
}
-------------------
21. Linear Search :
import java.util.*;
public class Solution {
public static int linearSearch(int n, int num, int []arr){
// Write your code here.
for(int i=0;i<n;i++){
if(arr[i] == num){
search = i;
break;
}
return search;
}
}
------------------
22. find the missing number :
class Solution {
public int missingNumber(int[] nums) {
int N = nums.length;
for(int i=0;i<N;i++){
sum2 += nums[i];
}
return sum - sum2;
}
}
----------
23. Maximum consecutive ones :
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int n = nums.length;
int maxi = 0;
int cnt =0;
for(int i =0;i<n;i++){
if(nums[i] == 1){
cnt++;
maxi = Math.max(maxi,cnt);
}
else{
cnt = 0;
}
}
return maxi;
}
}
-----------