Posts

Tic-Tac-Toe Game in Java

Tic-Tac-Toe Java Code Tic-Tac-Toe Java Code Copy Code Download Code import java.util.*; class tic_tac_toe { Character a[][]; boolean right() { int n = 3; char ch = 'X'; int c = 0; for (int i = 0; i 2 || y>2 || x = 3 || y = 3 || a[x][y] != '-') { System.out.println("Invalid move! Try again."); x = sc.nextInt(); y = sc.nextInt(); } a[x][y] = 'X'; if (right() || left() || row() || column()) { for (int i = 0; i 2 || y > 2 || x = 3 || y = 3 || a[x][y] != '-') { System.out.println("Invalid move! Try again."); x = sc.nextInt(); y = sc.nextInt(); } a[x][y] = ...

Finding Subarrays with a Target Sum in Java: A Beginner's Guide

  PROGRAM : Finding a Subarray with a Target Sum in Java This program helps in identifying subarrays in a given array whose elements add up to a specific target sum ( k ). It takes input from the user for the size of the array, its elements, and the target sum. It then iterates through the array to find and display all subarrays that meet the criteria. CODE import java.util.*; class kth_subarray {     int a[], n, k = 0;     // Constructor to initialize default values     kth_subarray() {         n = 0;         k = 0;     }     // Method to take user input     void input() {         Scanner sc = new Scanner(System.in);         System.out.println("Enter the size of the array: ");         n = sc.nextInt();         a = new int[n];         System.out.println("Enter the elements: ");   ...

Majority Element in a Array

PROGRAM import java.util.*; public class majority_element{     public static void main(String args[]){         Scanner sc = new Scanner(System.in);         System.out.println("Enter the size: ");         int n = sc.nextInt();         System.out.println("Enter the elements: ");         int a[] = new int[n];         for(int i = 0; i<n; i++){             a[i] = sc.nextInt();         }         int max = n/2;         int k=0, c=0;         for(int i = 0; i<n; i++){             for(int j = 0; j<n; j++){                 if(a[i] == a[j])                     c++;             }         ...