Sunday, 19 January 2025

UGC NET COMPUTER SCIENCE AND APPLICATIONS PAPER - 2 Question 2 November 2017 Solution

Q: 2 Let m=(313)4 and n=(322)4 . Find the base 4 expansion of m+n.

(1) (635)4

(2) (32312)4

(3) (21323)4

(4) (1301)4

Answer: (4)

Explanation:


Firstly convert m & n into base 10:

(313)4 = 3*42+1*41+3*40 = (55)10
(322)4 = 3*42+2*41+2*40
 = (58)10

Now m + n = 55+58 = (113)10

Now Convert (113)10 into base 4

  • When 113 is divided by 4, the quotient is 28 and the remainder is 1.
  • When 28 is divided by 4, the quotient is 7 and the remainder is 0.
  • When 7 is divided by 4, the quotient is 1 and the remainder is 3.
  • When 1 is divided by 4, the quotient is 0 and the remainder is 1.

Write the remainders from bottom to top.

So, (113)10 = (1301)4      which is Correct.

UGC NET COMPUTER SCIENCE AND APPLICATIONS PAPER - 2 Question 1 November 2017 Solution

UGC NET COMPUTER SCIENCE AND APPLICATIONS PAPER - 2 November 2017 Solution


Q: 1 If the time is now 4 O’clock, what will be the time after 101 hours from now ? 

(1) 9 O’clock (2) 8 O’clock (3) 5 O’clock (4) 4 O’clock

Answer: (1)

To find the time after 101 hours from 4 o’clock, we can use the modulo operation with respect to 12 (since it’s a 12-hour clock).
  1. First, calculate the remainder when 101 is divided by 12:

    Quotient is 8 and Remainder is 5
  2. Now add this remainder to the current time: 

    i.e. 4+5 = 9

    Thus, 101 hours from 4 o’clock will be 9 o’clock.
    The correct answer is (1) 9 O’clock.



Try Your Self:
Q:  If the time is now 2 O’clock, what will be the time after 55 hours from now?
2 O’clock
3 O’clock
10 O’clock
9 O’clock


Thursday, 2 January 2025

Find All Triplets with Zero Sum

  

Problem Explanation: Find All Triplets with Zero Sum

The task is to find all unique triplets in an array that add up to zero. A triplet consists of three numbers, and the sum of these three numbers should be zero. For example:

Input[-1, 0, 1, 2, -1, -4]
Output[[-1, -1, 2], [-1, 0, 1]]

Approach to Solve the Problem

  1. Sort the Array:
    First, sort the array in ascending order. Sorting helps to efficiently find the triplets using two pointers.

  2. Fix One Element:
    Iterate through the array and fix one element. For each fixed element, find two other elements (using two pointers) such that their sum equals the negative of the fixed element.

  3. Use Two Pointers:
    After fixing one element, use two pointers:

    • One pointer starts just after the fixed element.
    • The other pointer starts at the end of the array.
    • Move the pointers closer based on whether the current sum is less than, equal to, or greater than zero.
  4. Avoid Duplicates:
    Skip duplicate elements to ensure the triplets are unique.

  5. Add Valid Triplets:
    If the sum of the triplet is zero, add it to the result list.


Solution Code in Java

import java.util.*;

public class ZeroSumTriplets {

    public static List<List<Integer>> findTriplets(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums); // Step 1: Sort the array

        for (int i = 0; i < nums.length - 2; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue; // Skip duplicates for the first element
            }

            int left = i + 1; // Pointer 1
            int right = nums.length - 1; // Pointer 2

            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];

                if (sum == 0) {
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));

                    // Move both pointers and skip duplicates
                    while (left < right && nums[left] == nums[left + 1]) left++;
                    while (left < right && nums[right] == nums[right - 1]) right--;

                    left++;
                    right--;
                } else if (sum < 0) {
                    left++; // Move the left pointer to increase the sum
                } else {
                    right--; // Move the right pointer to decrease the sum
                }
            }
        }

        return result;
    }

    public static void main(String[] args) {
        int[] nums = {-1, 0, 1, 2, -1, -4};
        List<List<Integer>> triplets = findTriplets(nums);

        System.out.println("Triplets with zero sum: " + triplets);
    }
}

Explanation of the Code

  1. Sorting: The array is sorted to simplify the two-pointer approach.
  2. Outer Loop: Iterates through the array and fixes one element at a time.
  3. Inner Logic with Two Pointers:
    • Adjusts pointers based on the sum of the triplet.
    • Adds valid triplets to the result list while skipping duplicates.
  4. Output: The program prints all unique triplets with a zero sum.

Complexity

  • Time Complexity:
    Sorting the array takes O(nlogn), and the two-pointer approach runs in O(n2), making the overall complexity O(n2).
  • Space Complexity:
    O(n) for storing the result list.

This solution is efficient and easy to implement for finding triplets with zero sum.

#datastructure #coding #google #maang #leetcode

Monday, 22 April 2024

Why Learning to Code Matters in Computer Science

Coding is like giving instructions to a robot or a computer. You tell them what to do, step by step, and they do it. But it's not just about making computers do things; it's about solving puzzles and being creative!
Imagine you have a big problem to solve, like figuring out how to make a game or a cool app. Coding is the tool you use to make your ideas come to life. It's like having a magic wand that turns your thoughts into reality on a screen.
Learning to code isn't just about memorizing lots of complicated stuff. It's about learning how to think in a special way – a way that helps you break down big problems into smaller, manageable pieces. It's like solving a puzzle where every piece you put together gets you closer to the solution.
And guess what? Knowing how to code opens up tons of job opportunities! Companies all over the world are looking for people who can code because they need help making websites, apps, and all kinds of cool tech stuff. So, if you learn to code, you'll have lots of chances to find a fun and rewarding job.
But coding isn't just for getting jobs. It's also super important for understanding the world around us. Think about it: almost everything we use today, like smartphones, video games, and even smart home devices, runs on code. So, when you learn to code, you're not just learning a skill – you're learning how the world works!
In simple terms, coding is like learning a new language – a language that lets you talk to computers and create amazing things. So, if you're curious about how things work and love solving puzzles, learning to code might just be the perfect adventure for you!

Friday, 24 March 2023

NCERT Class 8th Textbook Mathematics/Ganit/Riyazi

 

Mathematics
Prelims(Open)
Guide for using QR Code(Open)
Chapter 1(Open)
Chapter 2(Open)
Chapter 3(Open)
Chapter 4(Open)
Chapter 5(Open)
Chapter 6(Open)
Chapter 7(Open)
Chapter 8(Open)
Chapter 9(Open)
Chapter 10(Open)
Chapter 11(Open)
Chapter 12(Open)
Chapter 13(Open)
Chapter 14(Open)
Chapter 15(Open)
Chapter 16(Open)
Answers(Open)
Download complete book

Ganit
Prelims(Open)
Guide for using QR Code(Open)
Chapter 1(Open)
Chapter 2(Open)
Chapter 3(Open)
Chapter 4(Open)
Chapter 5(Open)
Chapter 6(Open)
Chapter 7(Open)
Chapter 8(Open)
Chapter 9(Open)
Chapter 10(Open)
Chapter 11(Open)
Chapter 12(Open)
Chapter 13(Open)
Chapter 14(Open)
Chapter 15(Open)
Chapter 16(Open)
Answers(Open)
Download complete book
#ncert #ncertbooks #cbse #class8 #cbsebooks #ncertbooks
Riyazi
Prelims(Open)
Guide for using QR Code(Open)
Chapter 1(Open)
Chapter 2(Open)
Chapter 3(Open)
Chapter 4(Open)
Chapter 5(Open)
Chapter 6(Open)
Chapter 7(Open)
Chapter 8(Open)
Chapter 9(Open)
Chapter 10(Open)
Chapter 11(Open)
Chapter 12(Open)
Chapter 13(Open)
Chapter 14(Open)
Chapter 15(Open)
Chapter 16(Open)
Download complete book