0% found this document useful (0 votes)
23 views1 page

Capgemini Coding Cheatsheet

The document contains multiple Java code snippets demonstrating various programming tasks. These include string compression, spiral matrix traversal, counting occurrences of elements in an array, solving a polynomial equation, handling dealership tyre data, finding factors of a number, and calculating maximum marks per semester. Each snippet provides a brief implementation of the respective functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views1 page

Capgemini Coding Cheatsheet

The document contains multiple Java code snippets demonstrating various programming tasks. These include string compression, spiral matrix traversal, counting occurrences of elements in an array, solving a polynomial equation, handling dealership tyre data, finding factors of a number, and calculating maximum marks per semester. Each snippet provides a brief implementation of the respective functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1.

String Compression
public class StringCompression { public static void main(String[] args) { String s =
"aabbbbeeeeffggg"; StringBuilder sb = new StringBuilder(); int count = 1; for(int
i=1; i

2. Spiral Matrix Traversal


class SpiralMatrix { public static void printSpiral(int[][] mat, int r, int c){ int
top=0, left=0, bottom=r-1, right=c-1; while(top<=bottom && left<=right){ for(int
i=left;i<=right;i++) System.out.print(mat[top][i]+" "); top++; for(int
i=top;i<=bottom;i++) System.out.print(mat[i][right]+" "); right--; if(top<=bottom){
for(int i=right;i>=left;i--) System.out.print(mat[bottom][i]+" "); bottom--; }
if(left<=right){ for(int i=bottom;i>=top;i--) System.out.print(mat[i][left]+" ");
left++; } } } }

3. Count Occurrences
import java.util.*; class CountOccurrences{ public static void main(String[] args){
int[] arr={1,2,3,3,4,1,4,5,1,2}; Map map=new HashMap<>(); for(int x:arr)
map.put(x,map.getOrDefault(x,0)+1); for(int k:map.keySet()) System.out.println(k+"
occurs "+map.get(k)+" times"); } }

4. Equation Solver
class Equation{ public static void main(String[] args){ int a=2, b=3; int
result=(a*a*a)+(3*a*a*b)+(3*a*b*b)+(b*b*b); System.out.println(result); } }

5. Dealership Tyres
class Dealership{ public static void main(String[] args){ int[][]
arr={{4,2},{4,0},{1,2}}; for(int i=0;i

6. Find Factors
class FindFactor{ public static void main(String[] args){ int n=54;
if(n==0){System.out.println("No Factors"); return;} n=Math.abs(n); for(int
i=1;i<=n;i++) if(n%i==0) System.out.print(i+" "); } }

7. Max Marks per Semester


import java.util.*; class MaxMarks{ public static void main(String[] args){ int[][]
marks={{50,60,70},{90,98,76,67},{89,76}}; for(int
i=0;i100){System.out.println("Invalid"); return;} else max=Math.max(max,x);
System.out.println("Max in semester " + (i+1) + ": "+max); } } }

You might also like